Browse Source

商品默认加选中

caiyuqin 1 month ago
parent
commit
571d9b6c49
1 changed files with 28 additions and 1 deletions
  1. 28 1
      pages/product/creatProduct.vue

+ 28 - 1
pages/product/creatProduct.vue

@@ -662,6 +662,10 @@
 			selectProductType() {
 				if (this.tabIndex == '0' || this.form.isUpdatePrice == 1) {
 					this.isSelectType = true
+					// 编辑模式下已有分类数据时,保留默认选中
+					if (this.productId && this.form.categoryId) {
+						return
+					}
 					this.showText = ''
 					this.productType = []
 					this.selectTypeList = []
@@ -691,7 +695,6 @@
 			},
 			//选中二级
 			selectRight(item, index) {
-				this.getRightTree(this.treeCenter[0].id)
 				this.currentCenter = index
 				this.currentRight = null
 				if (this.selectTypeList.length >= 2) {
@@ -832,6 +835,22 @@
 
 				}
 			},
+			// 根据接口返回的分类ID,级联加载并选中树节点
+			async initDefaultCategory() {
+				if (!this.productType.length) return;
+				// 一级
+				this.currentLeft = this.treeLeft.findIndex(item => item.id === this.productType[0]);
+				if (this.currentLeft === -1 || this.productType.length < 2) return;
+				// 二级
+				const res1 = await this.$http.get("/sys/category/childList?pid=" + this.productType[0]);
+				this.treeCenter = res1.data.result || [];
+				this.currentCenter = this.treeCenter.findIndex(item => item.id === this.productType[1]);
+				if (this.currentCenter === -1 || this.productType.length < 3) return;
+				// 三级
+				const res2 = await this.$http.get("/sys/category/childList?pid=" + this.productType[1]);
+				this.treeRight = res2.data.result || [];
+				this.currentRight = this.treeRight.findIndex(item => item.id === this.productType[2]);
+			},
 			//回显数据
 			getForm() {
 				this.$http.get("/app/product/queryById?id=" + this.productId, {}).then(res => {
@@ -841,6 +860,14 @@
 						this.jifenChecked = res.data.result.integralAward ? true : false
 						this.jindouChecked = res.data.result.goldAward ? true : false
 						this.showText = res.data.result.productCategory
+						// 设置分类树的默认选中
+						if (res.data.result.categoryId) {
+							this.productType = res.data.result.categoryId.split(',').filter(id => id)
+						}
+						if (res.data.result.productCategory) {
+							this.selectTypeList = res.data.result.productCategory.split('·').filter(name => name)
+						}
+						this.initDefaultCategory()
 						this.fmImageList.push({
 							url: res.data.result.productImage
 						})