es6條件查詢篩選數據


引用場景如下,列表查詢時需要用到前端查詢

 

 具體實現方法如下

前面定義的變量是查詢條件,this.allData表示被篩選的數據,filteData是查詢結果

    queryData () {
      let officerType = this.search.officerType
      let officerName = this.search.officerName
      let position = this.search.position
      let filterData = this.allData.filter( item => {
        let isOfficerType = officerType.length > 0 && item.officerType.indexOf(officerType) === -1 ? false : true
        let isOfficerName = officerName.length > 0 && item.officerName.indexOf(officerName) === -1 ? false : true
        let isPosition = position.length > 0 && item.position.indexOf(position) === -1 ? false : true
        //返回篩選條件
        return isOfficerType && isOfficerName && isPosition
      })
      this.officerData = filterData
    },

因為有多個地方需要用到前端查詢,我們可以進行封裝

在util.js里面

// 前端查詢
let queryData = function (query, allData) {
  //數據格式
  // let query = [
  //   {
  //     key: 'officerType',
  //     value: this.search.officerType
  //   },{
  //     key: 'officerName',
  //     value: this.search.officerName
  //   }
  // ]
  let filterData = allData.filter( item => {
    return query.every( innerItem => {
      return innerItem.value.length > 0 && item[innerItem.key].indexOf(innerItem.value) === -1 ? false : true 
    })
  })
  return filterData
}

組件里面調用

    // 前端查詢列表
 queryData () {
     // 查詢參數
      let query = [
        {
          key: 'teamType',
          value: this.search.teamType
        },{
          key: 'teamName',
          value: this.search.teamName
        }
      ]
      let allData = this.allData  // 被篩選數據
      this.teamData = this.$util.queryData(query, allData)
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM