uni-check-list.vue 3.6 KB

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