当没有值时,在选择控件中显示空选项

发布于 2025-02-06 20:48:44 字数 2569 浏览 1 评论 0原文

class Index extends React.Component {

    constructor(props) {
        super(props);
        
        this.state = {
          records:[],  
        } 
    }
.....
render() {
const columns = [
      {
        title: 'ID',
        dataIndex: 'ID',
        key: 'ID',
        sorter: (a, b) => a.ID - b.ID,
        render: (val)=><div title={val} className="text_overlap">{val}</div>
      },{
        title: 'Module',
        dataIndex: 'Description',
        key: 'Description',
        sorter: false,
        render: (val, record, index) => <div title={'Module: ' + val} className="text_overlap d-flex">
             
              {record.IsEdit ? <Select mode="multiple"
              style={{ width: '80%' }}
              value={this.state.descr}
              placeholder="Please select"
              onChange={(e) => {this.setState({set:true, descr:e});}}
              filterOption={(input, option) =>
                option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
              }>
              {this.state.Modules && this.state.Modules.map((d) => <Select.Option value={d.Description}>{d.Description}</Select.Option>)}
            </Select>  : <div style={{maxWidth:'90%',overflow:'hidden', textOverflow:'ellipsis'}}>{val && val.join(', ')}</div>}
            {!record.IsEdit && <div className="mx-2" style={{ cursor: 'pointer' }} title={"Edit"} onClick={() => { this.doEdit(record,index); this.setState({descr:record.Description}) }}><i className="fa fa-edit" style={{  fontSize:17, marginTop: 0 }}></i></div>}
            {record.IsEdit && <div className="mx-2 pt-1" style={{ cursor: 'pointer' }} title={"Save"} onClick={() => { this.updateRecord(record); this.setState({descr:''}) }}><i style={{fontSize:17}} className={this.state.updating ? "fa fa-spin fa-spinner" : "fa fa-save"}></i></div>}
            {record.IsEdit && <div className="mx-2 pt-1" style={{ cursor: 'pointer' }} title={"Cancel"} onClick={() => { this.doCancel(record); this.setState({descr:''}) }}><i style={{fontSize:17}} className="fa fa-times"></i></div>}
              </div>
      }
      
    ];

我的网格中有一个可编辑的列。但是,如果后端没有值,则在某些情况下显示一个空的选项。如果有价值,则没有问题。如何删除该空值?我正在使用antd。

class Index extends React.Component {

    constructor(props) {
        super(props);
        
        this.state = {
          records:[],  
        } 
    }
.....
render() {
const columns = [
      {
        title: 'ID',
        dataIndex: 'ID',
        key: 'ID',
        sorter: (a, b) => a.ID - b.ID,
        render: (val)=><div title={val} className="text_overlap">{val}</div>
      },{
        title: 'Module',
        dataIndex: 'Description',
        key: 'Description',
        sorter: false,
        render: (val, record, index) => <div title={'Module: ' + val} className="text_overlap d-flex">
             
              {record.IsEdit ? <Select mode="multiple"
              style={{ width: '80%' }}
              value={this.state.descr}
              placeholder="Please select"
              onChange={(e) => {this.setState({set:true, descr:e});}}
              filterOption={(input, option) =>
                option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
              }>
              {this.state.Modules && this.state.Modules.map((d) => <Select.Option value={d.Description}>{d.Description}</Select.Option>)}
            </Select>  : <div style={{maxWidth:'90%',overflow:'hidden', textOverflow:'ellipsis'}}>{val && val.join(', ')}</div>}
            {!record.IsEdit && <div className="mx-2" style={{ cursor: 'pointer' }} title={"Edit"} onClick={() => { this.doEdit(record,index); this.setState({descr:record.Description}) }}><i className="fa fa-edit" style={{  fontSize:17, marginTop: 0 }}></i></div>}
            {record.IsEdit && <div className="mx-2 pt-1" style={{ cursor: 'pointer' }} title={"Save"} onClick={() => { this.updateRecord(record); this.setState({descr:''}) }}><i style={{fontSize:17}} className={this.state.updating ? "fa fa-spin fa-spinner" : "fa fa-save"}></i></div>}
            {record.IsEdit && <div className="mx-2 pt-1" style={{ cursor: 'pointer' }} title={"Cancel"} onClick={() => { this.doCancel(record); this.setState({descr:''}) }}><i style={{fontSize:17}} className="fa fa-times"></i></div>}
              </div>
      }
      
    ];

enter image description here

I have a editable column in my grid.But if there is no value from the backend in some cases it shows an empty options. If it has values, no issues there. How to remove that empty value? I am using antd.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潜移默化 2025-02-13 20:48:44

如果this.state.descr没有任何值,请将其null。

If this.state.descr doesn't have any value, make it null.

錯遇了你 2025-02-13 20:48:44

也许您可以在this.state.length&gt;的渲染方法中添加条件渲染。 0

Maybe you can add a conditional rendering in your render method for this.state.length > 0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文