【问题标题】:How to add a radiobutton to select a row with ant design table如何添加单选按钮以选择带有 ant 设计表的行
【发布时间】:2019-01-13 03:51:53
【问题描述】:

我想在这个 ant 表中添加一个额外的列,该列应该有一个单选按钮来选择活动行。

一次只能选择一行,选择后我需要调用一个至少具有所选客户端ID的rest api。

在 antd 文档中我找不到类似的东西。 https://ant.design/components/table/

import React, { Component } from 'react';
import {  Table} from 'antd';
import { adalApiFetch } from '../../adalConfig';


class ListTenants extends Component {

    constructor(props) {
        super(props);
        this.state = {
            data: []
        };
    }



    fetchData = () => {
        adalApiFetch(fetch, "/Tenant", {})
          .then(response => response.json())
          .then(responseJson => {
            if (!this.isCancelled) {
                const results= responseJson.map(row => ({
                    key: row.ClientId,
                    ClientId: row.ClientId,
                    ClientSecret: row.ClientSecret,
                    Id: row.Id,
                    SiteCollectionTestUrl: row.SiteCollectionTestUrl,
                    TenantDomainUrl: row.TenantDomainUrl
                  }))
              this.setState({ data: results });
            }
          })
          .catch(error => {
            console.error(error);
          });
      };


    componentDidMount(){
        this.fetchData();
    }

    render() {
        const columns = [
                {
                    title: 'Client Id',
                    dataIndex: 'ClientId',
                    key: 'ClientId'
                }, 
                {
                    title: 'Site Collection TestUrl',
                    dataIndex: 'SiteCollectionTestUrl',
                    key: 'SiteCollectionTestUrl',
                },
                {
                    title: 'Tenant DomainUrl',
                    dataIndex: 'TenantDomainUrl',
                    key: 'TenantDomainUrl',
                }
        ];



        return (
            <Table columns={columns} dataSource={this.state.data} />
        );
    }
}

export default ListTenants;

感谢任何有关如何执行此操作的指导

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    只是像这样的简单用户表

    &lt;Table rowSelection={{type:'radio'}} &gt;...&lt;/Table&gt;

    【讨论】:

      【解决方案2】:

      我是这样解决的

      import React, { Component } from 'react';
      import {  Table, Radio} from 'antd';
      import { adalApiFetch } from '../../adalConfig';
      import Notification from '../../components/notification';
      
      class ListTenants extends Component {
      
          constructor(props) {
              super(props);
              this.state = {
                  data: []
              };
          }
      
      
      
          fetchData = () => {
              adalApiFetch(fetch, "/Tenant", {})
                .then(response => response.json())
                .then(responseJson => {
                  if (!this.isCancelled) {
                      const results= responseJson.map(row => ({
                          key: row.ClientId,
                          ClientId: row.ClientId,
                          ClientSecret: row.ClientSecret,
                          Id: row.Id,
                          SiteCollectionTestUrl: row.SiteCollectionTestUrl,
                          TenantDomainUrl: row.TenantDomainUrl
                        }))
                    this.setState({ data: results });
                  }
                })
                .catch(error => {
                  console.error(error);
                });
            };
      
      
          componentDidMount(){
              this.fetchData();
          }
      
          render() {
              const columns = [
                      {
                          title: 'Client Id',
                          dataIndex: 'ClientId',
                          key: 'ClientId'
                      }, 
                      {
                          title: 'Site Collection TestUrl',
                          dataIndex: 'SiteCollectionTestUrl',
                          key: 'SiteCollectionTestUrl',
                      },
                      {
                          title: 'Tenant DomainUrl',
                          dataIndex: 'TenantDomainUrl',
                          key: 'TenantDomainUrl',
                      }
              ];
      
              // rowSelection object indicates the need for row selection
              const rowSelection = {
                  onChange: (selectedRowKeys, selectedRows) => {
                      if(selectedRows[0].key != undefined){
                          console.log(selectedRows[0].key);
      
      
                          const options = { 
                              method: 'post', 
                              body: JSON.stringify({ clientid : selectedRows[0].key.toString() }) ,
                              config: {
                                  headers: {
                                    'Content-Type': 'application/json'
                                  }
                                }
                          };
      
                          adalApiFetch(fetch, "/Tenant/SetTenantActive", options)
                              .then(response =>{
                              if(response.status === 200){
                                  Notification(
                                      'success',
                                      'Tenant set to active',
                                      ''
                                      );
                              }else{
                                  throw "error";
                              }
                              })
                              .catch(error => {
                              Notification(
                                  'error',
                                  'Tenant not activated',
                                  error
                                  );
                              console.error(error);
                          });
                      }
                  },
                  getCheckboxProps: record => ({
                      type: Radio
                  }),
              };
      
              return (
                  <Table rowSelection={rowSelection} columns={columns} dataSource={this.state.data} />
              );
          }
      }
      
      export default ListTenants;
      

      基本上是 const rowselection,有一个属性 getCheckboxProps 来设置单选或复选框。

      【讨论】:

        猜你喜欢
        • 2022-12-01
        • 2017-09-30
        • 2016-03-08
        • 2016-12-14
        • 2019-04-04
        • 2013-10-27
        • 2022-11-24
        • 2018-04-19
        • 1970-01-01
        相关资源
        最近更新 更多