詳解vue如何給特殊字段設(shè)置插槽
大綱:
<template>
<div>
<div>
<span>卡號(hào)</span>
<el-input type="text" v-model="cardNo" clearable placeholder="請(qǐng)輸入卡號(hào)" />
<el-button type="primary" plain icon="el-icon-search" @click="search">搜索</el-button>
</div>
<!-- 表格 -->
<el-table class="fontTextStyle" border :header-cell-style="{background:'#f0f9eb'}" :data="data" style="width: 100%">
<el-table-column align="center" prop="staffName" label="姓名"></el-table-column>
<el-table-column align="center" prop="deptName" label="部門"></el-table-column>
<el-table-column align="center" prop="cardNo" label="卡號(hào)" :show-overflow-tooltip="true"></el-table-column>
<el-table-column align="center" prop="termNo" label="設(shè)備號(hào)"></el-table-column>
<el-table-column align="center" prop="datails" label="菜品">
<template slot-scope="scope">
<el-popover placement="top-start" trigger="click">
<el-tag size="small" style="margin-right: 10px;" v-for="item in scope.row.details" :key="item.pid">
{{ item.name }} X {{ item.qty }}份
</el-tag>
<el-button type="text" slot="reference"> {{scope.row.details.length}}個(gè)菜品</el-button>
</el-popover>
</template>
</el-table-column>
<el-table-column align="center" prop="amt" label="消費(fèi)金額"></el-table-column>
<el-table-column align="center" prop="balance" label="余額"></el-table-column>
<el-table-column align="center" prop="payTime" label="支付時(shí)間" :show-overflow-tooltip="true"></el-table-column>
</el-table>
</div>
</template>
<script>
import API from '@/components/common/Api'
export default {
data() {
return {
//查詢參數(shù)
pn: 1,
ps: 10,
count: 0,
data: [],
form: {},
cardNo: '',//搜索鍵值
datails: '',
}
},
mounted() {
this.getList()
},
methods: {
search() {
this.getList();
},
//獲取智盤支付記錄列表
getList() {
let params = {
cardNo: this.cardNo,
}
API.getPosPayLog(params).then(result => {
if (result.data && result.data.code == 0) {
this.data = result.data.data.list || [];
//循環(huán)遍歷菜品
this.data.forEach(item => {
item.details = JSON.parse(item.details)
})
this.count = result.data.data.total
}
});
},
},
}
</script>
<style></style>代碼詳解:
代碼中使用了 Element UI 的組件,包括 el-input (輸入框)、 el-button (按鈕)、 el-table (表格)、 el-table-column (表格列)和 el-popover (氣泡彈出框)等等。
第一步: 在頁面初始化時(shí),通過 mounted 鉤子函數(shù)調(diào)用 getList 方法獲取支付記錄列表,并將結(jié)果保存在 data 數(shù)組中。當(dāng)點(diǎn)擊搜索按鈕時(shí),會(huì)觸發(fā) search 方法,該方法再次調(diào)用 getList 方法進(jìn)行搜索。
第二步: getList 方法發(fā)送請(qǐng)求到后端接口,傳遞卡號(hào)作為參數(shù)。當(dāng)接口返回?cái)?shù)據(jù)后,將數(shù)據(jù)賦值給 data 數(shù)組,并使用 JSON.parse 將菜品詳情從字符串解析為對(duì)象數(shù)組。
第三步:通過循環(huán)遍歷 data 數(shù)組中的每一項(xiàng),將菜品詳情 item.details 解析為對(duì)象數(shù)組,以便在表格中展示。
注意:具體的接口返回的數(shù)據(jù)類型可能會(huì)有所不同,可以根據(jù)實(shí)際需求進(jìn)行調(diào)整。
類型:字符串json


<template>
<div>
<avue-crud ref="crud" v-model="form" :data="data" :option="option">
<!-- 消費(fèi)商品 -->
<template slot-scope="scope" slot="payOrder">
<el-popover trigger="click" title="" v-if="scope.row.payOrder.length">
<div>
<el-tag size="small" v-for="(item,index) in viewList" :key="index">
{{ item.name }}X{{ item.num }}份
</el-tag>
</div>
<el-button slot="reference" type="text" size="small" @click="getmenu(scope.row.payOrder)">
{{ scope.row.payOrder.length }}個(gè)菜品
</el-button>
</el-popover>
<div v-else>無</div>
</template>
</avue-crud>
</div>
</template>
<script>
import API from '@/components/common/Api';
export default {
data() {
return {
/* data:[]后端獲取到的數(shù)據(jù)列表。option表格配置項(xiàng).form 表單*/
data: [],
form: {},
query: {}, //搜索鍵值
viewList: [],
option: {
size: 'mini', //表格大小 medium/small/mini
border: true,
align: 'center',
viewBtn: false, //查看詳情按鈕
editBtn: false, //編輯修改按鈕
delBtn: false, //刪除按鈕
addBtn: false,
menu: false,
column: [{
label: '消費(fèi)商品', //表頭
prop: 'payOrder', //鍵值
slot: true,
}]
},
}
},
mounted() {
this.onLoad()
},
methods: {
// 根據(jù)接口獲取data數(shù)據(jù)
onLoad() {
let params = {
data: this.query,
}
API.yktPosPayLogAll(params).then(res => {
if (res.data.code == 0) {
this.data = res.data.data.list;
//循環(huán)遍歷商品
this.data.forEach(item => {
item.payOrder = JSON.parse(item.payOrder);
})
}
})
},
getmenu(arr) {
this.viewList = arr;
},
}
}
</script>
<style></style><template>
<avue-crud :option="option" :data="data" v-model="form" ref="crud">
<template slot-scope="scope" slot="posFoodReserveDetail">
<el-popover trigger="click" title="菜單">
<div>
<el-tag size="small" v-for="item in viewList" :key="item.foodId">
{{ item.foodName }}X{{ item.reserveNum }}份
</el-tag>
</div>
<el-button slot="reference" type="text" size="small" @click="getmenu(scope.row.posFoodReserveDetail)">
{{ scope.row.posFoodReserveDetail.length }}個(gè)菜品
</el-button>
</el-popover>
</template>
</avue-crud>
</template>
<script>
import API from '@/components/common/newApi';
export default {
data() {
return {
data: [],
form: {},
query: {},
option: {
border: true,
column: [{
label: '預(yù)定內(nèi)容',
prop: 'posFoodReserveDetail',
slot: true,
overHidden: true,
}]
},
viewList: []
};
},
mounted() {
this.onLoad();
},
methods: {
// 加載表格數(shù)據(jù)
onLoad() {
let param = {
data: this.query
};
API.selectMyReserve(param).then(res => {
if (res.data.code === 0) {
this.data = res.data.data.list;
}
});
},
getmenu(arr) {
this.viewList = arr;
},
}
};
</script>
<style></style>代碼詳解:
在這個(gè)插槽中,我使用了 el-popover 組件來顯示菜單信息,并通過點(diǎn)擊按鈕觸發(fā)了 getmenu 方法。根據(jù)我這邊的業(yè)務(wù)需求,我將在點(diǎn)擊按鈕時(shí)調(diào)用 getmenu 方法,并將傳入的 scope.row.posFoodReserveDetail 賦值給 viewList。
類型:數(shù)組

以上就是詳解vue如何給特殊字段設(shè)置插槽的詳細(xì)內(nèi)容,更多關(guān)于vue插槽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
深入探討Vue計(jì)算屬性與監(jiān)聽器的區(qū)別和用途
在Vue的開發(fā)中,計(jì)算屬性(Computed Properties)和監(jiān)聽器(Watchers)是兩種非常重要的概念,它們都用于響應(yīng)式地處理數(shù)據(jù)變化,本文將帶你深入了解計(jì)算屬性和監(jiān)聽器的區(qū)別,以及在何時(shí)使用它們,感興趣的朋友可以參考下2023-09-09
vue使用v-model進(jìn)行跨組件綁定的基本實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue使用v-model進(jìn)行跨組件綁定的基本實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Nuxt.js nuxt-link與router-link的區(qū)別說明
這篇文章主要介紹了Nuxt.js nuxt-link與router-link的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
關(guān)于vue-lunar-full-calendar的使用說明
這篇文章主要介紹了關(guān)于vue-lunar-full-calendar的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

