ReactAntd(二十四) React-基础表格带CheckBox或者Radio选项

基础表格带 CheckBox 或者 Radio 选项

CheckBox 选项

import React, { useState } from 'react'
import { Table } from 'antd'
function useBasicTable2() {
  const [pagnationPage] = useState('bottomCenter') //改成BottomCenter
  const [selectionType] = useState('checkbox') //checkbox
  //列数据
  const columns = [
    {
      title: '姓名',
      width: 100, //不写就自适应
      dataIndex: 'name', //列对应的数据名称
      render: (text) => <div>{text}</div>,
    },
    {
      title: '资产',
      width: 100, //不写就自适应
      className: 'column-money',
      dataIndex: 'money', //列对应的数据名称
      align: 'center', //right center,left
    },
    {
      title: '地址',
      dataIndex: 'address', //列对应的数据名称
    },
  ]
  //数据
  const data = [
    {
      key: '1',
      name: '张三',
      money: '$300,000.00',
      address: 'New York No. 1 Lake Park',
    },
    {
      key: '2',
      name: '李四',
      money: '$1,256,000.00',
      address: 'London No. 1 Lake Park',
    },
    {
      key: '3',
      name: '王五',
      money: '$120,000.00',
      address: 'Sidney No. 1 Lake Park',
    },
  ]
  //Header
  const HeaderData = () => {
    return <div classsname="Footer">这就是头部222</div>
  }
  //Footer
  const FooterData = () => {
    return <div classsname="Footer">这就是尾巴</div>
  }
  //分页
  const handleClick = (current, pageSize) => {
    console.log(current, pageSize)
    console.log(`当前页是${current}`)
    console.log(`每页条数${pageSize}`)
  }
  //checkbox 选择
  const rowSelection = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(
        `selectedRowKeys: ${selectedRowKeys}`,
        'selectedRows: ',
        selectedRows
      )
    },
    getCheckboxProps: (record) => ({
      disabled: record.name === '张三', // Column configuration not to be checked
      name: record.name,
    }),
  }

  return (
    <Table
      rowSelection={{
        type: selectionType,
        ...rowSelection,
      }}
      columns={columns}
      dataSource={data}
      bordered
      title={HeaderData}
      footer={FooterData}
      pagination={{
        position: [pagnationPage],
        defaultCurrent: 1,
        defaultPageSize: 2,
        total: 50,
        onChange: handleClick,
      }}
    />
  )
}

export default useBasicTable2

Radio 选项

import React, { useState } from 'react'
import { Table } from 'antd'
function useBasicTable3() {
  const [selectionType] = useState('radio')
  const [pagnationPage] = useState('bottomCenter') //改成BottomCenter
  const columns = [
    {
      title: 'Name',
      dataIndex: 'name',
      render: (text) => <div>{text}</div>,
    },
    {
      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',
    },
  ]
  const rowSelection = {
    onChange: (selectedRowKeys, selectedRows) => {
      console.log(
        `selectedRowKeys: ${selectedRowKeys}`,
        'selectedRows: ',
        selectedRows
      )
    },
    getCheckboxProps: (record) => ({
      disabled: record.name === 'Disabled User', // Column configuration not to be checked
      name: record.name,
    }),
  }
  //分页
  const handleClick = (current, pageSize) => {
    console.log(current, pageSize)
    console.log(`当前页是${current}`)
    console.log(`每页条数${pageSize}`)
  }
  return (
    <Table
      rowSelection={{
        type: selectionType,
        ...rowSelection,
      }}
      columns={columns}
      dataSource={data}
      pagination={{
        position: [pagnationPage],
        defaultCurrent: 1,
        defaultPageSize: 2,
        total: 50,
        onChange: handleClick,
      }}
    />
  )
}

export default useBasicTable3

文章作者: 雾烟云
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 雾烟云 !
  目录