uni-check-list.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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: [],
  78. value: this.defValue
  79. };
  80. },
  81. watch: {
  82. listData(newValue, oldValue) {
  83. this.list = JSON.parse(JSON.stringify(newValue));
  84. this.hasCheck();
  85. },
  86. defValue(newValue, oldValue){
  87. this.value = newValue;
  88. }
  89. },
  90. mounted() {
  91. this.hasCheck();
  92. },
  93. methods: {
  94. hasCheck() {
  95. this.list.map((item,index) => {
  96. let a = this.value.findIndex(k=>{
  97. return this.backValue == 'idArr' ? k == item.id : k.id == item.id
  98. })
  99. item.checked = a>=0
  100. this.list.splice(index,1,item)
  101. })
  102. },
  103. // 选中
  104. chooseItem(item) {
  105. // 单选
  106. if (this.types == 'radio') {
  107. this.value[0] = this.backValue == 'idArr' ? item.id : item;
  108. this.$emit('ok', this.value);
  109. } else {
  110. // 取消选中
  111. if (item.checked) {
  112. let index = this.backValue == 'idArr' ? this.value.indexOf(item.id) : this.value.findIndex(a=> a.id == item.id);
  113. this.value.splice(index, 1);
  114. } else {
  115. this.value.push(this.backValue == 'idArr' ? item.id : item);
  116. }
  117. }
  118. this.hasCheck();
  119. },
  120. rightClick(item) {
  121. this.$emit('rightClick', item);
  122. },
  123. kpOk() {
  124. this.$emit('ok', this.value);
  125. }
  126. }
  127. };
  128. </script>
  129. <style lang="less">
  130. .check-list-pages {
  131. height: 100%;
  132. display: flex;
  133. flex-direction: column;
  134. > view {
  135. &:first-child {
  136. flex-grow: 1;
  137. overflow: auto;
  138. }
  139. }
  140. }
  141. .selList {
  142. width: 100%;
  143. border-bottom: 1px solid #eee;
  144. display: flex;
  145. align-items: center;
  146. .radios {
  147. display: flex;
  148. align-items: center;
  149. flex-grow: 1;
  150. width: 50%;
  151. padding: 26upx 20upx;
  152. &:active {
  153. background-color: #f8f8f8;
  154. }
  155. .title {
  156. word-break: break-all;
  157. }
  158. }
  159. .details {
  160. color: #007aff;
  161. padding-right: 20upx;
  162. display: flex;
  163. align-items: center;
  164. &:active {
  165. color: #d9363e;
  166. }
  167. }
  168. }
  169. .kp-ok {
  170. text-align: center;
  171. padding: 26upx;
  172. background: #55aaff;
  173. color: #fff;
  174. font-size: 18px;
  175. }
  176. </style>