uni-check-list.vue 3.6 KB

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