1.商品列表搜索框

2.搜索框頁面的結構為
-
<div className="row search-wrap">
-
<div className="col-md-12">
-
<div className="form-inline">
-
<div className="form-group">
-
<select className="form-control"
-
name="searchType"
-
onChange={(e) => this.onValueChange(e)}>
-
<option value="productId">按商品ID查詢</option>
-
<option value="productName">按商品名稱查詢</option>
-
</select>
-
</div>
-
<div className="form-group">
-
<input type="text"
-
className="form-control"
-
placeholder="關鍵詞"
-
name="searchKeyword"
-
onKeyUp={(e) => this.onSearchKeywordKeyUp(e)}
-
onChange={(e) => this.onValueChange(e)}/>
-
</div>
-
<button className="btn btn-primary"
-
onClick={(e) => this.onSearch()}>搜索</button>
-
</div>
-
</div>
-
</div>
3. State里邊定義數據
-
this.state = {
-
searchType : 'productId',
-
searchKeyword : ' '
-
}
分別給select標簽和input輸入框設置 onChange事件,監聽選擇框和輸入框的變化,變化時執行onValueChange函數,傳入參數e, 更state里邊searchType和searchkeyword的值
-
// 數據變化的時候
-
onValueChange(e){
-
let name = e.target.name,
-
value = e.target.value.trim();
-
console.log(name)
-
console.log(value)
-
this.setState({
-
[name] : value
-
});
-
}
監聽鍵盤事件onKeyUp,當按下enter鍵時觸發
-
// 輸入關鍵字后按回車,自動提交
-
onSearchKeywordKeyUp(e){
-
if(e.keyCode === 13){
-
this.onSearch();
-
}
-
}
4.點擊查詢的時候,執行onSearch()函數, onSearch函數在商品列表組件里邊定義,通過prop參數傳過來,listSearch組件提供searchType和searchkeyword即可
-
// 點擊搜索按鈕的時候
-
onSearch(){
-
this.props.onSearch(this.state.searchType, this.state.searchKeyword);
-
}
l istSearch組件
-
<ListSearch onSearch={(searchType, searchKeyword) => {this.onSearch(searchType, searchKeyword)}} />
onSearch()函數
-
//搜索
-
onSearch(searchType, searchKeyword){
-
let listType=searchKeyword ==='' ? 'list': 'search';
-
this.setState({
-
listType : listType,
-
pageNum : 1,
-
searchType : searchType,
-
searchKeyword : searchKeyword
-
}, () => {
-
this.loadProductList();
-
})
-
}
