uni-check-list.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. watch: {
  58. listData(newValue, oldValue) {
  59. this.list = newValue
  60. }
  61. },
  62. mounted() {
  63. this.hasCheck()
  64. },
  65. methods: {
  66. hasCheck(){
  67. this.list.map(item => {
  68. let a = this.value.indexOf(item.id)
  69. item.checked = a >= 0
  70. })
  71. this.list.splice()
  72. },
  73. // 选中
  74. chooseItem(item) {
  75. // 单选
  76. if(this.types == 'radio'){
  77. this.value[0] = item.id
  78. this.$emit('ok',this.value)
  79. }else{
  80. // 取消选中
  81. if(item.checked){
  82. let index = this.value.indexOf(item.id)
  83. this.value.splice(index,1)
  84. }else{
  85. this.value.push(item.id)
  86. }
  87. }
  88. this.hasCheck()
  89. },
  90. rightClick(item){
  91. this.$emit('rightClick',item)
  92. },
  93. kpOk(){
  94. this.$emit('ok',this.value)
  95. }
  96. },
  97. }
  98. </script>
  99. <style lang="less">
  100. .check-list-pages{
  101. height: 100%;
  102. display: flex;
  103. flex-direction: column;
  104. > view{
  105. &:first-child{
  106. flex-grow: 1;
  107. }
  108. }
  109. }
  110. .selList{
  111. width: 100%;
  112. border-bottom: 1px solid #eee;
  113. display: flex;
  114. align-items: center;
  115. .radios{
  116. display: flex;
  117. align-items: center;
  118. flex-grow: 1;
  119. width: 50%;
  120. padding: 26upx 20upx;
  121. &:active{
  122. background-color: #F8F8F8;
  123. }
  124. text{
  125. word-break: break-all;
  126. }
  127. }
  128. .details{
  129. color: #007AFF;
  130. padding-right: 20upx;
  131. display: flex;
  132. align-items: center;
  133. &:active{
  134. color: #D9363E;
  135. }
  136. }
  137. }
  138. .kp-ok{
  139. text-align: center;
  140. padding: 26upx;
  141. background: #55aaff;
  142. color: #fff;
  143. font-size: 18px;
  144. }
  145. </style>