com-add-cat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <style>
  2. .com-add-cat .com-goods-cat-list {
  3. border: 1px solid #E8EAEE;
  4. border-radius: 5px;
  5. margin-top: -5px;
  6. padding: 10px 0;
  7. overflow: scroll;
  8. height: 400px;
  9. }
  10. .com-add-cat .com-goods-cat-list .cat-item {
  11. cursor: pointer;
  12. padding: 5px 10px;
  13. }
  14. .com-add-cat .com-goods-cat-list .active {
  15. background: #FAFAFA;
  16. }
  17. .com-add-cat .el-checkbox {
  18. margin-right: 0;
  19. }
  20. .com-add-cat .tag-box {
  21. margin: 20px 0;
  22. }
  23. .com-add-cat .tag-box .tag-item {
  24. margin-right: 5px;
  25. }
  26. </style>
  27. <template id="com-add-cat">
  28. <div class="com-add-cat">
  29. <el-dialog :title="title" width="1100px" :visible.sync="dialogVisible" :show-close="false">
  30. <el-row v-loading="dialogLoading" :gutter="20" style="margin-top: -30px;">
  31. <template v-if="options.length > 0">
  32. <el-col :span="8">
  33. <h3>一级分类</h3>
  34. <div class="com-goods-cat-list active">
  35. <div :class="{'active': current1 == option.id ? true : false}" class="cat-item"
  36. flex="dir:left box:first"
  37. v-for="(option,index) in options"
  38. :key="option.id">
  39. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  40. :label="option.id" size="mini">
  41. <span style="display: none;">{{option.name}}</span>
  42. </el-checkbox>
  43. <div @click="selectCats(option,1)" flex="box:last cross:center">
  44. <span>{{option.name}}</span>
  45. <i v-if="option.children" class="el-icon-arrow-right"></i>
  46. </div>
  47. </div>
  48. </div>
  49. </el-col>
  50. <el-col :span="8" v-if="children.length > 0">
  51. <h3>二级分类</h3>
  52. <div class="com-goods-cat-list">
  53. <div :class="{'active': current2 == option.id ? true : false}" class="cat-item"
  54. flex="dir:left box:first"
  55. v-for="(option,index) in children"
  56. :key="option.id">
  57. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  58. :label="option.id" size="mini">
  59. <span style="display: none;">{{option.name}}</span>
  60. </el-checkbox>
  61. <div @click="selectCats(option,2)" flex="box:last cross:center">
  62. <span>{{option.name}}</span>
  63. <i v-if="option.children" class="el-icon-arrow-right"></i>
  64. </div>
  65. </div>
  66. </div>
  67. </el-col>
  68. <el-col :span="8" v-if="third.length > 0">
  69. <h3>三级分类</h3>
  70. <div class="com-goods-cat-list">
  71. <div :class="{'active': current3 == option.id ? true : false}" class="cat-item"
  72. flex="dir:left box:first"
  73. v-for="(option,index) in third"
  74. :key="option.id">
  75. <el-checkbox @change="optionClick(option)" v-model="option.checked"
  76. :label="option.id" size="mini">
  77. <span style="display: none;">{{option.name}}</span>
  78. </el-checkbox>
  79. <div @click="selectCats(option,3)" flex="box:last cross:center">
  80. <span>{{option.name}}</span>
  81. <i v-if="option.children" class="el-icon-arrow-right"></i>
  82. </div>
  83. </div>
  84. </div>
  85. </el-col>
  86. </template>
  87. <template v-else>
  88. <div flex="main:center" style="align-items: center;margin-top: 20px;">
  89. <span>无系统分类</span>
  90. <el-button style="display: inline-block;margin-left: 10px" flex="main:center" type="primary"
  91. size="small"
  92. @click="$navigate({r: 'purchase/cate/edit'})">
  93. 请先添加商品分类
  94. </el-button>
  95. </div>
  96. </template>
  97. </el-row>
  98. <div class="tag-box" v-if="cats.length > 0">
  99. <el-tag type="warning" class="tag-item" @close="deleteCat(item, index)" closable v-for="(item, index) in cats"
  100. :key="item.id">
  101. {{item.name}}
  102. </el-tag>
  103. </div>
  104. <span slot="footer" class="dialog-footer">
  105. <el-button size='small' @click="dialogVisible = false">取 消</el-button>
  106. <el-button size='small' type="primary" @click="dialogSubmit">确 定</el-button>
  107. </span>
  108. </el-dialog>
  109. </div>
  110. </template>
  111. <script>
  112. Vue.component('com-add-cat', {
  113. template: '#com-add-cat',
  114. props: {
  115. newCats: {
  116. type: Array,
  117. default: function () {
  118. return []
  119. }
  120. }
  121. },
  122. data() {
  123. return {
  124. dialogVisible: false,
  125. options: [],// 商品分类列表
  126. children: [],
  127. third: [],
  128. cats: [], //用于前端已选的分类展示
  129. dialogLoading: false,
  130. title: '选择分类',
  131. current1: '',
  132. current2: '',
  133. current3: '',
  134. }
  135. },
  136. methods: {
  137. openDialog() {
  138. let self = this;
  139. this.getCats();
  140. self.title = '选择分类';
  141. this.cats = [];
  142. this.children = [];
  143. this.third = [];
  144. this.dialogVisible = true;
  145. },
  146. // 获取商品分类
  147. getCats() {
  148. let self = this;
  149. self.dialogLoading = true;
  150. request({
  151. params: {
  152. r: 'purchase/cate/tree'
  153. },
  154. method: 'get',
  155. data: {}
  156. }).then(e => {
  157. self.dialogLoading = false;
  158. if (e.data.code === 0) {
  159. self.options = e.data.data.list;
  160. self.setCats();
  161. } else {
  162. self.$message.error(e.data.msg);
  163. }
  164. }).catch(e => {
  165. self.dialogLoading = false;
  166. });
  167. },
  168. setCats() {
  169. let self = this;
  170. self.options.forEach(function (item) {
  171. item.checked = false;
  172. self.newCats.forEach(function (cItem) {
  173. if (item.id === cItem) {
  174. item.checked = true;
  175. self.cats.push({
  176. name: item.name,
  177. id: item.id,
  178. })
  179. }
  180. });
  181. if (item.children) {
  182. item.children.forEach(function (item2) {
  183. item2.checked = false;
  184. self.newCats.forEach(function (cItem) {
  185. if (item2.id === cItem) {
  186. item2.checked = true;
  187. self.cats.push({
  188. name: item2.name,
  189. id: item2.id,
  190. })
  191. }
  192. });
  193. if (item2.children) {
  194. item2.children.forEach(function (item3) {
  195. item3.checked = false;
  196. self.newCats.forEach(function (cItem) {
  197. if (item3.id === cItem) {
  198. item3.checked = true;
  199. self.cats.push({
  200. name: item3.name,
  201. id: item3.id,
  202. })
  203. }
  204. });
  205. })
  206. }
  207. })
  208. }
  209. })
  210. },
  211. deleteCat(option, index) {
  212. let self = this;
  213. self.cats.splice(index, 1);
  214. self.options.forEach(function (item) {
  215. if (item.id === option.id) {
  216. item.checked = false;
  217. }
  218. if (item.children) {
  219. item.children.forEach(function (item2) {
  220. if (item2.id === option.id) {
  221. item2.checked = false
  222. }
  223. if (item2.children) {
  224. item2.children.forEach(function (item3) {
  225. if (item3.id === option.id) {
  226. item3.checked = false
  227. }
  228. })
  229. }
  230. })
  231. }
  232. })
  233. },
  234. optionClick(option) {
  235. let self = this;
  236. let sign = true;
  237. self.cats.forEach(function (item, index) {
  238. // 移除分类
  239. if (option.id === item.id) {
  240. sign = false;
  241. self.cats.splice(index, 1);
  242. }
  243. });
  244. // 新增分类
  245. if (sign && option.checked) {
  246. self.cats.push({
  247. name: option.name,
  248. id: option.id,
  249. })
  250. }
  251. },
  252. // 选择分类
  253. selectCats(option, type) {
  254. let self = this;
  255. if (type === 1) {
  256. self.current1 = option.id;
  257. self.children = [];
  258. if (option.children) {
  259. option.children.forEach(function (item) {
  260. self.children.push(item);
  261. })
  262. }
  263. }
  264. if (type === 2) {
  265. self.current2 = option.id;
  266. self.third = [];
  267. if (option.children) {
  268. option.children.forEach(function (item) {
  269. self.third.push(item);
  270. })
  271. }
  272. }
  273. if (type === 3) {
  274. self.current3 = option.id;
  275. }
  276. },
  277. dialogSubmit() {
  278. this.dialogVisible = false;
  279. this.$emit('select', this.cats)
  280. }
  281. }
  282. })
  283. </script>