com-select-cat.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <style>
  2. .com-goods-cat-list {
  3. border: 1px solid #E8EAEE;
  4. border-radius: 5px;
  5. margin-top: -5px;
  6. padding: 10px;
  7. }
  8. </style>
  9. <template id="com-select-cat">
  10. <el-dialog title="选择分类" width="1100px" :visible.sync="dialogVisible">
  11. <el-row v-loading="dialogLoading" :gutter="20" style="margin-top: -30px;">
  12. <template v-if="options.length > 0">
  13. <el-col :span="8">
  14. <h3>一级分类</h3>
  15. <div class="com-goods-cat-list">
  16. <el-checkbox-group v-model="cat_list"
  17. size="mini" flex="dir:left" style="flex-wrap: wrap">
  18. <el-checkbox v-for="(option,index) in options" style="width: 100%;"
  19. @change="selectCats(option,1)" size="mini"
  20. :key="option.value" :label="option.value">
  21. {{option.label}}
  22. </el-checkbox>
  23. </el-checkbox-group>
  24. </div>
  25. </el-col>
  26. <el-col :span="8" v-if="children.length > 0">
  27. <h3>二级分类</h3>
  28. <div class="com-goods-cat-list">
  29. <el-checkbox-group v-model="cat_list"
  30. size="mini" flex="dir:left" style="flex-wrap: wrap">
  31. <el-checkbox v-for="(option,index) in children" style="width: 100%;"
  32. @change="selectCats(option,2)"
  33. :key="option.value" :label="option.value" size="mini">
  34. {{option.label}}
  35. </el-checkbox>
  36. </el-checkbox-group>
  37. </div>
  38. </el-col>
  39. <el-col :span="8" v-if="third.length > 0">
  40. <h3>三级分类</h3>
  41. <div class="com-goods-cat-list">
  42. <el-checkbox-group v-model="cat_list"
  43. size="mini" flex="dir:left" style="flex-wrap: wrap">
  44. <el-checkbox v-for="(option,index) in third" style="width: 100%;"
  45. @change="selectCats(option,3)"
  46. :key="option.value" :label="option.value" size="mini">
  47. {{option.label}}
  48. </el-checkbox>
  49. </el-checkbox-group>
  50. </div>
  51. </el-col>
  52. </template>
  53. <template v-else>
  54. <div flex="main:center" style="align-items: center;margin-top: 20px;">
  55. <span>无系统分类</span>
  56. <el-button style="display: inline-block;margin-left: 10px" flex="main:center" type="primary"
  57. size="small"
  58. @click="$navigate({r: 'mall/cate/edit'})">
  59. 请先添加商品分类
  60. </el-button>
  61. </div>
  62. </template>
  63. </el-row>
  64. <span slot="footer" class="dialog-footer">
  65. <el-button size='small' @click="catDialogCancel">取 消</el-button>
  66. <el-button size='small' type="primary" @click="beSelectCats">确 定</el-button>
  67. </span>
  68. </el-dialog>
  69. </template>
  70. <script>
  71. Vue.component('com-select-cat', {
  72. template: '#com-select-cat',
  73. props: {
  74. show: Boolean,
  75. value: Array,
  76. },
  77. data() {
  78. return {
  79. dialogLoading: false,
  80. dialogVisible: false,
  81. options: [],
  82. children: [],
  83. third: [],
  84. data: [],
  85. cat_list: [],
  86. };
  87. },
  88. created() {
  89. this.cat_list = [];
  90. this.value.forEach(item => {
  91. this.cat_list.push(item.value);
  92. });
  93. this.data = JSON.parse(JSON.stringify(this.value));
  94. },
  95. watch: {
  96. show: {
  97. handler(v) {
  98. this.dialogVisible = this.show;
  99. if (v) {
  100. this.getCats();
  101. }
  102. },
  103. immediate: true,
  104. },
  105. value: {
  106. deep: true,
  107. handler(newVal, oldVal) {
  108. this.cat_list = [];
  109. this.value.forEach(item => {
  110. this.cat_list.push(item.value);
  111. });
  112. this.data = JSON.parse(JSON.stringify(this.value));
  113. },
  114. },
  115. dialogVisible: {
  116. handler(v) {
  117. if (!v) {
  118. this.catDialogCancel();
  119. }
  120. }
  121. }
  122. },
  123. methods: {
  124. getCats() {
  125. let self = this;
  126. self.dialogLoading = true;
  127. request({
  128. params: {
  129. r: 'mall/cate/tree',
  130. mch_id: 0
  131. },
  132. method: 'get',
  133. data: {}
  134. }).then(e => {
  135. self.dialogLoading = false;
  136. if (e.data.code == 0) {
  137. self.options = e.data.data.list;
  138. } else {
  139. self.$message.error(e.data.msg);
  140. }
  141. }).catch(e => {
  142. console.log(e);
  143. });
  144. },
  145. selectCats(option, type) {
  146. let flag = true;
  147. let list = JSON.parse(JSON.stringify(this.data));
  148. list.forEach(item => {
  149. if (item.value == option.value) {
  150. flag = false;
  151. }
  152. });
  153. if (flag) {
  154. list.push({
  155. label: option.label,
  156. value: option.value
  157. });
  158. }
  159. let newList = [];
  160. this.cat_list.forEach(item => {
  161. list.forEach(data => {
  162. if (data.value == item) {
  163. newList.push(data);
  164. }
  165. })
  166. });
  167. this.data = newList;
  168. if (type == 1) {
  169. if (typeof option.children !== 'undefined') {
  170. this.children = option.children;
  171. } else {
  172. this.children = [];
  173. }
  174. }
  175. if (type == 2) {
  176. if (typeof option.children !== 'undefined') {
  177. this.third = option.children;
  178. } else {
  179. this.third = [];
  180. }
  181. }
  182. },
  183. beSelectCats() {
  184. this.$emit('input', this.data);
  185. this.catDialogCancel();
  186. },
  187. catDialogCancel() {
  188. this.dialogVisible = false;
  189. this.$emit('cancel');
  190. }
  191. }
  192. });
  193. </script>