uni-check-list.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. <u-checkbox v-if="types == 'checkbox'" :disabled="item.disabled" v-model="item.checked" :name="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 && status!='loading'">
  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. status: {
  53. type: String,
  54. default: 'onmore'
  55. },
  56. // 是否显示右边箭头
  57. showArrow: {
  58. type: Boolean,
  59. default: true
  60. },
  61. // 赋值,格式要和backValue 一致
  62. defValue: {
  63. type: Array,
  64. default: function() {
  65. return [];
  66. }
  67. },
  68. // 返回结果是
  69. // idArr: [id,id,id...],objArr:[obj,obj,obj...]
  70. backValue:{
  71. type: String,
  72. default: 'idArr'
  73. },
  74. // 选择按钮位置,left ,right
  75. checkIconPositon:{
  76. type: String,
  77. default: 'left'
  78. }
  79. },
  80. data() {
  81. return {
  82. list: JSON.parse(JSON.stringify(this.listData)),
  83. value: this.defValue
  84. };
  85. },
  86. watch: {
  87. listData(newValue, oldValue) {
  88. this.list = JSON.parse(JSON.stringify(newValue));
  89. this.hasCheck();
  90. },
  91. defValue(newValue, oldValue){
  92. this.value = newValue;
  93. }
  94. },
  95. mounted() {
  96. this.hasCheck();
  97. },
  98. methods: {
  99. hasCheck() {
  100. this.list.map((item,index) => {
  101. let a = this.value.findIndex(k=>{
  102. return this.backValue == 'idArr' ? k == item.id : k.id == item.id
  103. })
  104. item.checked = a>=0
  105. this.list.splice(index,1,item)
  106. console.log(this.list,'this.list')
  107. })
  108. },
  109. // 选中
  110. chooseItem(item) {
  111. if (item.disabled) {
  112. return
  113. }
  114. // 单选
  115. if (this.types == 'radio') {
  116. this.value[0] = this.backValue == 'idArr' ? item.id : item;
  117. this.$emit('ok', this.value);
  118. } else {
  119. // 取消选中
  120. if (item.checked) {
  121. this.value.push(this.backValue == 'idArr' ? item.id : item);
  122. } else {
  123. let index = this.backValue == 'idArr' ? this.value.indexOf(item.id) : this.value.findIndex(a=> a.id == item.id);
  124. this.value.splice(index, 1);
  125. }
  126. }
  127. this.hasCheck();
  128. },
  129. rightClick(item) {
  130. this.$emit('rightClick', item);
  131. },
  132. kpOk() {
  133. this.$emit('ok', this.value);
  134. }
  135. }
  136. };
  137. </script>
  138. <style lang="less">
  139. .check-list-pages {
  140. height: 100%;
  141. display: flex;
  142. flex-direction: column;
  143. > view {
  144. &:first-child {
  145. flex-grow: 1;
  146. overflow: auto;
  147. }
  148. }
  149. }
  150. .selList {
  151. width: 100%;
  152. border-bottom: 1px solid #eee;
  153. display: flex;
  154. align-items: center;
  155. .radios {
  156. display: flex;
  157. align-items: center;
  158. flex-grow: 1;
  159. width: 50%;
  160. padding: 26upx 20upx;
  161. &:active {
  162. background-color: #f8f8f8;
  163. }
  164. .title {
  165. word-break: break-all;
  166. }
  167. }
  168. .details {
  169. color: #007aff;
  170. padding-right: 20upx;
  171. display: flex;
  172. align-items: center;
  173. &:active {
  174. color: #d9363e;
  175. }
  176. }
  177. }
  178. .kp-ok {
  179. text-align: center;
  180. padding: 26upx;
  181. background: #55aaff;
  182. color: #fff;
  183. font-size: 18px;
  184. }
  185. </style>