gpt4 book ai didi

javascript - 重构 ValueConverter 以动态过滤

转载 作者:行者123 更新时间:2023-11-27 22:33:48 26 4
gpt4 key购买 nike

我想采用以下 ValueConverter 并使其可重用于许多 View 。

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
);
}
}

上面的过滤器从文本字段中获取一个值,并与我的“缩写”列或“名称”列相匹配。

为了使其可重用而进行重构的第一步是添加一个附加参数,该参数可能是一个字符串数组,其中包含我想要包含在匹配 OR 逻辑中的列。

如果我在数组中传入一个值,例如 ["zip"],那么我只想匹配我的邮政编码列,并且不需要 OR 运算符。如果像上面的情况一样,我想匹配 m,y 产品,也许我的数组看起来像:["abbr","name"]

上面的方法或可能定义了另一个对象,该对象知道所有不同的对象列,我们可以将其用作查找:

var lookup = {
products: ["abbr","name"],
zipcodes: ["zip"],
offices: ["city", "state", "zipcode"]
}

我预计这个 ValueConverter 将用作我的新网站中许多内容的过滤器。 IT 最好在任何对象上使用它,并传递一个搜索术语作为第二个参数和一个列名列表作为第三个参数进行匹配,并使用 && 和/或进行条件搜索||。哇,最后一点令人困惑。

下面是我迄今为止在 View 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!

最佳答案

这是我想出的解决方案,因为我没有运气使用 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;
}
}

查看.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"];
}

关于javascript - 重构 ValueConverter 以动态过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com