accessories.vue 4.2 KB

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