使用vNode實(shí)現(xiàn)給列表字段打標(biāo)簽
更新時(shí)間:2022年09月23日 15:21:19 作者:甜點(diǎn)cc
這篇文章主要為大家介紹了使用vNode實(shí)現(xiàn)給列表字段打標(biāo)簽示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
問題
如何給列表數(shù)據(jù)打標(biāo)簽?類似下面這種樣子??
思路 實(shí)現(xiàn)
- 數(shù)模轉(zhuǎn)換(對接口請求回來的數(shù)據(jù)進(jìn)行過濾標(biāo)記,返回新的數(shù)據(jù))
- 渲染新的數(shù)據(jù)模型
1、過濾數(shù)據(jù),需要打標(biāo)簽的采用jsx寫法
業(yè)務(wù)數(shù)據(jù)的處理我封裝在 mixins
里面
// 存放全局的mixin, 可拆分到模塊獨(dú)享 import { mapGetters } from 'vuex' import { fetchListData } from '@/api/global/api.js' export default { data() { return { p_category: [], listdata: [], p_total: 0, p_loading: false, } }, computed: { // ...mapGetters(['productLevel', 'productLevelInfo']), p_listdata() { const data = this.listdata; data.forEach((item) => { // ... // jsx 方式,打標(biāo)簽 if (item.status === 2 || item.status === 3) { item.status = <span style={{color: '#999'}}>停售</span> } else { item.status = item.status } if (item.age <= 25) { item.age = <span class="badge_info">{item.age}</span> } if (item.sex === 'Man') { item.sex = <span class="badge_default">{item.sex}</span> } }) return data; } }, methods: { async getProductList(params = {}) { try { this.p_loading = true this.listdata = [] const res = await fetchListData(params) if (res.code === 0) { const { data = [], total = 0 } = res || {} if (Array.isArray(data)) { this.listdata = [...data] this.p_total = total } else { this.listdata = [] this.p_total = 0 } } else { this.listdata = [] this.p_total = 0 this.$message.error(res.message || '出錯(cuò)了') } this.p_loading = false; } catch (err) { this.p_loading = false this.listdata = [] this.p_total = 0 console.log(err); } } } }
base.less 定義標(biāo)簽樣式
.badge_info { color: #4760f0; background: #1C84C6; padding: 5px 8px; color: #fff; border-radius: 5px; } .badge_default { color: #4760f0; background: #4760f0; padding: 5px 8px; color: #fff; border-radius: 5px; }
2、封裝列表渲染組件
<template> <ul class="listV2"> <li class="listV2_row-title"> <span v-for="(col, index) in fieldList" :key="index" class="listV2_cell ellipsis" :name="col.fieldName"> {{col.fieldLabel}} </span> </li> <!-- 行 --> <div v-if="tableData.length === 0" class="nodata">暫無數(shù)據(jù)</div> <li v-for="(row, index) in tableData" :key="index" class="listV2_row pointer" @click="rowClickToDetail(row)"> <!-- 單元格-列 --> <span v-for="(col, index) in fieldList" :key="index" class="listV2_cell ellipsis" :name="col.fieldName"> <RenderDom :vNode="row[col.fieldName] || '-'"></RenderDom> </span> </li> </ul> </template> <script> import RenderDom from "./renderDom"; export default { name: 'TableList', props: { tableData: { type: Array, required: true, }, fieldList: { type: Array, required: true, }, align: { type: String, default: 'left', }, }, components: { RenderDom, }, data() { return {} }, computed: {}, watch: {}, created() { }, mounted() { }, methods: {}, updated() { }, beforeDestroy() { }, } </script> <style lang='less' rel='stylesheet/less' scoped> @import "./index.less"; </style>
3、封裝渲染vNode的方法
const renderDom = { props: { vNode: { type: [Array, String, Object,Number], }, }, render(h) { // jsx - vNode 直接返回,交給框架處理(js語法帶來很多可能,列表打標(biāo)簽功能) if (typeof this.vNode === 'object') { return this.vNode; } // 普通數(shù)據(jù),直接包一層div,然后返回給頁面 return h( 'div', { class: 'ellipsis', }, this.vNode ) } }
4、頁面組件調(diào)用
<template> <div class="customer"> <table-list v-loading="p_loading" :tableData="p_listdata" :fieldList="fieldList"></table-list> </div> </template> <script> import TableList from '@/basecomponents/TableList/index' import $_pMixins from "@/mixins/product.js"; import enums from './enum.js' export default { name: 'Customer', props: {}, components: { 'table-list': TableList, }, mixins: [$_pMixins], data() { return { tableData: [], fieldList: Object.freeze(enums.Enum_customerFieldList), } }, computed: {}, watch: {}, created() { }, mounted() { this.initData() }, methods: { initData() { this.getProductList() } }, updated() { }, beforeDestroy() { }, } </script> <style lang='less' rel='stylesheet/less' scoped> @import "./index.less"; </style>
效果展示
以上就是使用vNode實(shí)現(xiàn)給列表字段打標(biāo)簽的詳細(xì)內(nèi)容,更多關(guān)于vNode列表字段標(biāo)簽的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue一個(gè)頁面實(shí)現(xiàn)音樂播放器的示例
這篇文章主要介紹了vue一個(gè)頁面實(shí)現(xiàn)音樂播放器的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02vue下axios攔截器token刷新機(jī)制的實(shí)例代碼
這篇文章主要介紹了vue下axios攔截器token刷新機(jī)制的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01