uni-check-list.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="check-list-pages">
  3. <view>
  4. <view class="selList" v-for="(item, index) in list" :key="index">
  5. <view class="radios" @click="chooseItem(item)">
  6. <view v-if="checkIconPositon=='right'" style="flex-grow: 1;" class="title">{{ item[titleKeys] }}</view>
  7. <view>
  8. <radio v-if="types == 'radio'" :checked="item.checked" :value="item.id" />
  9. <checkbox v-if="types == 'checkbox'" :checked="item.checked" :value="item.id" />
  10. </view>
  11. <view v-if="checkIconPositon=='left'" class="title">{{ item[titleKeys] }}</view>
  12. </view>
  13. <view class="details" @click="rightClick(item)">
  14. <slot name="right"></slot>
  15. <u-icon v-if="showArrow && types != 'views'" name="icon-xian-11" custom-prefix="xd-icon" size="28" color="#888888"></u-icon>
  16. </view>
  17. </view>
  18. <view style="padding-top: 10vh;" v-if="list.length==0">
  19. <u-empty :text="noDataText" mode="list"></u-empty>
  20. </view>
  21. </view>
  22. <!-- 选择按钮 -->
  23. <view v-if="types == 'checkbox'" @click="kpOk" class="kp-ok">确认选择</view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: 'UniCheckList',
  29. props: {
  30. listData: {
  31. type: Array,
  32. default: function() {
  33. return [];
  34. }
  35. },
  36. // 类型 radio 或 checkbox 或 views 指 只是展示列表
  37. types: {
  38. type: String,
  39. default: 'radio'
  40. },
  41. // 标题显示的字段
  42. titleKeys: {
  43. type: String,
  44. default: 'name'
  45. },
  46. // 暂无数据提示内容
  47. noDataText: {
  48. type: String,
  49. default: '暂无考评方案'
  50. },
  51. // 是否显示右边箭头
  52. showArrow: {
  53. type: Boolean,
  54. default: true
  55. },
  56. // 赋值,格式要和backValue 一致
  57. defValue: {
  58. type: Array,
  59. default: function() {
  60. return [];
  61. }
  62. },
  63. // 返回结果是
  64. // idArr: [id,id,id...],objArr:[obj,obj,obj...]
  65. backValue:{
  66. type: String,
  67. default: 'idArr'
  68. },
  69. // 选择按钮位置,left ,right
  70. checkIconPositon:{
  71. type: String,
  72. default: 'left'
  73. }
  74. },
  75. data() {
  76. return {
  77. list: this.listData,
  78. value: this.defValue
  79. };
  80. },
  81. watch: {
  82. listData(newValue, oldValue) {
  83. this.list = newValue;
  84. this.hasCheck();
  85. },
  86. defValue(newValue) {
  87. console.log(newValue)
  88. this.value = newValue;
  89. this.hasCheck();
  90. }
  91. },
  92. mounted() {
  93. this.hasCheck();
  94. },
  95. methods: {
  96. hasCheck() {
  97. this.list.map(item => {
  98. let a = this.value.findIndex(k=>{
  99. return this.backValue == 'idArr' ? k == item.id : k.id == item.id
  100. })
  101. item.checked = a>=0
  102. })
  103. console.log(this.list,'111111111')
  104. },
  105. // 选中
  106. chooseItem(item) {
  107. // 单选
  108. if (this.types == 'radio') {
  109. this.value[0] = this.backValue == 'idArr' ? item.id : item;
  110. this.$emit('ok', this.value);
  111. } else {
  112. // 取消选中
  113. if (item.checked) {
  114. let index = this.backValue == 'idArr' ? this.value.indexOf(item.id) : this.value.findIndex(a=> a.id == item.id);
  115. this.value.splice(index, 1);
  116. } else {
  117. this.value.push(this.backValue == 'idArr' ? item.id : item);
  118. }
  119. }
  120. },
  121. rightClick(item) {
  122. this.$emit('rightClick', item);
  123. },
  124. kpOk() {
  125. this.$emit('ok', this.value);
  126. }
  127. }
  128. };
  129. </script>
  130. <style lang="less">
  131. .check-list-pages {
  132. height: 100%;
  133. display: flex;
  134. flex-direction: column;
  135. > view {
  136. &:first-child {
  137. flex-grow: 1;
  138. overflow: auto;
  139. }
  140. }
  141. }
  142. .selList {
  143. width: 100%;
  144. border-bottom: 1px solid #eee;
  145. display: flex;
  146. align-items: center;
  147. .radios {
  148. display: flex;
  149. align-items: center;
  150. flex-grow: 1;
  151. width: 50%;
  152. padding: 26upx 20upx;
  153. &:active {
  154. background-color: #f8f8f8;
  155. }
  156. .title {
  157. word-break: break-all;
  158. }
  159. }
  160. .details {
  161. color: #007aff;
  162. padding-right: 20upx;
  163. display: flex;
  164. align-items: center;
  165. &:active {
  166. color: #d9363e;
  167. }
  168. }
  169. }
  170. .kp-ok {
  171. text-align: center;
  172. padding: 26upx;
  173. background: #55aaff;
  174. color: #fff;
  175. font-size: 18px;
  176. }
  177. </style>