vue3+Naive?UI數(shù)據(jù)表格基本使用方式詳解
一、基礎(chǔ)使用方式
1.引入表格及數(shù)據(jù)
<-- 表格標(biāo)簽 --> <n-data-table :bordered="false" :single-line="false" :columns="tableHead" :data="tabeldata"/> //表頭數(shù)據(jù) 一個(gè)對(duì)象表示一列 titel是每一列的名字 key是對(duì)應(yīng)的字段名 可在對(duì)象類寫(xiě)每一列的寬度居中方式等樣式 const tableHead = ref([ { title: '姓名', key: 'name', width: 300, align: 'center', }, { title: '年齡', key: 'age' }, { title: '地址', key: 'address' }, { title: '標(biāo)簽', key: 'tags', render(row) { const tags = row.tags.map((tagKey) => { return h( NTag, { style: { marginRight: '6px' }, type: 'info', bordered: false }, { default: () => tagKey } ) }) return tags } }, { title: '狀態(tài)', key: 'status' }, { title: '操作', key: 'actions', //添加按鈕時(shí)必須有模板組件,將按鈕信息以參數(shù)形式傳到組件中,在組件中寫(xiě)相關(guān)樣式 或 使用naive ui提供的組件 render(record) { return [ h(NButton, { //NButton是naive ui提供的按鈕組件模板,需要引入 import { NTag, NButton, } from 'naive-ui' text: true, style: { marginRight: '10px' }, onClick: () => viewdetail(record) }, { default: () => '詳情' } ), h(NButton, { text: true, onClick: () => deletedata(record) }, { default: () => '刪除' } ) ] } } ]) //表格數(shù)據(jù) const tabeldata = ref([ { key: 0, name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', status: '0', tags: ['nice', 'developer'] }, { key: 1, name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', status: '1', tags: ['wow'] }, { key: 2, name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', status: '0', tags: ['cool', 'teacher'] } ])
這樣一個(gè)基礎(chǔ)表格就渲染出來(lái)了
二、修改操作按鈕等標(biāo)簽樣式
項(xiàng)目中可能會(huì)修改操作按鈕、標(biāo)簽等元素樣式修改,以按鈕為例,提供以下方式(需要注意的是,在naive ui數(shù)據(jù)表格中使用按鈕、標(biāo)簽等元素必須提供HTML元素模板,否則渲染出錯(cuò),所謂模板其實(shí)就是一個(gè)組件)
1.naive ui提供的模板
{ title: '操作', key: 'actions', //使用naive ui提供的按鈕模板,需要引入按鈕模板 import { NButton } from 'naive-ui' render(record) { return [ h(NButton, {//可在里面寫(xiě)按鈕樣式 text: true, style: { marginRight: '10px' }, onClick: () => viewdetail(record)//點(diǎn)擊按鈕后的回調(diào) }, { default: () => '詳情' }//按鈕顯示名稱 ), h(NButton, { info: true, onClick: () => deletedata(record) }, { default: () => '刪除' } ) ] } }
這樣就可以渲染出按鈕
2.自定義按鈕模板
項(xiàng)目中使用naive提供的模板可能滿足不了需求,這時(shí)可以使用自定義模板(組件)
①定義一個(gè)組件文件
<template> <div> <template v-for="(action, index) in getActions" :key="index"> <n-button v-bind="action" style="margin-right: 10px;" text> {{ action.label }} </n-button> </template> </div> </template> <script lang="ts"> import { defineComponent, PropType, computed, toRaw, onMounted } from 'vue'; export default defineComponent({ props: { actions: { type: Object, default: null, required: true, }, }, setup(props) { onMounted(() => { // console.log(props); }) const getActions = computed(() => { return props.actions }); return { getActions, }; }, }); </script>
②頁(yè)面中引入該組件并使用
import ActionTemplate from '@/components/ActionTemplate.vue'
{ title: '操作', key: 'actions', render(record) { return h(ActionTemplate,//使用按鈕組件 { actions: [//通過(guò)props傳參將actions傳給按鈕組件(對(duì)應(yīng)樣式也可傳遞過(guò)去) { label: '刪除', onClick: deletedata.bind(null, record), }, { label: '詳情', onClick: viewdetail.bind(null, record), }, ], } ) } }
這樣就可以渲染出按鈕
三、根據(jù)數(shù)據(jù)顯示不同內(nèi)容
項(xiàng)目中可能會(huì)根據(jù)后臺(tái)返回的數(shù)據(jù)在表格中顯示不同內(nèi)容,可以通過(guò)以下方式實(shí)現(xiàn)
{ title: '狀態(tài)', key: 'status', className: 'stustatus', render(record) { let text = '' if (record.status == '0') { text = '在讀' } else if (record.status == '1') { text = '畢業(yè)' } return h('span', text)//這種渲染方式與渲染按鈕操作一樣必須提供元素模板,但是這里直接這樣寫(xiě)'span'就可以,而渲染操作按鈕的時(shí)候?qū)?n-button'卻會(huì)報(bào)錯(cuò),我也搞不懂,有了解的伙伴可以解釋一下 } },
最后就可以根據(jù)數(shù)據(jù)渲染不同內(nèi)容了
四、不同內(nèi)容顯示不同樣式
項(xiàng)目中表格不同內(nèi)容會(huì)顯示不同的樣式,提供以下方式實(shí)現(xiàn)
①在表格標(biāo)簽中加上返回樣式的屬性 :row-class-name="rowClassName"
<n-data-table :bordered="false" :single-line="false" :columns="tableHead" :data="tabeldata" :row-class-name="rowClassName" />
②定義返回類名的函數(shù)
const rowClassName = (row) => { if (row.status == '0') { return 'statusin' } return 'statusout' }
③在style中寫(xiě)不同類名對(duì)應(yīng)的樣式
:deep(.statusin .stustatus) { color: rgba(63, 210, 19, 0.75) !important; } :deep(.statusout .stustatus) { color: rgba(251, 8, 61, 0.75) !important; }
④在對(duì)應(yīng)列的表頭對(duì)象里面加入類名屬性 className: 'stustatus'
{ title: '狀態(tài)', key: 'status', className: 'stustatus', render(record) { let text = '' if (record.status == '0') { text = '在讀' } else if (record.status == '1') { text = '畢業(yè)' } return h('span', text) } },
最后表格就渲染出來(lái)了
我也是在最近的項(xiàng)目迭代中開(kāi)始使用的vue3+naive ui,開(kāi)發(fā)過(guò)程中也遇到很多問(wèn)題,網(wǎng)上與naive ui相關(guān)的文章太少了,也只能看文檔慢慢研究,后面也會(huì)不斷地更新與naive ui相關(guān)的文章,總之多看看文檔吧。
總結(jié)
到此這篇關(guān)于vue3+Naive UI數(shù)據(jù)表格基本使用方式的文章就介紹到這了,更多相關(guān)vue3 Naive UI數(shù)據(jù)表格使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 實(shí)現(xiàn)穿梭框功能的詳細(xì)代碼
本文給大家介紹Vue 實(shí)現(xiàn)穿梭框功能,代碼分為css,html和js代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-10-10

教你如何在 Nuxt 3 中使用 wavesurfer.js

Vue前端如何設(shè)置Cookie和鑒權(quán)問(wèn)題詳解

vue項(xiàng)目中v-model父子組件通信的實(shí)現(xiàn)詳解

前端項(xiàng)目中如何使用百度地圖api(含實(shí)例)

如何用vue-pdf包實(shí)現(xiàn)pdf文件預(yù)覽,支持分頁(yè)

Vuejs 2.0 子組件訪問(wèn)/調(diào)用父組件的方法(示例代碼)

利用vue組件實(shí)現(xiàn)圖片的拖拽和縮放功能