cartPopup.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <u-popup :show="show" mode="bottom" @close="close" @open="open" :round="10">
  3. <view class="my-popup padding">
  4. <view class="container-sel">
  5. <radio :checked="allSelected" color="#E51C23" @click="selectAll">全选
  6. </radio>
  7. <view class="dflex">
  8. <u-tag text="清空购物车" type="info" color="#000" borderColor="#fff" size="mini" icon="trash" plain
  9. @click="clearCart"></u-tag>
  10. </view>
  11. </view>
  12. <radio class="w-full padding-bottom-lg" color="#E51C23" v-for="(item, index) in localCarList" :key="index"
  13. :name="item.name" :checked="item.selected" @click="radioChange(item)">
  14. <my-goods :item="item" ref="my-goods">
  15. <view class="num-step">
  16. <u-number-box v-model="item.goods_num" :min="1" @change="changeValue($event, item.id)" />
  17. </view>
  18. </my-goods>
  19. </radio>
  20. </view>
  21. </u-popup>
  22. </template>
  23. <script>
  24. export default {
  25. name: "cartPopup",
  26. props: {
  27. carList: {
  28. type: Array,
  29. required: true
  30. }
  31. },
  32. data() {
  33. return {
  34. show: false,
  35. allSelected: false,
  36. localCarList: []
  37. };
  38. },
  39. methods: {
  40. open() {},
  41. setShow(v) {
  42. this.show = v;
  43. // 检查 localCarList 是否存在且至少有一个元素
  44. if (this.localCarList && this.localCarList.length > 0) {
  45. // 检查 localCarList 的第一个元素是否有 selected 属性
  46. if (!this.localCarList[0].hasOwnProperty('selected')) {
  47. // 如果没有 selected 属性,使用 map 方法添加 selected 属性
  48. this.localCarList = this.carList.map(item => ({
  49. ...item,
  50. selected: true
  51. }));
  52. }
  53. } else {
  54. // 如果 localCarList 不存在或没有元素,直接使用 map 方法添加 selected 属性
  55. this.localCarList = this.carList.map(item => ({
  56. ...item,
  57. selected: true
  58. }));
  59. }
  60. if (this.localCarList.length > 0) {
  61. this.allSelected = this.localCarList.every(car => car.selected)
  62. }
  63. console.log('this.localCarList', this.localCarList)
  64. },
  65. close() {
  66. this.show = false;
  67. },
  68. changeValue(value, index) {
  69. this.$emit('numChanged', value.value, index)
  70. console.log('更新后的数量=======:', value, '索引=========:', index)
  71. },
  72. selectAll() {
  73. this.allSelected = !this.allSelected
  74. this.localCarList.forEach(item => {
  75. item.selected = this.allSelected
  76. })
  77. this.$emit('selected-changed', this.localCarList)
  78. console.log('this.localCarList111111111111111', this.localCarList)
  79. },
  80. clearCart() {
  81. this.localCarList = []
  82. this.allSelected = false
  83. this.$emit('selected-changed', this.localCarList)
  84. },
  85. radioChange(e) {
  86. console.log(e)
  87. e.selected = !e.selected
  88. // 检查是否所有车辆都被选中了
  89. this.allSelected = this.localCarList.every(car => car.selected)
  90. this.$emit('selected-changed', this.localCarList);
  91. // 打印结果
  92. console.log('this.allSelected', this.allSelected);
  93. }
  94. }
  95. }
  96. </script>
  97. <style scoped lang="scss">
  98. .my-popup {
  99. width: 100%;
  100. max-height: 800rpx;
  101. overflow: auto;
  102. padding-bottom: 130rpx;
  103. .num-step {
  104. width: 200rpx;
  105. padding-top: 40rpx;
  106. }
  107. .container-sel {
  108. display: flex;
  109. justify-content: space-between;
  110. align-items: center;
  111. margin: 10px 0;
  112. }
  113. }
  114. </style>