accessories.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <div>
  3. <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
  4. <el-checkbox-group class="options" v-if="groupOptions.length" v-model="checkeds" @change="optionChange">
  5. <el-checkbox class="option" v-for="item in groupOptions" :label="item.id" :key="item.id">
  6. <strong>
  7. <span>{{item.name}}</span>
  8. <span>{{item.price}}元</span>
  9. </strong>
  10. <p v-for="sub in item.subs" :key="sub">{{sub}}</p>
  11. </el-checkbox>
  12. </el-checkbox-group>
  13. <el-checkbox-group class="options" v-else v-model="checkeds" @change="optionChange">
  14. <el-checkbox class="option" v-for="item in list" :label="item.id" :key="item.id">
  15. <strong>
  16. <span>{{item.name}}</span>
  17. <span>{{item.groupPurchasePrice > 0 ? item.groupPurchasePrice + ' 元' : '免费'}}</span>
  18. </strong>
  19. </el-checkbox>
  20. </el-checkbox-group>
  21. </div>
  22. </template>
  23. <script>
  24. export default {
  25. props: ['list', 'groupList'],
  26. data() {
  27. return {
  28. type: 'list',
  29. checkAll: false,
  30. checkeds: [],
  31. accessoriesByid: {},
  32. groupListById: {},
  33. selectMoney: 0,
  34. isIndeterminate: false
  35. };
  36. },
  37. watch: {
  38. checkeds(vals) {
  39. const optionsById = this.type === 'list' ? this.accessoriesByid : this.groupListById
  40. let selectMoney = 0
  41. if (this.type === 'list') {
  42. for (const item of vals) {
  43. const activeItem = this.accessoriesByid[item]
  44. if (activeItem) {
  45. selectMoney += parseFloat(activeItem.groupPurchasePrice)
  46. }
  47. }
  48. } else {
  49. for (const item of vals) {
  50. const activeItem = this.groupListById[item]
  51. if (activeItem) {
  52. selectMoney += parseFloat(activeItem.price)
  53. }
  54. }
  55. }
  56. let formatids = []
  57. if (this.type !== 'list') {
  58. for (const item of vals) {
  59. const active = this.groupListById[item]
  60. if (active) {
  61. const { goodsList } = this.groupListById[item]
  62. formatids = formatids.concat((goodsList || []).map(goods => goods.id))
  63. }
  64. }
  65. }
  66. this.$listeners.change(formatids, selectMoney)
  67. }
  68. },
  69. mounted() {
  70. this.$nextTick(() => {
  71. const accessoriesByid = {}
  72. for (const item of this.list) {
  73. accessoriesByid[item.id] = item
  74. }
  75. const groupListById = {}
  76. for (const item of this.groupList) {
  77. groupListById[item.id] = item
  78. }
  79. this.accessoriesByid = accessoriesByid
  80. this.groupListById = groupListById
  81. })
  82. },
  83. computed: {
  84. groupOptions() {
  85. console.log(this.groupList)
  86. const options = this.groupList.map(item => ({
  87. name: item.name,
  88. price:item.price,
  89. id: item.id,
  90. subs: item.goodsList ? item.goodsList.map(sub => sub.name) : []
  91. }))
  92. if (options.length) {
  93. this.type = 'group'
  94. }
  95. return options
  96. }
  97. },
  98. methods: {
  99. handleCheckAllChange(val) {
  100. const options = this.type === 'list' ? this.list : this.groupOptions
  101. const allids = options.map(item => item.id)
  102. this.checkeds = val ? allids : [];
  103. this.isIndeterminate = false;
  104. },
  105. optionChange(value) {
  106. const options = this.type === 'list' ? this.list : this.groupOptions
  107. const checkedCount = value.length;
  108. this.checkAll = checkedCount === options.length;
  109. this.isIndeterminate = checkedCount > 0 && checkedCount < options.length;
  110. }
  111. }
  112. };
  113. </script>
  114. <style lang="less" scoped>
  115. .options {
  116. display: block;
  117. .option{
  118. display: flex;
  119. width: 100%;
  120. margin-top: 10px;
  121. /deep/ .el-checkbox__label{
  122. flex: 1;
  123. >strong{
  124. display: flex;
  125. justify-content: space-between;
  126. align-items: center;
  127. >span:last-child{
  128. color: red;
  129. }
  130. }
  131. >p{
  132. line-height: 1.8;
  133. color: #606266;
  134. }
  135. }
  136. }
  137. }
  138. </style>