【问题标题】:Searching for substring knockout js搜索子字符串淘汰js
【发布时间】:2018-10-20 02:03:46
【问题描述】:

我正在尝试过滤一个数组并检查我的数组中的任何标题是否包含用户正在搜索的内容。但是,当我运行该函数时,它似乎只检查搜索是否与标题完全匹配,而不是是否包含子字符串。

JS

var viewModel = function() {
var self = this;
self.filter = ko.observable('');
self.locationList = ko.observableArray(model);
self.filterList = function(){

    return ko.utils.arrayFilter(model, function(location) {

       if(self.filter().includes(location.title)){
          console.log(location.title)
       }

    });
  };
}

HTML

<div class="col-lg-12">
  <div class="input-group">
    <input data-bind="textInput: filter, event:{keyup: filterList}"
    type="text" class="form-control" placeholder="Filter Places"
    aria-describedby="basic-addon2" id="test">
    <button id="basic-addon2">Filter</button>
   </div>
 </div>

【问题讨论】:

    标签: javascript html knockout.js data-binding


    【解决方案1】:

    问题出在这部分if(self.filter().includes(location.title)){

    在这里,您正在检查搜索的键是否包含数组中的标题之一。相反,您应该检查其他情况

    if(location.title.includes(self.filter())){
    

    您可以为此使用简单的.filter.includes

    var filterdList = self.locationList.filter(e=> e.includes(self.filter()))
    

    【讨论】:

    • 这就是我刚刚所做的,它似乎正在工作,谢谢您的回复。
    • 很高兴知道这一点。如果它解决了您的问题,请考虑接受并支持答案:)
    • 得等到支票
    猜你喜欢
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多