Vue參數(shù)的增刪改實(shí)例詳解
展示參數(shù)明細(xì)

elementui Tag標(biāo)簽

<el-table-column label="明細(xì)" type="expand">
<template slot-scope="scope">
<!-- 循環(huán)渲染tag組件 參數(shù)明細(xì) -->
<el-tag :key="i" v-for="(item,i) in scope.row.attr_vals" closable @close="handleClose(scope.row,i)">
{{ item }}
</el-tag>
<!-- 輸入的文本框 -->
<el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
</el-input>
<!-- 添加的按鈕i -->
<el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
</template>
// 獲取分類參數(shù)的數(shù)據(jù)
async getParamsData() {
// 判斷是否選中三級(jí)分類,如果未選中則重新選中
if (this.selectdKeys.length !== 3) {
this.selectdKeys = []
this.paramsData = []
return
}
// 根據(jù)所選分類獲取動(dòng)態(tài)參數(shù)或者靜態(tài)屬性 三級(jí)分類的id
const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
params: {
sel: this.activeName,
},
})
if (res.meta.status !== 200) {
return this.$message.error('獲取參數(shù)列表失敗')
}
//對(duì)參數(shù)的明細(xì)進(jìn)行處理:按空格拆分為數(shù)組
res.data.forEach(item=>{
item.attr_vals = item.attr_vals ? item.attr_vals.split(',') : []
})
console.log(res.data)
this.paramsData = res.data
},

展示參數(shù)明細(xì)功能
inputVisible 控制輸入框的顯示
showInput
// tag 標(biāo)簽 顯示文本輸入框
inputVisible:false,
<!-- 輸入的文本框 -->
<el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
</el-input>
<!-- 添加的按鈕i -->
<el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
// tag 顯示文本輸入框
showInput(){
this.inputVisible=true
}


輸入框太寬,修改下長(zhǎng)度
.input-new-tag{
width: 120px;
}
但是現(xiàn)在會(huì)存在一個(gè)問(wèn)題,顏色和尺寸屬性應(yīng)該是獨(dú)立的互不產(chǎn)生影響才對(duì),但是這里產(chǎn)生了關(guān)聯(lián)(點(diǎn)擊按鈕顏色和尺寸都會(huì)出現(xiàn)輸入框,如果輸入內(nèi)存也都會(huì)同時(shí)輸入)

直接在data里面定義下面的屬性是不行的
// tag 標(biāo)簽 顯示文本輸入框
inputVisible:false,
解決方法:可以在后端返回的參數(shù)數(shù)據(jù)里面加上該屬性,用以控制文本框的顯示和隱藏
//對(duì)參數(shù)的明細(xì)進(jìn)行處理:按空格拆分為數(shù)組
res.data.forEach(item=>{
item.attr_vals = item.attr_vals ? item.attr_vals.split(',') : []
item.inputVisible=false //控制文本框的顯示和隱藏
})
這里的v-if 相當(dāng)于從該行數(shù)據(jù)中獲取該值用以控制
<!-- 輸入的文本框 --> <el-input class="input-new-tag" v-if="scope.row.inputVisible" v-model="scope.row.inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
這是文本框輸入的值
v-model="scope.row.inputValue"
//對(duì)參數(shù)的明細(xì)進(jìn)行處理:按空格拆分為數(shù)組
res.data.forEach(item=>{
item.attr_vals = item.attr_vals ? item.attr_vals.split(',') : []
item.inputVisible=false //控制文本框的顯示和隱藏
item.inputValue='' // 文本框輸入的值
})
這兩步的目的就是讓添加參數(shù)的按鈕輸入框互不產(chǎn)生影響
showInput(scope.row) 這里需要拿到參數(shù)對(duì)象
<!-- 添加的按鈕i --> <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>

所以獲取到用戶輸入的參數(shù)后先添加到參數(shù)明細(xì)attr_vals中
row.attr_vals.push(row.inputValue.trim())
// tag 顯示文本輸入框
showInput(row){
row.inputVisible=true
this.$nextTick(_ => {
// 點(diǎn)擊新增按鈕后 輸入框獲取焦點(diǎn) ref = saveTagInput
this.$refs.saveTagInput.$refs.input.focus();
});
},
// 文本框失去焦點(diǎn)或按下enter 按鍵
handleInputConfirm(row){
if(row.inputValue.trim()){
row.attr_vals.push(row.inputValue.trim())
// 更新參數(shù)的明細(xì)
this.updateParamsDetail(row)
}
row.inputVisible=false
},
// 更新參數(shù)的明細(xì)
async updateParamsDetail(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('更新參數(shù)明細(xì)失敗')
}
this.$message.success('更新參數(shù)明細(xì)成功!')
}
這里失去焦點(diǎn)和執(zhí)行enter會(huì)觸發(fā)兩次事件,執(zhí)行兩次
row.inputValue=''
// 文本框失去焦點(diǎn)或按下enter 按鍵
handleInputConfirm(row){
if(row.inputValue.trim()){
row.attr_vals.push(row.inputValue.trim())
// 更新參數(shù)的明細(xì)
this.updateParamsDetail(row)
}
row.inputVisible=false
// 執(zhí)行完一次(enter 或者失去焦點(diǎn)) 清空,這樣就不會(huì)執(zhí)行上面的if
row.inputValue=''
},
實(shí)現(xiàn)刪除參數(shù)明細(xì)功能
// 監(jiān)視tag標(biāo)簽的關(guān)閉事件,即刪除對(duì)應(yīng)的參數(shù)明細(xì)項(xiàng)
handleClose(){
row.attr_vals.splice(i,1)
// 更新參數(shù)的明細(xì)
this.updateParamsDetail(row)
},
實(shí)現(xiàn)靜態(tài)屬性的更新
<el-table-column label="明細(xì)" type="expand">
<template slot-scope="scope">
<!-- 循環(huán)渲染tag組件 參數(shù)明細(xì) -->
<el-tag :key="i" v-for="(item,i) in scope.row.attr_vals" closable @close="handleClose(scope.row,i)">
{{ item }}
</el-tag>
<!-- 輸入的文本框 -->
<el-input class="input-new-tag" v-if="scope.row.inputVisible" v-model="inputValue" ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(scope.row)" @blur="handleInputConfirm(scope.row)">
</el-input>
<!-- 添加的按鈕i -->
<el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
</template>
</el-table-column>
只需要把動(dòng)態(tài)屬性的明細(xì) 復(fù)制到靜態(tài)屬性即可
完整代碼
<template>
<div>
<!-- 面包屑導(dǎo)航-->
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/home' }">首頁(yè)</el-breadcrumb-item>
<el-breadcrumb-item>商品管理</el-breadcrumb-item>
<el-breadcrumb-item>分類參數(shù)</el-breadcrumb-item>
</el-breadcrumb>
<!-- 卡片試圖-->
<el-card>
<!-- 提示信息-->
<el-alert title="注意:只允許為第三級(jí)分類設(shè)置相關(guān)參數(shù)!" type="warning" show-icon :closable="false"> </el-alert>
<!-- 選擇商品分類-->
<el-row class="cat_select">
<el-col>
<span>選擇商品分類:</span>
<el-cascader v-model="selectdKeys" :options="cateList" :props="cascaderProps" @change="handleChange" clearable></el-cascader>
</el-col>
</el-row>
<!-- tabs標(biāo)簽頁(yè)-->
<el-tabs v-model="activeName" @tab-click="handleClick">
<el-tab-pane label="動(dòng)態(tài)參數(shù)" name="many">
<el-button type="primary" size="mini" :disabled="btnDisabled" @click="addDialogVisible = true">添加參數(shù)</el-button>
<el-table :data="paramsData" border stripe>
<el-table-column label="明細(xì)" type="expand">
<template slot-scope="scope">
<!-- 循環(huán)渲染tag組件 參數(shù)明細(xì) -->
<el-tag :key="i" v-for="(item, i) in scope.row.attr_vals" closable @close="handleClose(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>
<!-- 添加的按鈕i -->
<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 label="序號(hào)" type="index"></el-table-column>
<el-table-column label="參數(shù)名稱" prop="attr_name"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.attr_id)">編輯</el-button>
<el-button type="warning" icon="el-icon-delete" size="mini" @click="removeParams(scope.row.attr_id)">刪除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
<el-tab-pane label="靜態(tài)屬性" name="only">
<el-button type="primary" size="mini" :disabled="btnDisabled" @click="addDialogVisible = true">添加屬性</el-button>
<el-table :data="paramsData" border stripe>
<el-table-column label="明細(xì)" type="expand">
<template slot-scope="scope">
<!-- 循環(huán)渲染tag組件 參數(shù)明細(xì) -->
<el-tag :key="i" v-for="(item, i) in scope.row.attr_vals" closable @close="handleClose(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>
<!-- 添加的按鈕i -->
<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 label="序號(hào)" type="index"></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" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.attr_id)">編輯</el-button>
<el-button type="warning" icon="el-icon-delete" size="mini" @click="removeParams(scope.row.attr_id)">刪除</el-button>
</template>
</el-table-column>
</el-table>
</el-tab-pane>
</el-tabs>
</el-card>
<!-- 添加對(duì)話框-->
<el-dialog :title="'添加' + title" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
<el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
<el-form-item :label="title" prop="attr_name">
<el-input v-model="addForm.attr_name"></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="addParams()">確 定</el-button>
</span>
</el-dialog>
<!-- 修改對(duì)話框-->
<el-dialog :title="'修改' + title" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
<el-form-item :label="title" prop="attr_name">
<el-input v-model="editForm.attr_name"></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>
</template>
<script>
export default {
data() {
return {
// 分類列表
cateList: [],
// 級(jí)聯(lián)選擇器的屬性配置
cascaderProps: {
label: 'cat_name',
value: 'cat_id',
children: 'children',
expandTrigger: 'hover',
},
//級(jí)聯(lián)選擇器選中的id數(shù)組
selectdKeys: [],
// 激活第幾個(gè)標(biāo)簽頁(yè)
activeName: 'many',
// 獲取參數(shù)屬性數(shù)據(jù)
paramsData: [],
// 添加參數(shù)對(duì)話框
addDialogVisible: false,
addForm: {},
addFormRules: {
attr_name: [{ required: true, message: '請(qǐng)輸入?yún)?shù)名稱', trigger: 'blur' }],
},
// 編輯參數(shù)對(duì)話框
editDialogVisible: false,
editForm: {},
editFormRules: {
attr_name: [{ required: true, message: '請(qǐng)輸入?yún)?shù)名稱', trigger: 'blur' }],
},
}
},
created() {
this.getCateList()
},
methods: {
// 獲取所有分類列表數(shù)據(jù)(因?yàn)闆](méi)有傳遞具體參數(shù))
async getCateList() {
const { data: res } = await this.$http.get('categories')
if (res.meta.status !== 200) {
return this.$message.error('獲取分類失敗')
}
this.cateList = res.data
},
// 監(jiān)聽(tīng)級(jí)聯(lián)選擇器的改變事件
handleChange() {
this.getParamsData()
},
// 監(jiān)聽(tīng)標(biāo)簽頁(yè)的點(diǎn)擊事件
handleClick() {
this.getParamsData()
},
// 獲取分類參數(shù)的數(shù)據(jù)
async getParamsData() {
// 判斷是否選中三級(jí)分類,如果未選中則重新選中
if (this.selectdKeys.length !== 3) {
this.selectdKeys = []
this.paramsData = []
return
}
// 根據(jù)所選分類獲取動(dòng)態(tài)參數(shù)或者靜態(tài)屬性 三級(jí)分類的id
const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, {
params: {
sel: this.activeName,
},
})
if (res.meta.status !== 200) {
return this.$message.error('獲取參數(shù)列表失敗')
}
//對(duì)參數(shù)的明細(xì)進(jìn)行處理:按空格拆分為數(shù)組
res.data.forEach((item) => {
item.attr_vals = item.attr_vals ? item.attr_vals.split(',') : []
item.inputVisible = false //控制文本框的顯示和隱藏
})
console.log(res.data)
this.paramsData = res.data
},
// 監(jiān)聽(tīng)添加對(duì)話框的關(guān)閉事件
addDialogClosed() {
this.$refs.addFormRef.resetFields()
},
// 添加參數(shù)
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('添加參數(shù)失敗')
}
this.addDialogVisible = false
this.getParamsData()
this.$message.success('添加參數(shù)成功')
})
},
// 刪除參數(shù)
removeParams(id) {
this.$confirm('確定要?jiǎng)h除該參數(shù)嗎?', '提示', {
confirmButtonText: '確定',
cancelButtonText: '取消',
type: 'warning',
})
.then(async () => {
const { data: res } = await this.$http.delete(`categories/${this.cateId}/attributes/${id}`)
if (res.meta.status !== 200) {
return this.$message.error('刪除參數(shù)失敗')
}
this.getParamsData()
this.$message.success('刪除參數(shù)成功')
})
.catch(() => {
this.$message({
type: 'info',
message: '已取消刪除',
})
})
},
// 顯示編輯對(duì)話框
async showEditDialog(id) {
const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes/${id}`, {
params: {
attr_sel: this.activeName,
},
})
if (res.meta.status !== 200) {
return this.$message.error('查詢參數(shù)失敗')
}
this.editForm = res.data
this.editDialogVisible = true
},
// 監(jiān)聽(tīng)修改對(duì)話框的關(guān)閉事件
editDialogClosed() {
this.$refs.editFormRef.resetFields()
},
// 修改參數(shù)
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,
attr_vals: this.editForm.attr_vals,
})
if (res.meta.status !== 200) {
return this.$message.error('修改參數(shù)名稱失敗')
}
this.editDialogVisible = false
this.getParamsData()
this.$message.success('修改參數(shù)名稱成功!')
})
},
// 監(jiān)視tag標(biāo)簽的關(guān)閉事件,即刪除對(duì)應(yīng)的參數(shù)明細(xì)項(xiàng)
handleClose() {
row.attr_vals.splice(i, 1)
// 更新參數(shù)的明細(xì)
this.updateParamsDetail(row)
},
// tag 顯示文本輸入框
showInput(row) {
row.inputVisible = true
this.$nextTick((_) => {
// 點(diǎn)擊新增按鈕后 輸入框獲取焦點(diǎn) ref = saveTagInput
this.$refs.saveTagInput.$refs.input.focus()
})
},
// 文本框失去焦點(diǎn)或按下enter 按鍵
handleInputConfirm(row) {
if (row.inputValue.trim()) {
row.attr_vals.push(row.inputValue.trim())
// 更新參數(shù)的明細(xì)
this.updateParamsDetail(row)
}
row.inputVisible = false
// 執(zhí)行完一次(enter 或者失去焦點(diǎn)) 清空,這樣就不會(huì)執(zhí)行上面的if
row.inputValue = ''
},
// 更新參數(shù)的明細(xì)
async updateParamsDetail(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('更新參數(shù)明細(xì)失敗')
}
this.$message.success('更新參數(shù)明細(xì)成功!')
},
},
computed: {
// 當(dāng)前選中的三級(jí)分類的id
cateId() {
return this.selectdKeys.length === 3 ? this.selectdKeys[2] : null
},
// 是否禁用按鈕
btnDisabled() {
return this.selectdKeys.length === 3 ? false : true
},
// 添加對(duì)話框標(biāo)題
title() {
return this.activeName === 'many' ? '動(dòng)態(tài)參數(shù)' : '靜態(tài)屬性'
},
},
}
</script>
<style lang="less" scoped>
.cat_select {
margin: 15px 0;
}
.el-tag {
margin: 10px;
}
.input-new-tag {
width: 120px;
}
</style>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- 從vue基礎(chǔ)開(kāi)始創(chuàng)建一個(gè)簡(jiǎn)單的增刪改查的實(shí)例代碼(推薦)
- vue實(shí)現(xiàn)表格增刪改查效果的實(shí)例代碼
- vue2 中如何實(shí)現(xiàn)動(dòng)態(tài)表單增刪改查實(shí)例
- Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(二)
- Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(一)
相關(guān)文章
詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
vue+iview+less 實(shí)現(xiàn)換膚功能
這篇文章主要介紹了vue+iview+less 實(shí)現(xiàn)換膚功能,項(xiàng)目搭建用的vue—cli,css框架選擇的iview,具體操作流程大家跟隨腳本之家小編一起看看吧2018-08-08
利用vueJs實(shí)現(xiàn)圖片輪播實(shí)例代碼
本篇文章主要介紹了利用vueJs實(shí)現(xiàn)圖片輪播實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
vue?Keep-alive組件緩存的簡(jiǎn)單使用代碼
keep-alive是Vue提供的一個(gè)抽象組件,用來(lái)對(duì)組件進(jìn)行緩存,從而節(jié)省性能,下面這篇文章主要給大家介紹了關(guān)于vue?Keep-alive組件緩存的簡(jiǎn)單使用,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
vuejs移動(dòng)端實(shí)現(xiàn)div拖拽移動(dòng)
這篇文章主要為大家詳細(xì)介紹了vuejs移動(dòng)端實(shí)現(xiàn)div拖拽移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Vue中computed、methods與watch的區(qū)別總結(jié)
這篇文章主要給大家介紹了關(guān)于Vue中computed、methods與watch區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue3.0 搭建項(xiàng)目總結(jié)(詳細(xì)步驟)
這篇文章主要介紹了vue3.0 搭建項(xiàng)目總結(jié)(詳細(xì)步驟),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

