当前位置:   article > 正文

vue3+Naive UI数据表格基本使用方式_naivueui

naivueui

一、基础使用方式

1.引入表格及数据

  1. <-- 表格标签 -->
  2. <n-data-table :bordered="false" :single-line="false"
  3. :columns="tableHead" :data="tabeldata"/>
  4. //表头数据 一个对象表示一列 titel是每一列的名字 key是对应的字段名 可在对象类写每一列的宽度居中方式等样式
  5. const tableHead = ref([
  6. {
  7. title: '姓名',
  8. key: 'name',
  9. width: 300,
  10. align: 'center',
  11. },
  12. {
  13. title: '年龄',
  14. key: 'age'
  15. },
  16. {
  17. title: '地址',
  18. key: 'address'
  19. },
  20. {
  21. title: '标签',
  22. key: 'tags',
  23. render(row) {
  24. const tags = row.tags.map((tagKey) => {
  25. return h(
  26. NTag,
  27. {
  28. style: {
  29. marginRight: '6px'
  30. },
  31. type: 'info',
  32. bordered: false
  33. },
  34. {
  35. default: () => tagKey
  36. }
  37. )
  38. })
  39. return tags
  40. }
  41. },
  42. {
  43. title: '状态',
  44. key: 'status'
  45. },
  46. {
  47. title: '操作',
  48. key: 'actions',
  49. //添加按钮时必须有模板组件,将按钮信息以参数形式传到组件中,在组件中写相关样式 或 使用naive ui提供的组件
  50. render(record) {
  51. return [
  52. h(NButton, { //NButton是naive ui提供的按钮组件模板,需要引入 import { NTag, NButton, } from 'naive-ui'
  53. text: true,
  54. style: { marginRight: '10px' },
  55. onClick: () => viewdetail(record)
  56. },
  57. { default: () => '详情' }
  58. ),
  59. h(NButton, {
  60. text: true,
  61. onClick: () => deletedata(record)
  62. },
  63. { default: () => '删除' }
  64. )
  65. ]
  66. }
  67. }
  68. ])
  69. //表格数据
  70. const tabeldata = ref([
  71. {
  72. key: 0,
  73. name: 'John Brown',
  74. age: 32,
  75. address: 'New York No. 1 Lake Park',
  76. status: '0',
  77. tags: ['nice', 'developer']
  78. },
  79. {
  80. key: 1,
  81. name: 'Jim Green',
  82. age: 42,
  83. address: 'London No. 1 Lake Park',
  84. status: '1',
  85. tags: ['wow']
  86. },
  87. {
  88. key: 2,
  89. name: 'Joe Black',
  90. age: 32,
  91. address: 'Sidney No. 1 Lake Park',
  92. status: '0',
  93. tags: ['cool', 'teacher']
  94. }
  95. ])

 这样一个基础表格就渲染出来了

 二、修改操作按钮等标签样式

项目中可能会修改操作按钮、标签等元素样式修改,以按钮为例,提供以下方式(需要注意的是,在naive ui数据表格中使用按钮、标签等元素必须提供HTML元素模板,否则渲染出错,所谓模板其实就是一个组件)

1.naive ui提供的模板

  1. {
  2. title: '操作',
  3. key: 'actions',
  4. //使用naive ui提供的按钮模板,需要引入按钮模板 import { NButton } from 'naive-ui'
  5. render(record) {
  6. return [
  7. h(NButton, {//可在里面写按钮样式
  8. text: true,
  9. style: { marginRight: '10px' },
  10. onClick: () => viewdetail(record)//点击按钮后的回调
  11. },
  12. { default: () => '详情' }//按钮显示名称
  13. ),
  14. h(NButton, {
  15. info: true,
  16. onClick: () => deletedata(record)
  17. },
  18. { default: () => '删除' }
  19. )
  20. ]
  21. }
  22. }

这样就可以渲染出按钮

 

2.自定义按钮模板 

项目中使用naive提供的模板可能满足不了需求,这时可以使用自定义模板(组件)

①定义一个组件文件

  1. <template>
  2. <div>
  3. <template v-for="(action, index) in getActions" :key="index">
  4. <n-button v-bind="action" style="margin-right: 10px;" text>
  5. {{ action.label }}
  6. </n-button>
  7. </template>
  8. </div>
  9. </template>
  10. <script lang="ts">
  11. import { defineComponent, PropType, computed, toRaw, onMounted } from 'vue';
  12. export default defineComponent({
  13. props: {
  14. actions: {
  15. type: Object,
  16. default: null,
  17. required: true,
  18. },
  19. },
  20. setup(props) {
  21. onMounted(() => {
  22. // console.log(props);
  23. })
  24. const getActions = computed(() => {
  25. return props.actions
  26. });
  27. return {
  28. getActions,
  29. };
  30. },
  31. });
  32. </script>

②页面中引入该组件并使用

import ActionTemplate from '@/components/ActionTemplate.vue'
  1. {
  2. title: '操作',
  3. key: 'actions',
  4. render(record) {
  5. return h(ActionTemplate,//使用按钮组件
  6. {
  7. actions: [//通过props传参将actions传给按钮组件(对应样式也可传递过去)
  8. {
  9. label: '删除',
  10. onClick: deletedata.bind(null, record),
  11. },
  12. {
  13. label: '详情',
  14. onClick: viewdetail.bind(null, record),
  15. },
  16. ],
  17. }
  18. )
  19. }
  20. }

这样就可以渲染出按钮

 三、根据数据显示不同内容

项目中可能会根据后台返回的数据在表格中显示不同内容,可以通过以下方式实现

  1. {
  2. title: '状态',
  3. key: 'status',
  4. className: 'stustatus',
  5. render(record) {
  6. let text = ''
  7. if (record.status == '0') {
  8. text = '在读'
  9. } else if (record.status == '1') {
  10. text = '毕业'
  11. }
  12. return h('span', text)//这种渲染方式与渲染按钮操作一样必须提供元素模板,但是这里直接这样写'span'就可以,而渲染操作按钮的时候写'n-button'却会报错,我也搞不懂,有了解的伙伴可以解释一下
  13. }
  14. },

最后就可以根据数据渲染不同内容了

 四、不同内容显示不同样式

项目中表格不同内容会显示不同的样式,提供以下方式实现

①在表格标签中加上返回样式的属性 :row-class-name="rowClassName"

  1. <n-data-table :bordered="false" :single-line="false" :columns="tableHead" :data="tabeldata"
  2. :row-class-name="rowClassName" />

 ②定义返回类名的函数

  1. const rowClassName = (row) => {
  2. if (row.status == '0') {
  3. return 'statusin'
  4. }
  5. return 'statusout'
  6. }

 ③在style中写不同类名对应的样式

  1. :deep(.statusin .stustatus) {
  2. color: rgba(63, 210, 19, 0.75) !important;
  3. }
  4. :deep(.statusout .stustatus) {
  5. color: rgba(251, 8, 61, 0.75) !important;
  6. }

④在对应列的表头对象里面加入类名属性 className: 'stustatus'

  1. {
  2. title: '状态',
  3. key: 'status',
  4. className: 'stustatus',
  5. render(record) {
  6. let text = ''
  7. if (record.status == '0') {
  8. text = '在读'
  9. } else if (record.status == '1') {
  10. text = '毕业'
  11. }
  12. return h('span', text)
  13. }
  14. },

最后表格就渲染出来了

 我也是在最近的项目迭代中开始使用的vue3+naive ui,开发过程中也遇到很多问题,网上与naive ui相关的文章太少了,也只能看文档慢慢研究,后面也会不断地更新与naive ui相关的文章,总之多看看文档吧。以上问题如果有其他解决方法的话欢迎交流。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/515202
推荐阅读
相关标签
  

闽ICP备14008679号