diy-goods-group.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template id="diy-goods-group">
  2. <div class="pick-goods">
  3. <com-pick-link @selected="selectNavUrl" :default-select-list="selectList" :title="title" :show-id="showId" :pick-type="pickType" :default-tabs="defaultTabs">
  4. <slot></slot>
  5. </com-pick-link>
  6. <div flex="cross:center" style="margin-top: 20px;flex-wrap:wrap" v-if="pickImgList.length">
  7. <div class="pickImgItem" v-for="(item,index) in pickImgList" :key="index" @mouseenter="mouseenter(index)" @mouseleave="mouseleave(index)">
  8. <el-image style="width: 50px; height: 50px" :src="item.params.img" fit="contain"></el-image>
  9. <div v-show="index == pickIndex || item.visible">
  10. <el-popover placement="bottom" v-model="item.visible">
  11. <p class="diy-del-text">确定删除吗?</p>
  12. <div flex="cross:center">
  13. <el-button class="diy-del-btn" size="mini" plain @click="delModel(index)">删除</el-button>
  14. <el-button size="mini" plain @click="item.visible = false">取消</el-button>
  15. </div>
  16. <i class="el-icon-close" slot="reference" style="color:#999;"></i>
  17. </el-popover>
  18. </div>
  19. </div>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. Vue.component('diy-goods-group', {
  25. name: "diy-goods-group",
  26. template: "#diy-goods-group",
  27. props: {
  28. /* 选择链接弹窗 */
  29. // 最大数量:-1(无限制)
  30. maxLength: {
  31. type: [String,Number],
  32. default: -1
  33. },
  34. // 传入数组,数组可字符串和对象 [{name: 'goods',children:['goods_cate'] }]
  35. defaultTabs: { // 选择需要的tabs栏
  36. type: Array,
  37. default () {
  38. return []
  39. },
  40. },
  41. showId: { // 单选按钮旁边是否显示id
  42. type: Boolean,
  43. default: true
  44. },
  45. pickType: { // 列表选择多个还是单个参数,如:['online_goods','diy'],默认单选
  46. type: Array,
  47. default () {
  48. return []
  49. },
  50. },
  51. title: { //弹窗标题
  52. type: String,
  53. default: '',
  54. },
  55. /* 选中的商品列表 */
  56. list: {
  57. type: Array,
  58. default: () => {
  59. return []
  60. },
  61. }
  62. },
  63. data() {
  64. return {
  65. pickIndex: -1,
  66. pickImgList: [],
  67. selectList: [],
  68. }
  69. },
  70. watch: {
  71. list: {
  72. immediate: true,
  73. handler(val) {
  74. this.pickImgList = [];
  75. this.pickImgList.push(...val)
  76. this.setDefaultSelectIds();
  77. },
  78. },
  79. },
  80. methods: {
  81. selectNavUrl(e) {
  82. let self = this;
  83. e = e.filter(function (item) {
  84. if (self.selectList.length > 0) {
  85. return !self.selectList.includes(item.params.id);
  86. }
  87. return item;
  88. });
  89. if (this.maxLength != -1 && this.pickImgList.length > this.maxLength) {
  90. this.$message({
  91. message: `最多添加${this.maxLength}件商品噢`,
  92. type: 'warning'
  93. });
  94. }
  95. this.pickImgList.push(...e);
  96. this.$emit("change",e, -1, 'add');
  97. this.setDefaultSelectIds();
  98. },
  99. setDefaultSelectIds() {
  100. let pickData = this.pickImgList;
  101. let selectIds = [];
  102. pickData.forEach(item => {
  103. selectIds.push(item.params.id);
  104. })
  105. this.selectList = selectIds.filter((item, index, selectIds) => selectIds.indexOf(item) === index);
  106. },
  107. mouseenter(index) {
  108. this.pickIndex = index
  109. },
  110. mouseleave() {
  111. this.pickIndex = -1
  112. },
  113. delModel(index) {
  114. this.pickImgList.splice(index, 1)
  115. console.info("删除",this.pickImgList);
  116. this.$emit("change",this.pickImgList, index, 'del');
  117. this.selectList.splice(index, 1);
  118. },
  119. }
  120. });
  121. </script>
  122. <style>
  123. .diy-tab-item {
  124. padding: 10px;
  125. }
  126. .pickImgItem {
  127. position: relative;
  128. width: 50px;
  129. height: 50px;
  130. margin: 0 9px 9px 0;
  131. background-color: #ddd;
  132. cursor: pointer;
  133. }
  134. .pickImgItem .el-icon-close {
  135. position: absolute;
  136. top: 0;
  137. right: 0;
  138. font-size: 12px;
  139. }
  140. .add-goods-btn {
  141. border: 1px dashed #6b7685;
  142. line-height: 32px;
  143. height: 32px;
  144. border-radius: 4px;
  145. text-align: center;
  146. cursor: pointer;
  147. }
  148. </style>