com-attr-group.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <style>
  2. .com-attr-group {
  3. border: 1px solid #EBEEF5;
  4. padding: 10px;
  5. }
  6. .com-attr-group .bg {
  7. height: 44px;
  8. padding: 0 10px;
  9. background: #f8f8f8;
  10. }
  11. .com-attr-group .del-img {
  12. height: 20px;
  13. width: 20px;
  14. cursor: pointer;
  15. }
  16. .com-attr-group .img-box {
  17. position: relative;
  18. height: 100px;
  19. width: 100px;
  20. margin-top: 8px;
  21. border: 1px solid #EBEEF5;
  22. }
  23. .com-attr-group .attr-jt {
  24. background: #ffffff;
  25. width: 6px;
  26. height: 6px;
  27. border-top: 1px solid rgb(235, 238, 245);
  28. border-right: 1px solid #EBEEF5;
  29. transform: rotate(-45deg);
  30. -o-transform: rotate(-45deg);
  31. -webkit-transform: rotate(-45deg);
  32. -moz-transform: rotate(-45deg);
  33. -ms-transform: rotate(-45deg);
  34. position: absolute;
  35. top: -5px;
  36. }
  37. .com-attr-group .close {
  38. position: absolute;
  39. top: -4px;
  40. right: -4px;
  41. font-size: 16px;
  42. cursor: pointer;
  43. }
  44. .com-attr-group .attr-list {
  45. display: inline-block;
  46. margin-right: 10px;
  47. margin-bottom: 10px;
  48. position: relative;
  49. cursor: move;
  50. }
  51. </style>
  52. <template id="com-attr-group">
  53. <div class="com-attr-group">
  54. <div v-for="(group, i) in value" :key="i" style="margin-bottom: 16px">
  55. <div flex="dir:left cross:center" class="bg">
  56. <span>规格名:</span>
  57. <el-input type="text" v-model.trim="group.attr_group_name" size="mini" @input="makeAttrGroup"
  58. style="width:94px;margin:0 16px"></el-input>
  59. <el-checkbox v-if="i === 0" v-model="attrPic">规格图片</el-checkbox>
  60. <div @click="deleteAttrGroup(i)" style="margin-left: auto;line-height: 1">
  61. <el-image class="del-img" src="resources/img/mall/order/del.png"></el-image>
  62. </div>
  63. </div>
  64. <div flex="dir:left" style="padding:0 10px;margin-top: 16px">
  65. <div class="box-grow-0">规格值:</div>
  66. <draggable :options="{draggable:'.attr-list'}" @end="makeAttrGroup"
  67. flex="dir:left" style="flex-wrap: wrap" v-model="group.attr_list">
  68. <div class="attr-list" v-for="(attr, j) in group.attr_list" :key="j">
  69. <el-input style="width:152px" size="mini" type="text" @input="makeAttrGroup"
  70. v-model.trim="attr.attr_name"></el-input>
  71. <i class="el-icon-error close" @click="deleteAttr(i,j)"></i>
  72. <div flex="cross:center main:center" class="img-box" v-if="i===0 && attrPic">
  73. <div class="attr-jt"></div>
  74. <div v-if="attr.pic_url" @mouseenter="attrPicEnter(j)">
  75. <el-image :src="attr.pic_url" style="height:88px;width:88px"></el-image>
  76. </div>
  77. <com-attachment v-else v-model="attr.pic_url" :multiple="false" :max="1">
  78. <el-tooltip class="item" effect="dark" content="建议尺寸:750 * 750" placement="top">
  79. <el-button type="text">+添加图片</el-button>
  80. </el-tooltip>
  81. </com-attachment>
  82. <i v-if="attrPicStatus === j && attr.pic_url" class="el-icon-error close"
  83. @click="attr.pic_url = ''"></i>
  84. <div v-if="attrPicStatus === j && attr.pic_url" style="position: absolute">
  85. <com-attachment v-model="attr.pic_url" :multiple="false" :max="1">
  86. <el-button type="primary">替换</el-button>
  87. </com-attachment>
  88. </div>
  89. </div>
  90. </div>
  91. <div slot="footer">
  92. <el-button type="text" @click="addAttr(i)">添加规格值</el-button>
  93. </div>
  94. </draggable>
  95. </div>
  96. <div v-if="i === 0" style="margin-left: 65px;color:#c9c9c9">
  97. <p class="tips-text">
  98. 仅支持为第一组规格设置规格图片,买家选择不同规格会看到对应规格图片,建议尺寸:80 * 80像素
  99. </p>
  100. </div>
  101. </div>
  102. <div v-if="isAddAttrGroups" flex="dir:left cross:center" class="bg">
  103. <el-button @click="addAttrGroup">添加规格项目</el-button>
  104. <span style="padding-left: 14px;color:#c9c9c9">注:规格名最多添加5个</span>
  105. </div>
  106. </div>
  107. </template>
  108. <script>
  109. Vue.component('com-attr-group', {
  110. template: '#com-attr-group',
  111. props: {
  112. value: {
  113. type: Array,
  114. default: function () {
  115. return [];
  116. }
  117. }
  118. },
  119. data() {
  120. return {
  121. attrGroupMaxCount: 5, //可添加最大规格组
  122. isAddAttrGroups: true, //添加规格组按钮是否显示
  123. attrPic: true, //规格图片
  124. attrPicStatus: -1, //move
  125. };
  126. },
  127. watch: {
  128. 'attrPic'(newVal, oldVal) {
  129. if (!newVal) {
  130. this.value[0]['attr_list'].forEach((v) => {
  131. v.pic_url = '';
  132. })
  133. }
  134. },
  135. },
  136. methods: {
  137. makeAttrGroup() {
  138. this.$emit('select', this.value);
  139. },
  140. attrPicEnter(value) {
  141. if (this.attrPicStatus == value) {
  142. return;
  143. }
  144. this.attrPicStatus = value;
  145. },
  146. // 添加规格组
  147. addAttrGroup() {
  148. let attrGroundId = 0;
  149. if(this.value.length>0){
  150. attrGroundId = this.value[this.value.length - 1].attr_group_id;
  151. }
  152. this.value.push({
  153. attr_group_id: attrGroundId + 1,
  154. attr_group_name: '',
  155. attr_list: [],
  156. });
  157. // 限制添加的规格组
  158. if (this.value.length === this.attrGroupMaxCount) {
  159. this.isAddAttrGroups = false;
  160. }
  161. },
  162. // 删除规格组
  163. deleteAttrGroup(index) {
  164. this.value.splice(index, 1);
  165. if (this.value.length < this.attrGroupMaxCount) {
  166. this.isAddAttrGroups = true;
  167. }
  168. this.makeAttrGroup();
  169. },
  170. // 删除规格
  171. deleteAttr(i, j) {
  172. this.value[i].attr_list.splice(j, 1);
  173. this.makeAttrGroup();
  174. },
  175. // 添加规格
  176. addAttr(i) {
  177. this.value[i].attr_list.push({
  178. attr_id: this.getLastAttrId() + 1,
  179. attr_name: '',
  180. pic_url: '',
  181. });
  182. this.makeAttrGroup();
  183. },
  184. // 获取最后一个规格ID
  185. getLastAttrId() {
  186. let id = 1;
  187. this.value.forEach((attrGroupItem, attrGroupIndex) => {
  188. attrGroupItem.attr_list.forEach((v) => {
  189. id = (v.attr_id > id?v.attr_id:id);
  190. // console.info(v.attr_id,"id",id);
  191. });
  192. });
  193. return id;
  194. },
  195. }
  196. });
  197. </script>