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

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

多选

<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>

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值