<i id='PmAhJ'><tr id='PmAhJ'><dt id='PmAhJ'><q id='PmAhJ'><span id='PmAhJ'><b id='PmAhJ'><form id='PmAhJ'><ins id='PmAhJ'></ins><ul id='PmAhJ'></ul><sub id='PmAhJ'></sub></form><legend id='PmAhJ'></legend><bdo id='PmAhJ'><pre id='PmAhJ'><center id='PmAhJ'></center></pre></bdo></b><th id='PmAhJ'></th></span></q></dt></tr></i><div id='PmAhJ'><tfoot id='PmAhJ'></tfoot><dl id='PmAhJ'><fieldset id='PmAhJ'></fieldset></dl></div>

    <small id='PmAhJ'></small><noframes id='PmAhJ'>

    <legend id='PmAhJ'><style id='PmAhJ'><dir id='PmAhJ'><q id='PmAhJ'></q></dir></style></legend>
    • <bdo id='PmAhJ'></bdo><ul id='PmAhJ'></ul>

    1. <tfoot id='PmAhJ'></tfoot>

      1. Angular 6 多场过滤器

        Angular 6 multi field filter(Angular 6 多场过滤器)

        <i id='GYNpB'><tr id='GYNpB'><dt id='GYNpB'><q id='GYNpB'><span id='GYNpB'><b id='GYNpB'><form id='GYNpB'><ins id='GYNpB'></ins><ul id='GYNpB'></ul><sub id='GYNpB'></sub></form><legend id='GYNpB'></legend><bdo id='GYNpB'><pre id='GYNpB'><center id='GYNpB'></center></pre></bdo></b><th id='GYNpB'></th></span></q></dt></tr></i><div id='GYNpB'><tfoot id='GYNpB'></tfoot><dl id='GYNpB'><fieldset id='GYNpB'></fieldset></dl></div>
        <tfoot id='GYNpB'></tfoot><legend id='GYNpB'><style id='GYNpB'><dir id='GYNpB'><q id='GYNpB'></q></dir></style></legend>

          • <small id='GYNpB'></small><noframes id='GYNpB'>

                <tbody id='GYNpB'></tbody>
              • <bdo id='GYNpB'></bdo><ul id='GYNpB'></ul>

                  本文介绍了Angular 6 多场过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我目前正在开发 Angular 6 应用程序.我有一个庞大的文章列表,想对其进行一些过滤.现在它是实时工作的,没有提交过滤器选项的按钮,这一切都在你输入时发生.我想出了一个办法,但它仍然有一些我可以解决的问题,但不喜欢我这样做的方式.我敢肯定,一定有更优雅的东西.

                  I'm currently working on angular 6 app. I have a huge list of articles and want to do some filtering upon it. Right now it works real-time, there is no button to submit your filter options, it all happens as you type. I figure a way, but it has still some issues I can fix, but doesn't like the way I did it. I'm sure, there has to be something more elegant.

                  为了更好的想象,这些文章有类别、标题、作者、标签.我可以根据类别过滤它们,比如说……但我想做某种过滤.

                  For better imagination those articles have category, title, author, tags. I am able to filter them according to category lets say... but I want to do some kind of filtering.

                  示例:过滤体育"类别中的所有文章,然后从已过滤的数组中过滤标题中具有子字符串目标"的所有文章,然后过滤作者为约翰"的所有文章,然后过滤所有带有标签曲棍球"的文章.

                  Example: Filter all articles from category 'sports', then filter all articles having substring 'goal' in title from that already filtered array, then filter those whos author is 'john' and then filter all with tag 'hockey'.

                  我最终得到了大量的 IF 语句,这不是我想说的正确方法.你能推荐我一些更好的方法吗?

                  I ended up with huge amount of IF statements, which is not the right approach I would say. Can you please recommend me some better way to do this?

                  推荐答案

                  你可以这样做来创建一个没有很多 if 语句的多重过滤器.

                  This is what you could do to create a multifilter without many if statements.

                  第一步:创建一个执行各种比较功能的对象:

                  Step one: Create an object that performs various comparison functions:

                  let compareFunctions = {
                      equal: function(a,b) {
                          return a === b;
                      },
                      in: function(a,b){
                          return a.indexOf(b) !== -1
                      }
                  }
                  

                  第二步:创建一个具有以下参数的函数:

                  Step two: Create a function that has the following parameters:

                  1. key - 要过滤的记录字段的键.
                  2. value - 您要过滤的值
                  3. compareFn - 用于该字段的比较函数

                  这个函数返回一个执行条件的函数.

                  This function returns a function that executes the condition.

                  function condition(key, value, comparFn = compareFunctions.equal) {
                      return function(data) {
                          return comparFn(data[key],value);
                      }
                  }
                  

                  第三步:使用代表过滤器值的条件"函数创建一个数组:

                  Step three: Create an array with "condition" functions representing your filter values:

                  let filterArray = [
                      condition('category', 'sports'),
                      condition('title', 'goal', compareFunctions.in),
                      condition('author', 'john'),
                      condition('tags', 'hokey', compareFunctions.in),
                  ]
                  

                  第四步:通过为每个条目调用条件函数数组并评估每个条件的结果来过滤记录:

                  Step four: Filter your record by calling your array of conditions functions for each entry and evaluating the result of each condition:

                  let result = dataset.filter(y => {
                      let resolved = filterArray.map(x => x(y))
                      return resolved.every(x => x === true);
                  })
                  

                  完整示例代码:

                  let compareFunctions = {
                      equal: function(a,b) {
                          return a === b;
                      },
                      in: function(a,b){
                          return a.indexOf(b) !== -1
                      }
                  }
                  
                  function condition(key, value, comparFn = compareFunctions.equal) {
                      return function(data) {
                          return comparFn(data[key],value);
                      }
                  }
                  
                  let dataset = [
                      {
                          category: "sports",
                          title: "goal goal goal",
                          author: "john",
                          tags: ["hokey", "ice-hokey"]
                      },
                      {
                          category: "news",
                          title: "bla bla",
                          author: "Timo",
                          tags: ["news"]
                      },
                      {
                          category: "news",
                          title: "blub blub",
                          author: "alex",
                          tags: ["hokey", "ice-hokey"]
                      },
                      {
                          category: "sports",
                          title: "Klner Haie bla bla",
                          author: "Timo",
                          tags: ["hokey", "ice-hokey"]
                      }
                  ]
                  
                  let filterArray = [
                      condition('category', 'sports'),
                      condition('title', 'goal', compareFunctions.in),
                      condition('author', 'john'),
                      condition('tags', 'hokey', compareFunctions.in),
                  ]
                  
                  //console.log(filterArray)
                  
                  let result = dataset.filter(y => {
                      let resolved = filterArray.map(x => x(y))
                      return resolved.every(x => x === true);
                  })
                  
                  console.log(result)
                  

                  这篇关于Angular 6 多场过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                1. <i id='GwXOt'><tr id='GwXOt'><dt id='GwXOt'><q id='GwXOt'><span id='GwXOt'><b id='GwXOt'><form id='GwXOt'><ins id='GwXOt'></ins><ul id='GwXOt'></ul><sub id='GwXOt'></sub></form><legend id='GwXOt'></legend><bdo id='GwXOt'><pre id='GwXOt'><center id='GwXOt'></center></pre></bdo></b><th id='GwXOt'></th></span></q></dt></tr></i><div id='GwXOt'><tfoot id='GwXOt'></tfoot><dl id='GwXOt'><fieldset id='GwXOt'></fieldset></dl></div>
                      <tbody id='GwXOt'></tbody>
                        <bdo id='GwXOt'></bdo><ul id='GwXOt'></ul>
                        <tfoot id='GwXOt'></tfoot>

                          • <small id='GwXOt'></small><noframes id='GwXOt'>

                            <legend id='GwXOt'><style id='GwXOt'><dir id='GwXOt'><q id='GwXOt'></q></dir></style></legend>