【问题标题】:How to remove selected value when click button of Autocomplete Material UI?单击自动完成材质 UI 的按钮时如何删除选定的值?
【发布时间】:2019-12-01 15:29:21
【问题描述】:

我正在使用 material-ui 的自动完成功能,并尝试在单击按钮时删除选定的值,但找不到任何方法来做到这一点。有什么想法吗?

            <Autocomplete
              className={classes.techListBox}
              disableCloseOnSelect={true}
              multiple
              options={this.props.displayProject.techList}
              getOptionLabel={options => options.title}
              defaultValue={this.props.displayProject.techName}
              onChange={(e, techs) => {
                this.formatTechID(techs);
              }}
              renderInput={params => (
                <TextField
                  {...params}
                  variant="outlined"
                  placeholder={t("tech")}
                  margin="normal"
                  fullWidth
                />
              )}
            ></Autocomplete>```

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    您需要在自动完成时设置值(状态)和 onChange 事件:) 当您单击休息按钮时,它只会重置状态:)

       const [value, setValue] = React.useState(null);
       <Autocomplete 
        value={value}
        onChange={(event, newValue) => {
          setValue(newValue);
        }}
       >
    
       <button onClick={() => setValue(null)}>Reset autocomplete</button>
    

    我为你做了一个工作演示:https://codesandbox.io/s/material-demo-zqz4v

    评论更多问题:)

    【讨论】:

    • 在选择多个时,我不认为这是工作。我存储了选择值,我可以将存储设置为空,但不能在屏幕中将其删除。我怎样才能做到这一点。对不起我的英语
    • 它也适用于多个 :) 但现在你将数组而不是基本变量。单击我提供的完整演示的链接,您可以在其中删除它的示例;)随时 UPVOTE + 接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2021-11-10
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多