简体   繁体   English

在 MaterialUI 和 React.js 中为自动完成分配默认值

[英]Assigning default value to Autocomplete in MaterialUI and React.js

I would like to display default value to my Autocomplete TextField component from Material UI in React.js.我想从 React.js 中的 Material UI 向我的自动完成 TextField 组件显示默认值。 A pre-populated value that is automatically loaded from the users' profile and that can be changed with another one from the list.一个预填充的值,自动从用户的配置文件中加载,并且可以用列表中的另一个值进行更改。

Here is my code:这是我的代码:

import React from 'react';
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';

export const ComboBox =() => {
  return (
    <Autocomplete
      id="combo-box-demo"
      options={top100Films}
      getOptionLabel={(option) => option.title}
      style={{ width: 300 }}
      renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
    />
  );
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },
  { title: 'The Godfather: Part II', year: 1974 },
  { title: 'The Dark Knight', year: 2008 } 
]

I only see the input field with the label on it.我只看到带有 label 的输入字段。 defaultValue is listed as an API of both TextField and Autocomplete and I also tried to move it directly under Autocomplete. defaultValue 被列为 TextField 和 Autocomplete 的 API,我还尝试将其直接移动到 Autocomplete 下。 Still not working.还是行不通。

your defaultValue of <AutoComplete /> should have the same format as your options , the getOptionLable() is used to form the label even from your defaultValue .您的<AutoComplete />defaultValue应该与您的options具有相同的格式,即使从您的defaultValue也可以使用getOptionLable()来形成 label 。

in your code title property of the string is undefined , so nothing is shown.在您的代码中,字符串的title属性是undefined ,因此没有显示任何内容。

this should work fine:这应该可以正常工作:

<Autocomplete
    id="combo-box-demo"
    options={top100Films}
    defaultValue={{ title: "The Godfather", year: 1972 }}
    getOptionLabel={(option) => option.title}
    style={{ width: 300 }}
    renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>

You can use the defaultValue option like this:您可以像这样使用defaultValue选项:

<Autocomplete
    id="combo-box-demo"
    options={top100Films}
    defaultValue={ top100Films[0] }
    getOptionLabel={(option) => option.title}
    style={{ width: 300 }}
    renderInput={(params) => <TextField {...params} label="Combo box" defaultValue="The Godfather" variant="outlined" />}
/>

If you use multiple so you should set defaultValue in list like blow如果你使用多个,那么你应该像下面这样在列表中设置 defaultValue

<Autocomplete multiple id="tags-standard" options={categoryList}
    defaultValue={[{ title: "cat1", id: 1 }, { title: "cat2", id: 2 }]}

    getOptionLabel={(option) => option.title}
    renderInput={(params) => (
    <TextField {...params} variant="standard" label="Category" />
    )}
    onChange={handleCategory}
                            
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM