Antd中使用Table的rowSelection筛选功能,搭配检索选择需求实现

需求背景:需要一个列表能通过页面检索实现模糊查询,同时进行选择筛选

思路:通过Table组件的rowKey将列表选中项集合定位成一个code或者key集合,然后再模糊查询筛选列表时,不断地更新这个集合,最终再用户查询筛选的最后,将这个集合传给后端。

上代码

class RecordChecks extends React.Component<Props, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      dataAll: [{
        code: 'code1',
        name: '数据一'
      }, {
        code: 'code2',
        name: '数据二'
      }, {
        code: 'code3',
        name: '数据三'
      }, {
        code: 'code4',
        name: '数据四'
      }, {
        code: 'code5',
        name: '数据五'
      }], // 列表数据
      temporaryData: [], // 暂存未被模糊查询前的数据集合
      selectedRowKeys: ['code3', 'code4'], // 默认选中项,现实情况一般由后端返回
    };
  }
  componentDidMount(): void {
    this.setState({ temporaryData: this.state.dataAll }) // 一开始 将会被模糊查询调整的数据先保存一遍
  }
  seachChange = (e: any) => {
    let data = e.target.value, array: any = []
    const { dataAll, temporaryData } = this.state;
    if (dataAll && Array.isArray(dataAll) && dataAll.length > 0) {
      dataAll.map((item: any) => {  // 模糊查询
        if (item.code.indexOf(`${data}`) > -1 || item.name.indexOf(`${data}`) > -1) {
          array.push(item)
        }
      })
      if (!e.target.value) { //数据为空时,将一开始未筛选的数据重新展示
        array = temporaryData
      }
      this.setState({ dataAll: array })
    }
  }
  render() {
    const column2: any = [{
      dataIndex: 'code',
      title: '值',
    }, {
      dataIndex: 'name',
      title: '名称',
    }]

    const rowSelection = {
      selectedRowKeys: this.state.selectedRowKeys,
      onChange: (selectedRowKeys: any, selectedRows: any) => {
        this.setState({ selectedRowKeys, selectedRows })
      },
    };

    return (
      <div>
        <span>检索:</span>
        <Input onChange={this.seachChange} ></Input>
        <Table
          columns={column2}
          dataSource={this.state.dataAll}
          rowKey={(record: any, index) => `${record.code}`}
          rowSelection={rowSelection}
        ></Table>
      </div >
    );
  }
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue3使用Antd实现检索框嵌套一个Table的步骤如下: 1. 安装Antd和Axios ``` npm install ant-design-vue axios ``` 2. 在main.js引入Antd和Axios ```javascript import { createApp } from 'vue' import Antd from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; import axios from 'axios'; import App from './App.vue' const app = createApp(App) app.config.productionTip = false app.use(Antd) app.config.globalProperties.$axios = axios app.mount('#app') ``` 3. 在组件引入Table和Input组件 ```javascript <template> <div> <a-input-search v-model="search" placeholder="请输入关键字" @search="getTableData" /> <a-table :columns="columns" :dataSource="tableData" :loading="loading" /> </div> </template> <script> import { defineComponent } from 'vue' import { Table, Input } from 'ant-design-vue' export default defineComponent({ components: { 'a-table': Table, 'a-input-search': Input.Search }, data() { return { search: '', tableData: [], loading: false, columns: [ { title: '姓名', dataIndex: 'name', key: 'name' }, { title: '年龄', dataIndex: 'age', key: 'age' }, { title: '地址', dataIndex: 'address', key: 'address' } ] } }, methods: { getTableData() { this.loading = true this.$axios.get('/api/data', { params: { search: this.search } }).then(res => { this.tableData = res.data this.loading = false }).catch(err => { console.error(err) this.loading = false }) } } }) </script> ``` 在这个例子,我们使用了Input.Search来实现一个搜索框,当用户输入关键字并按下Enter键时,会触发search事件,调用getTableData方法来请求数据。在getTableData方法,我们通过Axios发送请求,将搜索关键字作为参数传递给后端,在后端进行数据查询,返回查询结果。最后,我们将查询结果显示在Table组件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值