Explorar el Código

添加参数标签

zhuyijun hace 2 años
padre
commit
f152a17279
Se han modificado 1 ficheros con 232 adiciones y 11 borrados
  1. 232 11
      src/components/goods/Params.vue

+ 232 - 11
src/components/goods/Params.vue

@@ -3,7 +3,7 @@
  * @Version: 1.0
  * @Autor: zhuyijun
  * @Date: 2021-11-25 22:52:20
- * @LastEditTime: 2021-11-26 23:57:15
+ * @LastEditTime: 2021-11-27 23:14:52
 -->
 <template>
   <div>
@@ -52,19 +52,47 @@
                     border
                     stripe>
             <!-- 展开行 -->
-            <el-table-column type="expand"></el-table-column>
+            <el-table-column type="expand">
+              <template slot-scope="scope">
+                <!-- 循环展示tag标签 -->
+                <el-tag v-for="(item,i) in scope.row.attr_vals"
+                        :key="i"
+                        closable
+                        @close="removeParamById(scope.row,i)">
+                  {{item}}
+                </el-tag>
+                <!-- 输入文本框 -->
+                <el-input class="input-new-tag"
+                          v-if="scope.row.inputVisible"
+                          v-model="scope.row.inputValue"
+                          ref="saveTagInput"
+                          size="small"
+                          @keyup.enter.native="handleInputConfirm(scope.row)"
+                          @blur="handleInputConfirm(scope.row)">
+                </el-input>
+                <!-- 添加按钮 -->
+                <el-button v-else
+                           class="button-new-tag"
+                           size="small"
+                           @click="showInput(scope.row)">+ New Tag</el-button>
+              </template>
+            </el-table-column>
             <!-- 索引列 -->
             <el-table-column type="index"></el-table-column>
-            <el-table-column label="参数名称"
+            <el-table-column label="属性编号"
+                             prop="attr_id"></el-table-column>
+            <el-table-column label="属性名称"
                              prop="attr_name"></el-table-column>
             <el-table-column label="操作">
               <template slot-scope="scope">
                 <el-button type="primary"
                            size="mini"
-                           icon="el-icon-edit">添加</el-button>
+                           icon="el-icon-edit"
+                           @click="showEditDialog(scope.row)">编辑</el-button>
                 <el-button type="primary"
                            size="mini"
-                           icon="el-icon-delete">删除</el-button>
+                           icon="el-icon-delete"
+                           @click="removeParamsByAttrId(scope.row.attr_id)">删除</el-button>
               </template>
             </el-table-column>
           </el-table>
@@ -82,19 +110,47 @@
                     border
                     stripe>
             <!-- 展开行 -->
-            <el-table-column type="expand"></el-table-column>
+            <el-table-column type="expand">
+              <template slot-scope="scope">
+                <!-- 循环展示tag标签 -->
+                <el-tag v-for="(item,i) in scope.row.attr_vals"
+                        :key="i"
+                        closable
+                        @close="removeParamById(scope.row,i)">
+                  {{item}}
+                </el-tag>
+                <!-- 输入文本框 -->
+                <el-input class="input-new-tag"
+                          v-if="scope.row.inputVisible"
+                          v-model="scope.row.inputValue"
+                          ref="saveTagInput"
+                          size="small"
+                          @keyup.enter.native="handleInputConfirm(scope.row)"
+                          @blur="handleInputConfirm(scope.row)">
+                </el-input>
+                <!-- 添加按钮 -->
+                <el-button v-else
+                           class="button-new-tag"
+                           size="small"
+                           @click="showInput(scope.row)">+ New Tag</el-button>
+              </template>
+            </el-table-column>
             <!-- 索引列 -->
             <el-table-column type="index"></el-table-column>
+            <el-table-column label="属性编号"
+                             prop="attr_id"></el-table-column>
             <el-table-column label="属性名称"
                              prop="attr_name"></el-table-column>
             <el-table-column label="操作">
               <template slot-scope="scope">
                 <el-button type="primary"
                            size="mini"
-                           icon="el-icon-edit">添加</el-button>
+                           icon="el-icon-edit"
+                           @click="showEditDialog(scope.row)">编辑</el-button>
                 <el-button type="primary"
                            size="mini"
-                           icon="el-icon-delete">删除</el-button>
+                           icon="el-icon-delete"
+                           @click="removeParamsByAttrId(scope.row.attr_id)">删除</el-button>
               </template>
             </el-table-column>
           </el-table>
@@ -118,12 +174,47 @@
                       prop="attr_name">
           <el-input v-model="addForm.attr_name"></el-input>
         </el-form-item>
+        <el-form-item :label="titleText+'的值'">
+          <el-input v-model="addForm.attr_vals"></el-input>
+        </el-form-item>
       </el-form>
       <span slot="footer"
             class="dialog-footer">
         <el-button @click="addDialogVisible = false">取 消</el-button>
         <el-button type="primary"
-                   @click="addDialogVisible = false">确 定</el-button>
+                   @click="addParams">确 定</el-button>
+      </span>
+    </el-dialog>
+
+    <!-- 修改参数 -->
+    <!-- 添加参数 -->
+    <el-dialog :title=" '修改' + titleText"
+               :visible.sync="editDialogVisible"
+               width="50%"
+               @close="editDialogClosed">
+      <!-- 添加参数对话框 -->
+      <el-form ref="editFormRef"
+               :model="editForm"
+               :rules="editFormRules"
+               label-width="100px">
+
+        <el-form-item :label="titleText">
+          <el-input v-model="editForm.attr_id"
+                    disabled></el-input>
+        </el-form-item>
+        <el-form-item :label="titleText"
+                      prop="attr_name">
+          <el-input v-model="editForm.attr_name"></el-input>
+        </el-form-item>
+        <el-form-item :label="titleText+'的值'">
+          <el-input v-model="editForm.attr_vals"></el-input>
+        </el-form-item>
+      </el-form>
+      <span slot="footer"
+            class="dialog-footer">
+        <el-button @click="editDialogVisible = false">取 消</el-button>
+        <el-button type="primary"
+                   @click="editParams">确 定</el-button>
       </span>
     </el-dialog>
   </div>
@@ -151,11 +242,27 @@ export default {
       //   添加对话框
       addDialogVisible: false,
       addForm: {
-        attr_name: ''
+        attr_name: '',
+        attr_vals: ''
       },
       addFormRules: {
         attr_name: [{ required: true, message: '请输入参数名称', trigger: 'blur' }]
-      }
+      },
+      editFormRules: {
+        attr_name: [{ required: true, message: '请输入参数名称', trigger: 'blur' }]
+      },
+      //   修改表单对象
+      editForm: {
+        attr_id: 0,
+        attr_name: '',
+        attr_sel: this.activeName,
+        attr_vals: '',
+        attr_write: '',
+        cat_id: 0,
+        delete_time: null
+      },
+      //   显示修改表单数据
+      editDialogVisible: false
     }
   },
   created () {
@@ -179,6 +286,8 @@ export default {
     async getParamsData () {
       if (this.selectedKey.length !== 3) {
         this.selectedKey = []
+        this.manyTableData = []
+        this.onlyTableData = []
         return this.$message.info('请选择三级分类')
       }
       //根据所选择分类的id ,当前所处的面板,获取对于的参数
@@ -190,6 +299,13 @@ export default {
       if (res.meta.status !== 200) {
         return this.$message.error(res.meta.msg)
       }
+      res.data.forEach(item => {
+        item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
+        // 文本框的显示与隐藏
+        item.inputVisible = false
+        // 文本框输入的值
+        item.inputValue = ''
+      })
       if (this.activeName === 'many') {
         this.manyTableData = res.data
       } else {
@@ -199,6 +315,103 @@ export default {
     // 添加对话框关闭事件
     addDialogClosed () {
       this.$refs.addFormRef.resetFields()
+    },
+    addParams () {
+      this.$refs.addFormRef.validate(async (valid) => {
+        if (!valid) return
+        const { data: res } = await this.$http.post(`categories/${this.cateId}/attributes`, {
+          attr_name: this.addForm.attr_name,
+          attr_sel: this.activeName
+        })
+        if (res.meta.status !== 201) {
+          return this.$message.error(res.data.msg)
+        }
+        this.addDialogVisible = false
+        this.$message.success(res.meta.msg)
+        this.getParamsData()
+      })
+    },
+    // 点击按钮修改参数对话框
+    showEditDialog (params) {
+      this.editDialogVisible = true
+      this.editForm = params
+    },
+    editDialogClosed () {
+      this.$refs.editFormRef.resetFields()
+    },
+    // 修改参数信息函数
+    editParams () {
+      this.$refs.editFormRef.validate(async (valid) => {
+        if (!valid) return
+        const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${this.editForm.attr_id}`, {
+          attr_name: this.editForm.attr_name,
+          attr_sel: this.activeName
+        })
+        if (res.meta.status !== 200) {
+          return this.$message.error(res.data.msg)
+        }
+        this.editDialogVisible = false
+        this.$message.success(res.meta.msg)
+        this.getParamsData()
+      })
+    },
+    // 根据attr_id删除对于参数
+    async removeParamsByAttrId (id) {
+      const confirmResule = await this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
+        confirmButtonText: '确定',
+        cancelButtonText: '取消',
+        type: 'warning'
+      }).catch(err => err)
+      if (confirmResule !== 'confirm') {
+        return this.$message.info('取消删除')
+      }
+      const { data: res } = await this.$http.delete(`categories/${this.cateId}/attributes/${id}`)
+      if (res.meta.status !== 200) {
+        return this.$message.error(res.data.msg)
+      }
+      this.$message.success(res.meta.msg)
+      this.getParamsData()
+    },
+    async removeParamById (row, index) {
+      // splice 删除 splice(索引,索引位置即之后要删除的数量)
+      row.attr_vals.splice(index, 1)
+      this.saveAttrVals(row)
+    },
+    async saveAttrVals (row) {
+      const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${row.attr_id}`, {
+        attr_name: row.attr_name,
+        attr_sel: row.attr_sel,
+        attr_vals: row.attr_vals.join(' ')
+      })
+      if (res.meta.status !== 200) {
+        return this.$message.error(res.data.msg)
+      }
+      this.$message.success(res.meta.msg)
+    },
+    // 文本框失去焦点或按下Enter键会触发
+    async handleInputConfirm (row) {
+      let inputValue = row.inputValue
+      if (!inputValue.trim()) {
+        row.inputVisible = false
+        row.inputValue = ''
+        return
+      }
+      //   如果没有return则输入内容需要左后序处理
+      row.inputVisible = false
+      row.inputValue = ''
+      row.attr_vals.push(inputValue.trim())
+      // 持久化 保存数据库
+      this.saveAttrVals(row)
+    },
+    // 点击按钮 展示文本输入框
+    showInput (row) {
+      row.inputVisible = true
+      //   让文本自动获得焦点
+      // $nextTick 方法的作用,就是当页面上的元素被重新渲染之后
+      // 才会指定回调函数中的代码
+      this.$nextTick(_ => {
+        this.$refs.saveTagInput.$refs.input.focus()
+      })
     }
   },
   computed: {
@@ -228,4 +441,12 @@ export default {
 .cat_opt {
   margin: 15px 0px;
 }
+.el-tag {
+  margin-left: 10px;
+  margin-right: 10px;
+  margin-top: 5px;
+}
+.input-new-tag {
+  width: 120px;
+}
 </style>