如何将级联下拉列表的选定值绑定到 Ant Design 中的 h1 标签?

发布于 2025-01-16 21:11:08 字数 1406 浏览 2 评论 0原文

我正在使用 Ant Design 创建一个级联下拉列表。我希望将所选值作为标签返回,以显示“您选择了 {name}”之类的内容。如何将所选值绑定到 h1 标签?这是到目前为止的代码:

    const options = [
      {
        value: 'Person',
        label: 'person',
        children: [
          {
            value: 'amy',
            label: 'Amy',
          },
          {
           value: 'john',
           label: 'John'
          }
        ],
      }
    ];
    
    function onChange(value, selectedOptions) {
      console.log(value, selectedOptions);
    }
    
    function filter(inputValue, path) {
      return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
    }  
    
    class CascadingDropdown extends Component{
      render(){
            return(
                <div>
                    <p>Please select a person:</p>
                  <div>
                  <Cascader
                      options={options}
                      onChange={onChange}
                      placeholder="Please select"
                      showSearch={{ filter }}
                      onSearch={value => console.log(value)}
                    />
                  </div>
                  <h1>You selected {name}</h1> // here is where I want to print the name
                </div>
            );
        }
    }
    
    export default CascadingDropdown;

I am using Ant Design to create a cascading dropdown. I am looking to return the selected value as a tag to display something like "You selected {name}". How can I bind the selected value to the h1 tag? Here is the code so far:

    const options = [
      {
        value: 'Person',
        label: 'person',
        children: [
          {
            value: 'amy',
            label: 'Amy',
          },
          {
           value: 'john',
           label: 'John'
          }
        ],
      }
    ];
    
    function onChange(value, selectedOptions) {
      console.log(value, selectedOptions);
    }
    
    function filter(inputValue, path) {
      return path.some(option => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1);
    }  
    
    class CascadingDropdown extends Component{
      render(){
            return(
                <div>
                    <p>Please select a person:</p>
                  <div>
                  <Cascader
                      options={options}
                      onChange={onChange}
                      placeholder="Please select"
                      showSearch={{ filter }}
                      onSearch={value => console.log(value)}
                    />
                  </div>
                  <h1>You selected {name}</h1> // here is where I want to print the name
                </div>
            );
        }
    }
    
    export default CascadingDropdown;

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

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

发布评论

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

评论(1

似梦非梦 2025-01-23 21:11:08

使用可以使用状态来显示所选值,如以下代码

const options = [
  {
    value: 'Person',
    label: 'person',
    children: [
      {
        value: 'amy',
        label: 'Amy',
      },
      {
        value: 'john',
        label: 'John',
      },
    ],
  },
];

class CascadingDropdown extends Component {
  onChange(value, selectedOptions) {
    console.log(value, selectedOptions);
    if (value != undefined) {
      this.setState({ name: value[1].toString() });
    }
  }

  filter(inputValue, path) {
    return path.some(
      (option) =>
        option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
    );
  }

  constructor(props) {
    super(props);
    this.state = {
      name: '',
    };
  }
  render() {
    return (
      <div>
        <p>Please select a person:</p>
        <div>
          <Cascader
            options={options}
            onChange={this.onChange.bind(this)}
            placeholder="Please select"
            showSearch={this.filter}
            onSearch={(value) => console.log(value)}
          />
        </div>
        <h1>You selected {this.state.name}</h1> 
      </div>
    );
  }
}

截图所示:

屏幕截图

Use can use state to display the selected value as shown in the following code

const options = [
  {
    value: 'Person',
    label: 'person',
    children: [
      {
        value: 'amy',
        label: 'Amy',
      },
      {
        value: 'john',
        label: 'John',
      },
    ],
  },
];

class CascadingDropdown extends Component {
  onChange(value, selectedOptions) {
    console.log(value, selectedOptions);
    if (value != undefined) {
      this.setState({ name: value[1].toString() });
    }
  }

  filter(inputValue, path) {
    return path.some(
      (option) =>
        option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
    );
  }

  constructor(props) {
    super(props);
    this.state = {
      name: '',
    };
  }
  render() {
    return (
      <div>
        <p>Please select a person:</p>
        <div>
          <Cascader
            options={options}
            onChange={this.onChange.bind(this)}
            placeholder="Please select"
            showSearch={this.filter}
            onSearch={(value) => console.log(value)}
          />
        </div>
        <h1>You selected {this.state.name}</h1> 
      </div>
    );
  }
}

Screenshot:

Screenshot

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