uni-check-list.vue 3.6 KB

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