vue3配合naive-ui的虚拟表格显示超过1000条数据,并且带有单选按钮

父组件

<template>
  <div style="width: 1900px">
    <n-data-table
      :columns="columns"
      :data="data"
      max-height="100%"
      scroll-x="100%"
      virtual-scroll
    />
  </div>
</template>

<script lang="ts" setup>
import { h, ref } from "vue";
import type { DataTableColumns } from "naive-ui";
import HomeView from "./views/HomeView.vue";
interface RowData {
  key: number;
  name: string;
  age: number;
  address: string;
}
const select = ref<number>(null);
const columns = ref<DataTableColumns<RowData>>([
  {
    title: "",
    key: "radio",
    width: 200,
    render(row) {
      return h(
        HomeView,
        {
          keys: row.key,
          checked: select.value,
          onchange: () => {
            select.value = row["key"];
          },
        },
        ""
      );
    },
  },
  {
    title: "Name",
    key: "name",
    width: 200,
  },
  {
    title: "Age",
    key: "age",
    width: 100,
  },
  {
    title: "Row",
    key: "row",
    render(row, index) {
      return h("span", ["row ", index]);
    },
  },
  {
    title: "Row1",
    key: "row1",
    render(row, index) {
      return h("span", ["row ", index]);
    },
  },
  {
    title: "Row2",
    key: "row2",
    render(row, index) {
      return h("span", ["row ", index]);
    },
    width: 100,
    fixed: "right",
  },
  {
    title: "Address",
    key: "address",
    width: 200,
    fixed: "right",
  },
]);
const data = ref<RowData[]>(
  Array.from({ length: 1000 }).map((_, index) => ({
    key: index,
    name: `Edward King ${index}`,
    age: 32,
    address: `London, Park Lane no. ${index}`,
  }))
);
</script>
<style>
.radio-class {
  padding: 20px;
}
</style>

子组件:HomeView.vue

<template>
  <div>
    <n-space>
      <n-radio :checked="checkedValue === keys" :value="keys" name="basic-demo">
      </n-radio>
    </n-space>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps, withDefaults, onMounted, watch } from "vue";
interface propsa {
  keys?: number;
  checked?: number;
}
const props = withDefaults(defineProps<propsa>(), {
  keys: null,
  checked: null,
});
const checkedValue = ref<number>(null);
watch(
  () => props.checked,
  () => {
    checkedValue.value = props.checked;
  }
);
onMounted(() => {
  checkedValue.value = props.checked;
});
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现外部点击按钮删除或添加table表格数据,你可以在Vue3中使用v-model来绑定table的数据,并在外部按钮的点击事件中修改这个数据。具体实现过程如下: 1. 在template中使用view-ui-plus中的table组件,并使用v-model绑定数据: ``` <template> <div> <el-button @click="addData">添加数据</el-button> <el-table v-model="tableData" :columns="columns"></el-table> </div> </template> <script> export default { data() { return { tableData: [], columns: [ // 的定义 ], } }, methods: { addData() { // 添加数据的逻辑 }, } } </script> ``` 2. 在外部按钮的点击事件中修改tableData数组的值: ``` <template> <div> <el-button @click="addData">添加数据</el-button> <el-button @click="removeData">删除数据</el-button> <el-table v-model="tableData" :columns="columns"></el-table> </div> </template> <script> export default { data() { return { tableData: [], columns: [ // 的定义 ], } }, methods: { addData() { // 添加数据的逻辑 }, removeData() { // 删除数据的逻辑 }, } } </script> ``` 3. 在添加或删除数据的逻辑中,修改tableData数组的值: ``` <template> <div> <el-button @click="addData">添加数据</el-button> <el-button @click="removeData">删除数据</el-button> <el-table v-model="tableData" :columns="columns"></el-table> </div> </template> <script> export default { data() { return { tableData: [], columns: [ // 的定义 ], } }, methods: { addData() { this.tableData.push({ // 新数据的定义 }) }, removeData() { this.tableData.splice(index, 1); // index为要删除的数据在tableData中的下标 }, } } </script> ``` 这样就可以在外部按钮的点击事件中添加或删除table表格数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沙滩上的一颗石头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值