簡體   English   中英

將過濾器應用於列表並顯示數據

[英]Apply filters to a list and show data

前端:

 const [searchParameters, setSearchParameters] = useState({
    type: "",
    country:"",
    
  });

  const onChangeSearchType = e => {
    const workingObject = {...searchParameters};
    workingObject.searchType = e.target.value; 
    setSearchParameters(workingObject);   
  };

  const onChangeSearchCountry = e => {
    const workingObject = {...searchParameters};
    workingObject.searchCountry = e.target.value; 
    setSearchParameters(workingObject);
  };


const handleFetchWithSearchParameters = () => {
    TutorialDataService.findByParameters(searchParameters)       
      .then(response => { 
        setTutorials(response.data); 
        console.log(response.data);       
      })       
      .catch(e => { 
        console.log(e);       
      });  
  }

return()

<Form.Control as="select" defaultValue=""
            type="text"
            className="form-control"
            id="country"
            required
            value={searchParameters.country}
            onChange={onChangeSearchCountry}
            name="country">
            <option>Nigeria</option>
            <option>Ghana</option>
            <option>Kenya</option>
            <option>Senegal</option>
                 </Form.Control>
                 <Form.Control as="select" defaultValue=""
            type="text"
            className="form-control"
            id="type"
            required
            value={searchParameters.type}
            onChange={onChangeSearchType}
            name="type">
            <option>Agricultural</option>
            <option>Manufacturing</option>
            <option>Industrial</option>
            <option>Livestock</option>
            <option>Service Industry</option>
                 </Form.Control>
 <div className="input-group-append">
<button 
className="btn btn-outline-secondary" 
type="button" 
onClick={handleFetchWithSearchParameters}
       Search 
</button>

服務.js :

import http from "../http-common.js";
const findByParameters = searchParameters => {
  // This is the destructuring syntax I've linked above
  const { type, country, creditscore, interest } = searchParameters;

  // Here we use & ampersand to concatinate URL parameters
  return http.get(`/tutorials?type=${type}&country=${country}&creditscore=${creditscore}&interest=${interest}`); 
}; 

export default {
 
  findByParameters
};

控制器.js

// Retrieve all Industries from the database.
exports.findAll = (req, res) => { 
const type = req.query.type ; 
let condition = type ? { type : { [Op.like]: %${type }% } } : null;

Tutorial.findAll({ 
where: condition, 
order: [   ['createdAt', 'DESC'] ] 
})     
.then(data => { res.send(data);     
})     
.catch(err => { 
res.status(500).send({ message:err.message || "Some error occurred while retrieving tutorials."
       });     
}); };

因此,我的網絡應用程序的這個頁面用於顯示保存在我的數據庫中的所有公司的列表。

我創建了一個過濾器,允許您通過findByType僅顯示特定類型的過濾器。

我想插入其他過濾器,例如: findByRevenuefindByEmployeesNumber

我不知道我是否應該為每種情況在前端和后端編寫新函數? 或者有更聰明的方法嗎?

此外,過濾器不必單獨工作,它們還需要組合在一起以改進您的搜索。 我希望我已經很好地解釋了它應該如何工作,就像任何電子商務網站一樣。

編輯:我按照建議更改了代碼,但我仍然遇到問題。 它不再讓我使用輸入表單。 事實上,請求是空的,例如:

type = ""
country = ""

我想我的input.value =

只是一個意見:我會稍微修改前端和后端以支持組合請求。 您可以使用不同的參數將 JavaScript 對象(作為 JSON)發送到您的 API,並在后端控制器函數中應用檢查。

所以基本上,而不是分開

 const findByType = () => {...}
    const findByRevenue = () => {...}
    const findByEmployeesNumber = () => {...}
   

我會使用(狀態可以是一個單一的對象,就像下面的例子一樣,或者在發送到 API 時分離然后組裝成一個對象)

   const [searchParameters, setSearchParameters] = useState({
        type: '',
        revenue: '',
        employeesNumber: ''
      });
    
    const onChangeSearchType = e => { 
      const workingObject = {...searchParameters};
      const workingObject.searchType = e.target.value; 
      setSearchParameters(workingObject);   
    };
    
    // same logic for onChangeRevenue and onChangeEmployeesNumber
    
    const handleFetchWithSearchParameters = () => {
      TutorialDataService.findByParameters(searchParameters)       
        .then(response => { 
          setTutorials(response.data); 
          console.log(response.data);       
        })       
        .catch(e => { 
          console.log(e);       
        });  
    }

然后在控制器中,我會破壞查詢對象並針對它運行查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM