uni-check-list.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <view class="check-list-pages">
  3. <view>
  4. <view
  5. class="selList"
  6. v-for="(item, index) in list" :key="index"
  7. >
  8. <view class="radios" @click="chooseItem(item)">
  9. <radio v-if="types=='radio'" :checked="item.checked" :value="item.id" />
  10. <checkbox v-if="types=='checkbox'" :checked="item.checked" :value="item.id" />
  11. <text>{{item[titleKeys]}}</text>
  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>
  19. <!-- 选择按钮 -->
  20. <view v-if="types=='checkbox'" @click="kpOk" class="kp-ok">
  21. 确认选择
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default{
  27. name: 'UniCheckList',
  28. props: {
  29. listData: {
  30. type: Array,
  31. default: function(){
  32. return []
  33. }
  34. },
  35. // 类型 radio 或 checkbox 或 views 指 只是展示列表
  36. types:{
  37. type: String,
  38. default: 'radio'
  39. },
  40. // 标题显示的字段
  41. titleKeys:{
  42. type: String,
  43. default: 'name'
  44. },
  45. // 是否显示右边箭头
  46. showArrow:{
  47. type: Boolean,
  48. default: true
  49. }
  50. },
  51. data() {
  52. return {
  53. list: this.listData,
  54. value: []
  55. }
  56. },
  57. mounted() {
  58. this.hasCheck()
  59. },
  60. methods: {
  61. hasCheck(){
  62. this.list.map(item => {
  63. let a = this.value.indexOf(item.id)
  64. item.checked = a >= 0
  65. })
  66. this.list.splice()
  67. },
  68. // 选中
  69. chooseItem(item) {
  70. // 单选
  71. if(this.types == 'radio'){
  72. this.value[0] = item.id
  73. this.$emit('ok',this.value)
  74. }else{
  75. // 取消选中
  76. if(item.checked){
  77. let index = this.value.indexOf(item.id)
  78. this.value.splice(index,1)
  79. }else{
  80. this.value.push(item.id)
  81. }
  82. }
  83. this.hasCheck()
  84. },
  85. rightClick(item){
  86. this.$emit('rightClick',item)
  87. },
  88. kpOk(){
  89. this.$emit('ok',this.value)
  90. }
  91. },
  92. }
  93. </script>
  94. <style lang="less">
  95. .check-list-pages{
  96. height: 100%;
  97. display: flex;
  98. flex-direction: column;
  99. > view{
  100. &:first-child{
  101. flex-grow: 1;
  102. }
  103. }
  104. }
  105. .selList{
  106. width: 100%;
  107. border-bottom: 1px solid #eee;
  108. display: flex;
  109. align-items: center;
  110. .radios{
  111. display: flex;
  112. align-items: center;
  113. flex-grow: 1;
  114. width: 50%;
  115. padding: 26upx 20upx;
  116. &:active{
  117. background-color: #F8F8F8;
  118. }
  119. text{
  120. word-break: break-all;
  121. }
  122. }
  123. .details{
  124. color: #007AFF;
  125. padding-right: 20upx;
  126. display: flex;
  127. align-items: center;
  128. &:active{
  129. color: #D9363E;
  130. }
  131. }
  132. }
  133. .kp-ok{
  134. text-align: center;
  135. padding: 26upx;
  136. background: #55aaff;
  137. color: #fff;
  138. font-size: 18px;
  139. }
  140. </style>