vue3 ant design table中插槽使用

简介: vue3

一、CustomRender写法

在这里插入图片描述

const columns: any[] = [
    {
    title: "是否启用",
    dataIndex: "itemStatus",
    customRender: (text, record, index) => {
      return text.text == 1 ? "否" : "是"
    }
  },
]

1、vue2x+antd1x

<template>
    <a-table :columns="columns" :dataSource="dataSource" :pagination="false" ></a-table>
</template>

<script>
export default {
    data() {
        return {
            dataSource: [],
             columns:[
              {
                title: 'Date Sent',
                dataIndex: 'paymentDate',
                key: 'paymentDate',
                align: 'center',
                customRender: (text,record) => {
                  if (record.status == 2) {
                      return <span style="color:red">{ text }</span>;
                    }else{
                      return text
                    }
                },
              },
              {
                title: 'Sender Account Name',
                key: 'accountName',
                dataIndex: 'accountName',
                align: 'center',
              },
          ]
        }
    }
}
</script>

2、vue3x+antd3x+js

<template>
<div>{{num}}</div>
  <a-table
    class="ant-table-striped"
    size="middle"
    :columns="columns"
    :data-source="data"
    :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)"
  />
</template>
<script>
import { defineComponent, ref } from 'vue'
const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    customRender: function ({ text }) {
      return <span style="color: red;cursor: pointer;" onClick={changeNum}>{text}</span>
    }
  }, {
    title: 'Age',
    dataIndex: 'age'
  }, {
    title: 'Address',
    dataIndex: 'address'
  }
]
const num = ref(0)
const changeNum = () => {
  num.value++
}
const data = [{
  key: '1',
  name: 'John Brown',
  age: 32,
  address: 'New York No. 1 Lake Park'
}, {
  key: '2',
  name: 'Jim Green',
  age: 42,
  address: 'London No. 1 Lake Park'
}, {
  key: '3',
  name: 'Joe Black',
  age: 32,
  address: 'Sidney No. 1 Lake Park'
}, {
  key: '4',
  name: 'Ben Kang',
  age: 15,
  address: 'Sidney No. 1 Lake Park'
}]
export default defineComponent({
  setup () {
    return {
      data,
      columns,
      num,
      changeNum
    }
  }

})
</script>

3、vue3x+antd3x+ts

<template>
  <div>{{num}}</div>
  <a-table
    class="ant-table-striped"
    size="middle"
    :columns="columns"
    :data-source="data"
    :row-class-name="(_record, index) => (index % 2 === 1 ? 'table-striped' : null)"
  />
</template>
<script lang="ts">
import { ref, defineComponent, h, VNode } from 'vue'

const columns = [
  {
    title: 'Name',
    dataIndex: 'name',
    customRender: function ({ text }):VNode {
      return h('div', {
        style: {
          color: 'red',
          cursor: 'pointer'
        },
        class: 'name',
        onClick: changeNum
      }, text)
    }
  },
  { title: 'Age', dataIndex: 'age' },
  { title: 'Address', dataIndex: 'address' }
]
const num = ref(0)
const changeNum = () => {
  num.value++
}

const data = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park'
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park'
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sidney No. 1 Lake Park'
  },
  {
    key: '4',
    name: 'Ben Kang',
    age: 15,
    address: 'Sidney No. 1 Lake Park'
  }
]

export default defineComponent({
  setup () {
    return {
      data,
      columns,
      num,
      changeNum
    }
  }
})
</script>

二、插槽写法

在这里插入图片描述

<template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'itemStatus'">
          <a-switch :checked="record.itemStatus"
                :checked-value="1"
                :unchecked-value="0"/>
        </template>
</template>

在这里插入图片描述

<template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex == 'itemStatus'">
          <a-tag v-if="record.itemStatus == 1" color="red">启用</a-tag>
          <a-tag v-else color="green">禁用</a-tag>
        </template>
</template>

三、数据处理(只针对翻译)

listTable.value.forEach(e => {
    e.itemStatus =e.itemStatus == 1 ? '是' : '否';
});

注意: 编辑后记得刷新,建议使用前两种方法,嘿嘿~

目录
相关文章
|
9天前
vue3【实战】语义化首页布局
vue3【实战】语义化首页布局
28 2
|
4天前
【vue3】Argumnt of type ‘history:RouterHistory;}is not assignable to paraeter of type ‘RouterOptions‘.
【vue3】Argumnt of type ‘history:RouterHistory;}is not assignable to paraeter of type ‘RouterOptions‘.
6 0
|
4天前
|
JavaScript
【vue3】vue3中路由hash与History的设置
【vue3】vue3中路由hash与History的设置
9 0
|
4天前
|
编解码 前端开发
【Vue3】解决电脑分辨率125%、150%及缩放导致页面变形的问题
【Vue3】解决电脑分辨率125%、150%及缩放导致页面变形的问题
11 0
|
4天前
|
JavaScript
【vue】 vue 翻页时钟制作,vue2、vue3
【vue】 vue 翻页时钟制作,vue2、vue3
11 0
|
2天前
|
JavaScript
|
4天前
|
JavaScript
【vue】el-dialog 内的tinymce弹窗被遮挡的解决办法 及 tinymce打开弹出菜单后直接关闭对话组件,导致该弹出菜单残留
【vue】el-dialog 内的tinymce弹窗被遮挡的解决办法 及 tinymce打开弹出菜单后直接关闭对话组件,导致该弹出菜单残留
16 6
|
1天前
|
存储 缓存 JavaScript
vue代码优化方案
【7月更文挑战第13天】 **Vue.js 优化要点:** 分解大组件以提高复用性和加载速度;利用计算属性与侦听器优化数据处理;使用Object.freeze()减少响应式数据;借助Vuex或Composition API管理状态;实现虚拟滚动和无限加载提升长列表性能;路由懒加载减少初始加载时间;用Vue DevTools检测性能瓶颈;定期代码审查与重构;应用缓存策略;遵循最佳实践与团队规范,提升应用整体质量。
10 2
|
4天前
|
JavaScript 前端开发
【vue】 el-table解决分页不能筛选全部数据的问题
【vue】 el-table解决分页不能筛选全部数据的问题
15 4
|
4天前
|
JavaScript
【vue】 vue2 监听滚动条滚动事件
【vue】 vue2 监听滚动条滚动事件
10 1