简体   繁体   English

jQuery dataTables-基于原始行数据的自定义过滤

[英]jQuery dataTables - Custom Filtering Based On Original Row Data

JSBin: http://live.datatables.net/nujehixu/3/edit?js,console,output JSBin: http ://live.datatables.net/nujehixu/3/edit?js,控制台,输出

$(document).ready( function () {

  // push our custom filter onto the stack of filters
  $.fn.dataTable.ext.search.push(function(settings, searchData, index, rowData, counter) {
    // get filter value
    var value = $("#example_filter > label > input").val().toLowerCase();
    // check filter value against original row data
    var original = rowData[1].toLowerCase();
    console.log(original);
    return original.indexOf(value) > -1;
  });

  function addEllipsis(original, charLimit) {
    if (original.length > charLimit) {
      // substring the original and add ellipsis with a title attribute of the original
      return '<div title="' + original + '">' + original.substr(0, charLimit) + '&hellip;' + '</div>';
    }
    // return the original value since it is already short enough
    return '<div title="' + original + '">' + original + '</div>';
  }

  var table = $('#example').DataTable({
    columnDefs: [
      {
        targets: 1,
        render: function (data, type, row) {
          // render with ellipsis if necessary
          return addEllipsis(data, 6);
        }
      }
    ]
  });
} );

Check out the linked example, I'm trying to use a custom filter based on the original row data, but the issue surfaces when entering a filter value like systems . 看看链接的示例,我试图基于原始行数据使用自定义过滤器,但是在输入诸如systems的过滤器值时出现问题。 The rows with Systems Administrator are filtered out when I would expect systems to match 'Systems Administrator' and show those rows. 当我希望systems与“ Systems Administrator”匹配并显示这些行时,将过滤掉具有Systems Administrator的行。

Reading through the source , it looks like there is a global filter applied first against a search string, which is compiled from the rendered values . 通读源代码 ,看起来好像首先对搜索字符串应用了全局过滤器 ,该搜索字符串是根据呈现的值进行编译的

Has anyone found a solution for this? 有没有人找到解决方案?

Your problem is that the default search still is performed, and by that it cannot find any matches to "systems" since the values of the columns is rendered to "system...". 您的问题是仍然执行默认搜索,并且由于列的值呈现为“ system ...”,因此它无法找到与“ systems”的任何匹配项。 You can do it like this : 您可以这样做:

$("#example_filter > label > input").unbind().bind('keyup', function() {
  var value = this.value.toLowerCase();
  // push our custom filter onto the stack of filters
  $.fn.dataTable.ext.search.push(function(settings, searchData, index, rowData, counter) {
    // get filter value
    // check filter value against original row data
    var original = rowData[1].toLowerCase();
    console.log(original);
    return original.indexOf(value) > -1; 
  });
  table.draw();
  $.fn.dataTable.ext.search.pop();
})  

altered code -> http://live.datatables.net/dalesexe/4/edit 更改的代码-> http://live.datatables.net/dalesexe/4/edit


A better answer is to return different values from render() based on type that can be _ , filter or display . 更好的答案是根据可以为_filterdisplay typerender()返回不同的值。 On display return the ellipsied' value, otherwise return the original value. display返回省略号,否则返回原始值。 By that you can skip the custom filter entirely : 这样,您可以完全跳过自定义过滤器:

render: function (data, type, row) {
             switch (type) {
               case 'display' : 
                 return addEllipsis(data, 6); break;
               default :
                 return data; break;  
             }             
        }

new code -> http://live.datatables.net/dalesexe/6/edit 新代码-> http://live.datatables.net/dalesexe/6/edit

The issue is that your charlimit cuts the characters and your matching with the displayed data not the original data. 问题是您的charlimit会剪切字符并与显示的数据而不是原始数据进行匹配。 Basically systems can't be found because system is the maximum length. 基本上systems不能找到,因为system是最大长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM