ant table 点击当前行 实现单选/多选和高亮

这篇博客展示了如何在 Vue.js 中使用 A-Table 组件实现表格的单选和多选功能。通过 `rowSelection` 属性配置选择行为,`customRow` 用于监听行点击事件,`selectedRowKeys` 保存已选中的行键。示例代码详细解释了单选和多选的逻辑处理,包括选中和取消选中行的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多选

<template>
  <a-table
    :row-selection="rowSelection"
    :columns="columns"
    :data-source="data"
    :custom-row="customRow"
  >
    <template #name="{ text }">
      <a>{{ text }}</a>
    </template>
  </a-table>
</template>
<script>
import { computed, defineComponent, reactive } from "vue";
const columns = [
  {
    title: "Name",
    dataIndex: "name",
    slots: { customRender: "name" },
  },
  {
    title: "Age",
    dataIndex: "age",
  },
  {
    title: "Address",
    dataIndex: "address",
  },
];
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: "Disabled User",
    age: 99,
    address: "Sidney No. 1 Lake Park",
  },
];
export default defineComponent({
  name: "App",
  setup() {
    const state = reactive({
      selectedRowKeys: [],
    });
    const selectRow = (record) => {
      const selectedRowKeys = [...state.selectedRowKeys];
      if (selectedRowKeys.indexOf(record.key) >= 0) {
        selectedRowKeys.splice(selectedRowKeys.indexOf(record.key), 1);
      } else {
        selectedRowKeys.push(record.key);
      }
      state.selectedRowKeys = selectedRowKeys;
    };
    const rowSelection = computed(() => {
      return {
        selectedRowKeys: state.selectedRowKeys,
        onChange: (selectedRowKeys) => {
          state.selectedRowKeys = selectedRowKeys;
        },
      };
    });
    const customRow = (record) => {
      return {
        onClick: () => {
          selectRow(record);
        },
      };
    };

    return {
      data,
      columns,
      customRow,
      rowSelection,
    };
  },
});
</script>

单选

<template>
  <a-table
    :row-selection="rowSelection"
    :columns="columns"
    :data-source="data"
    :custom-row="customRow"
  >
    <template #name="{ text }">
      <a>{{ text }}</a>
    </template>
  </a-table>
</template>
<script>
import { computed, defineComponent, reactive } from "vue";
const columns = [
  {
    title: "Name",
    dataIndex: "name",
    slots: { customRender: "name" },
  },
  {
    title: "Age",
    dataIndex: "age",
  },
  {
    title: "Address",
    dataIndex: "address",
  },
];
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: "Disabled User",
    age: 99,
    address: "Sidney No. 1 Lake Park",
  },
];
export default defineComponent({
  name: "App",
  setup() {
     /**
     * 点击当前行 实现单选和高亮
     */
    const state = reactive({
      selectedRowKeys: [],
    });

    const selectRow = record => {
      const selectedRowKeys = [...state.selectedRowKeys];
      selectedRowKeys.splice(0, 1, record.key);
      state.selectedRowKeys = selectedRowKeys;
    };
    const rowSelection = computed(() => {
      return {
        type: 'radio',
        selectedRowKeys: state.selectedRowKeys,
        onChange: selectedRowKeys => {
          state.selectedRowKeys = selectedRowKeys;
        },
      };
    });

    const customRow = record => {
      return {
        onClick: () => {
          selectRow(record);
        },
      };
    };

    return {
      data,
      columns,
      customRow,
      rowSelection,
    };
  },
});
</script>

   
Ant Design of Vue 是一套基于 Vue.js 的 UI 组件库,其中包含了丰富的组件,包括表格组件。下面是一个带分页功能的 Ant Design of Vue 表格示例: ```html <template> <a-table :columns="columns" :data-source="dataSource" :pagination="pagination" row-selection="rowSelection" @change="handleTableChange"> </a-table> </template> <script> export default { data() { return { columns: [ { title: '姓名', dataIndex: 'name' }, { title: '年龄', dataIndex: 'age' }, { title: '地址', dataIndex: 'address' } ], dataSource: [], // 表格数据 pagination: { // 分页配置 current: 1, // 当前页码 pageSize: 10, // 每页显示条数 total: 0 // 数据总条数 }, rowSelection: { // 配置 selectedRowKeys: [], onChange: this.handleRowSelectionChange } } }, methods: { // 处理表格变化事件 handleTableChange(pagination) { this.pagination = pagination this.fetchData() }, // 处理变化事件 handleRowSelectionChange(selectedRowKeys) { this.rowSelection.selectedRowKeys = selectedRowKeys }, // 获取表格数据 fetchData() { // 发送异步请求获取数据 // ... // 设置表格数据分页信息 this.dataSource = [] this.pagination.total = 100 } }, mounted() { // 初始化数据 this.fetchData() } } </script> ``` 在上面的代码中,`columns` 数组定义了表格的列信息,`dataSource` 数组存储了表格的数据,`pagination` 对象定义了分页信息,`rowSelection` 对象定义了配置。在 `a-table` 组件中,我们将这些配置传递给了组件,并设置了相应的事件回调函数。在 `handleTableChange` 方法中,我们处理了分页变化事件,在 `handleRowSelectionChange` 方法中,我们处理了变化事件。`fetchData` 方法用于获取表格数据,我们可以在其中发送异步请求获取数据,并设置表格数据分页信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值