check-group.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view class="radio-group-view">
  3. <view v-if="label" class="label-view">
  4. <text>{{ label }}</text>
  5. </view>
  6. <view class="radio-group">
  7. <template v-for="(item, i) in list">
  8. <view :class="{'radio-item':true}" :key="i" @click="toggleSelect(item, i)">
  9. <text class="text">{{ item[setting.name] }}</text>
  10. <image class="icon"
  11. :src="`/static/${checkedValues.includes(item[setting.value]) ? 'check-checkbox' : 'check-no'}.png`">
  12. </image>
  13. </view>
  14. </template>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. /**
  20. * 单选列表组合
  21. * 芦荟
  22. * 2021.2.2
  23. * props:属性说明看tm-radio-gruop.vue
  24. */
  25. export default {
  26. props: ['list', 'defaultValue', 'label', 'setting', 'pIndex'],
  27. data:function(){
  28. return {
  29. //已选中的项
  30. checkedList:[]
  31. }
  32. },
  33. computed:{
  34. checkedValues(){
  35. // console.log('this.defaultValue',this.defaultValue);
  36. this.checkedList = this.defaultValue;
  37. return this.defaultValue.map(item=>item[this.setting.value]);
  38. }
  39. },
  40. methods: {
  41. /**
  42. * 选中变化调用
  43. * @param {Object} selectData 当前选中的对象
  44. * @param {Object} index 当前选中下标
  45. *
  46. * 返回的参数
  47. * selectData[this.setting.value]: 当前选中的值
  48. * selectData: 当前选中的整条数据
  49. * index: 当前选中的下标
  50. */
  51. toggleSelect(selectData, index) {
  52. const positionIndex = this.checkedList.findIndex(item=>item[this.setting.value] == selectData[this.setting.value]);
  53. console.log({selectData, index,positionIndex});
  54. if(positionIndex != -1){
  55. this.checkedList.splice(positionIndex,1);
  56. }else {
  57. this.checkedList.push(selectData);
  58. }
  59. this.$emit(
  60. 'change',
  61. {
  62. checkedList:this.checkedList, //已选中的集合
  63. parentIndex:this.pIndex //父级index
  64. }
  65. );
  66. }
  67. }
  68. }
  69. </script>
  70. <style lang="less">
  71. .radio-group-view {
  72. .label-view {
  73. height: 47.5rpx;
  74. line-height: 45rpx;
  75. padding-left: 25rpx;
  76. >text {
  77. font-size: 22.5rpx;
  78. color: #666F80;
  79. }
  80. }
  81. .radio-group {
  82. padding-left: 25rpx;
  83. background-color: #fff;
  84. border-bottom: 0.62rpx solid #DADEE6;
  85. .radio-item {
  86. display: flex;
  87. align-items: center;
  88. justify-content: space-between;
  89. height: 87.5rpx;
  90. border-bottom: 0.62rpx solid #DADEE6;
  91. padding-right: 25rpx;
  92. .text {
  93. font-size: 22.5rpx;
  94. color: #292C33;
  95. }
  96. .icon {
  97. width: 25rpx;
  98. height: 25rpx;
  99. }
  100. &:last-child {
  101. border-bottom: none;
  102. }
  103. }
  104. }
  105. }
  106. </style>