繁体   English   中英

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

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

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

看看链接的示例,我试图基于原始行数据使用自定义过滤器,但是在输入诸如systems的过滤器值时出现问题。 当我希望systems与“ Systems Administrator”匹配并显示这些行时,将过滤掉具有Systems Administrator的行。

通读源代码 ,看起来好像首先对搜索字符串应用了全局过滤器 ,该搜索字符串是根据呈现的值进行编译的

有没有人找到解决方案?

您的问题是仍然执行默认搜索,并且由于列的值呈现为“ system ...”,因此它无法找到与“ systems”的任何匹配项。 您可以这样做:

$("#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();
})  

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


更好的答案是根据可以为_filterdisplay typerender()返回不同的值。 display返回省略号,否则返回原始值。 这样,您可以完全跳过自定义过滤器:

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

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

问题是您的charlimit会剪切字符并与显示的数据而不是原始数据进行匹配。 基本上systems不能找到,因为system是最大长度。

暂无
暂无

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

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