[英]How to filter data based on selected value from dropdown menu and search query in input field using reactjs?
i want to filter data based on the search query entered in input field from the values selected in dropdown menu. 我想根据在下拉菜单中选择的值在输入字段中输入的搜索查询来过滤数据。
What i am trying to do? 我想做什么? Consider the image below
考虑下面的图片
From the select dropdown option user can select both messages and info or either of the options. 从选择下拉选项中,用户可以选择消息和信息或任一选项。 Based on the option selected from dropdown menu and search query entered in the search input field it should filter data.
根据从搜索输入字段中输入的下拉菜单和搜索查询中选择的选项,它应该过滤数据。
Suppose if user selected messages and entered search query "hello" it should retrieve messages containing text "hello" and similarly with info and messages option as well. 假设用户选择了消息并输入了搜索查询“hello”,它应该检索包含文本“hello”的消息,同样也可以检索信息和消息选项。
I am not sure how to do this. 我不知道该怎么做。 Could someone help me solve this?
有人可以帮我解决这个问题吗?
Below is the code, 下面是代码,
<div className='wrapper'>
<div>
{!state.expanded && <Svgsearch/>}
{state.expanded && props.active && <div onClick=
{this.collapse_input}><Svgsearch/></div>}
{state.expanded &&
<div className="search_input">
<input type="text" placeholder="search query" />
</div>}
<div className="search_dropdown">
<FieldDropdown on_dropdown_toggle=
{this.handle_dropdown_toggle} />
</div>
</div>
</div>);
export default class FieldDropdown extends react.component {
render = () => {
return (
<Dropdown className="category_dropdown" on_dropdown_open=
{this.handle_dropdown_open} on_dropdown_close=
{this.handle_dropdown_close}>
<div>
<button>{dropdown_text}</button>
</div>
{state.options.map((option, i) => {
return (
<DropdownItem key={i} on_select=
{this.handle_option_selection} value={i}>
<input type="checkbox" value={option.value}
checked="true" readOnly />
<span>
{option.text}</span>
</DropdownItem>)
})}
</Dropdown>);
};
Consider i have messages and info in an array of objects. 考虑我在一个对象数组中有消息和信息。
How can i write a method to filter data based on option selected from dropdown. 如何根据从下拉列表中选择的选项编写方法来过滤数据。
Thanks. 谢谢。
Html for query field 查询字段的Html
<input
id="searchId"
type="text"
value={this.state.queryValue}
onChange={this.handleTextChange}
/>
State 州
state = { data: [], filteredData:[],queryValue: '' ,value:'' };
Event Handling Method 事件处理方法
handleTextChange = event => {
//read the value of the textbox with event.target.value
const filteredData = this.state.data.filter(d =>
// object where condition based on value(options value) & queryValue
);
this.setState({ queryValue: event.target.value, filteredData});
};
and bind UI to the filteredData. 并将UI绑定到filteredData。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.