我目前正在一家在線商店工作,該商店根據(jù)某些標準(例如尺寸、庫存、性別等)過濾產(chǎn)品。雖然我已經(jīng)能夠在一定程度上讓它發(fā)揮作用。我的程序目前按尺寸、性別、按價格排序等進行過濾。但是,我無法按品牌過濾。出于某種原因,一旦我點擊該品牌,我就可以過濾一次該功能,但是,一旦我點擊另一個品牌,該特定品牌的過濾器就不會運行。這是代碼沙箱的鏈接:https://codesandbox.io/s/mystifying-roentgen-7mp0t我目前堅持按品牌過濾,我嘗試通過檢查品牌是否包含在項目中并使用 localeCompare() 將過濾結(jié)果與點擊項目的狀態(tài)進行比較。這是代碼沙箱的鏈接:https://codesandbox.io/s/mystifying-roentgen-7mp0tcreateCheckboxes = () => available_sizes.map(this.createCheckbox); handleFormSubmit = event => { //4) this button updates the filters on the sizes, which I think I need to fix to update the brands, the price and the gender event.preventDefault(); //5) right here I am storing the selected checkboxes which is what I was doing before by pushing the checkboxes const selectedSizes = [...this.selectedCheckboxes]; const shallowCopy = [...this.state.filteredProducts]; let filteredProducts = shallowCopy.filter(product => selectedSizes.every(size => product.stock.some(s => s.stock > 0 && s.size === size) ) ); let filteredGender = filteredProducts.filter(product => { return product.gender.some((item, idx, arr) => { return item[this.selectedGender] === false ? null : product; }); }); //***this is the function that is not currently running***// let filteredData = filteredGender.filter(product => { //console.log(product.brand.includes(this.state.activeBrand)) //console.log(product.brand = this.state.brand) return product.brand.includes(this.state.activeBrand) }); let sortedPrice = filteredData.sort((a, b) => { return this.state.sortBy === "min" ? a.price - b.price : b.price - a.price; }); this.setState({ filteredProducts: sortedPrice }); };一旦點擊了一個項目,我希望能夠通過這個功能按品牌進行過濾。這是代碼沙箱的鏈接:https://codesandbox.io/s/mystifying-roentgen-7mp0t
如何通過反應(yīng)動態(tài)處理大量過濾器?
拉風的咖菲貓
2021-12-02 19:30:02