我想使用下面的ValueConverter,并使它对许多视图都可重用。
export class ProductFilterValueConverter {
toView(array, value) {
var regex = new RegExp(value, 'gi');
var matcher = item =>
item.abbr.match(regex) || item.name.match(regex);
return array.filter(
matcher
);
}
}上面的过滤器接受文本字段中的值,并匹配我的'abbr‘列或我的'name’列。
重构使此可重用的第一步是添加一个额外的参数,它可能是一个字符串数组,其中包含了我想要包含在匹配或逻辑中的列。
如果我在数组中传入一个值,比如["zip"],那么我只想匹配我的zipcode列,就不需要OR操作符了。如果像上面的情况一样,我想要匹配m,y产品,也许我的数组看起来是:["abbr","name"]。
这个方法上面或者可能定义了另一个对象,它知道所有不同的对象列,我们可以使用它作为查找:
var lookup = {
products: ["abbr","name"],
zipcodes: ["zip"],
offices: ["city", "state", "zipcode"]
}我预见到这个ValueConverter将被用作我的新站点中的许多东西的过滤器。最好在任何对象上使用它,并传递一个serch项作为第二个参数和一个作为第三个参数匹配的列名列表,并让它在&&和/或||中有条件地搜索。哇,最后一点令人困惑。
下面是我到目前为止对视图html代码和过滤器的js的看法,我很难做到这一点:
数据(我们正在过滤的实用程序对象)
[
{
"id": 1,
"utilityName": "Big Valley",
"abbr": "Big Valley",
"city": "Big Bear Lake"
},
{
"id": 2,
"utilityName": "Pac Electric",
"abbr": "PE",
"city": "Los Angelas"
}
]GenericFilter.js
export class GenericFilterValueConverter {
toView(array, value, cols) {
var columns = cols;
var matchLogic = "item => {";
var regex = new RegExp(value, 'gi');
//debugger;
for (var i = 1; i <= columns.length; i++) {
matchLogic += "item." + columns[i] + ".match(regex)";
matchLogic += (i < columns.length) ? " || " : "";
matchLogic += (i === columns.length ? "}" : "");
}
var matcher = eval(matchLogic);
return array.filter(
matcher
);
}
}view.js
export class Utilities {
constructor(...) {
//below the definedColumns are defined in the js module
this.definedColumns = ["abbr","utilityName"];
}view.html
<template>
<!--<require from="./officeFilter"></require>-->
<require from="../services/genericFilter"></require>
<input type="text" name="searchValue" value.bind="searchValue" />
<div class="grid" repeat.for="office of offices | genericFilter:searchValue:definedColumns">
<!--MORE HTML HERE TO RENDER GRID-->
</div>
</div>
</template>目前我没有得到一个JavaScript错误,但我的过滤器也不再工作。我的页面上没有任何结果,就好像ValueConverter第一次运行,而array.filter过滤掉了所有的结果可能吗?
显然,使用eval的想法并不是最伟大的,我想出的答案是,下面的答案并不是使用邪恶的eval!
发布于 2016-09-05 08:02:42
下面是我想出的解决方案,因为我在用eval()构建逻辑OR语句时没有运气:
GenericFilter.js
export class GenericFilterValueConverter {
toView(array, value, cols, showResults=false) {
if (!value) {
return showResults ? array : [];
};
var filteredArray = array.filter(
function(objArray) {
for (var col in objArray) {
if (objArray.hasOwnProperty(col)) {
if (cols.indexOf(col) != -1 && objArray[col].toLowerCase().indexOf(value.toLowerCase()) != -1) {
return true;
}
}
};
return false;
});
return filteredArray;
}
}view.html
<template>
<require from="../services/genericFilter"></require>
<input type="text" name="searchValue" value.bind="searchValue"/>
<div class="grid" repeat.for="utility of utilities | genericFilter:searchValue:definedColumns:true">
<!--template repeats for each utility-->
</div>
</div>
</div>
</template>view.js
export class Utilities {
constructor(utilityNameData, router) {
this.data = utilityNameData;
this.router = router;
this.utilities = [];
//...
this.definedColumns = ["abbr","utilityName"];
}https://stackoverflow.com/questions/39323948
复制相似问题