checkBoxFilter() {
//拼一個拉姆達表達式
var filterStr = "item=> item != undefined";
if (this.isTransitGroup.length > 0) {
filterStr +=
"&& this.isTransitGroup.indexOf(item.info.isTransit) != -1";
}
if (this.etdGroup.length > 0) {
filterStr += "&& this.etdGroup.indexOf(item.info.etdWeek) != -1";
}
if (this.etaGroup.length > 0) {
filterStr += "&& this.etaGroup.indexOf(item.info.etaWeek) != -1";
}
if (this.companyGroup.length > 0) {
filterStr +=
"&& this.companyGroup.indexOf(item.info.carrierName) != -1";
}
if (this.polGroup.length > 0) {
if (this.polGroup.indexOf('全部') == -1) {
filterStr += "&& this.polGroup.indexOf(item.info.polTerninal) != -1 ";
} else {
filterStr +=
"&& (this.polGroup.indexOf(item.info.polTerninal) != -1 || item.info.polTerninal == '')";
}
}
if (this.podGroup.length > 0) {
if (this.podGroup.indexOf('全部') == -1) {
filterStr += "&& this.podGroup.indexOf(item.info.podTerninal) != -1";
} else {
filterStr +=
"&& (this.podGroup.indexOf(item.info.podTerninal) != -1 || item.info.podTerninal == '')";
}
}
if (this.range > 0) {
filterStr += "&& this.range >= item.info.totalDuration";
}
this.scheduleList = this.tempList.filter(eval(filterStr));
this.total = this.scheduleList.length;
},
代碼沒什麼難度,主要分享一下我的思路
要篩選的字段很多,而且不一定篩選哪一個字段,開始就是各種if各種循環,自己看的都覺得low
於是換了一種方法,用js數組的filter函數,這裏就不講filter怎麼回事了,
這裏的if條件就是每種字段選中的長度,如果選了就添加一個條件
這裏我用的是filter參數直接放了一個 Lambda表達式,表達式的內容是根據條件拼的
拼完以後用eval將字符串執行成一個表達式供filter使用即可