简体   繁体   English

使用datatable.js过滤名字和姓氏

[英]Filtering firstname and lastname using datatable.js

I have created a application with dataTable for filtering firstname and lastname, I am having a firstname and lastname text-field through which I would filter firstname and lastname. 我创建了一个带有dataTable的应用程序,用于过滤名字和姓氏,我有一个名字和姓氏文本字段,通过该字段可以过滤名字和姓氏。 The filtering is working but the issue is that firstname and lastname filtering is doing from both the textfield, actually I need to make it into specific in such a way that firstname textfield is only for firstname filtering and lastname textfield is only for lastname filtering 过滤有效,但问题在于,姓氏和姓氏过滤同时在两个文本字段中进行,实际上我需要将其具体化,以使名字文本字段仅用于姓氏过滤,姓氏文本字段仅用于姓氏过滤

Can anyone please tell me some solution for this? 谁能告诉我一些解决方案吗?

JSFiddle 的jsfiddle

My code is as given below: 我的代码如下所示:

$(document).ready(function () {
    myTable = $('#myTable').dataTable({
            "bSort": false,
            "bInfo": false,
            "bLengthChange": false,
            "bPaginate": false,
            "scrollY": "300px",
            "scrollX": "100%",
            "scrollCollapse": true,
    });

    new $.fn.dataTable.FixedColumns(myTable, {
        leftColumns: 1,
        rightColumns: 1
    });

   $('#firstNameTextBox').keyup(function () {
        filterNames(this.value, 0);
    });

     $('#secondNameTextBox').keyup(function () {
        filterNames(this.value, 0);
    });


    function filterNames(value) {
        myTable.fnFilter(value, 0, false, false, false, false);
    }
});

One way is by using custom filtering (>1.10) 一种方法是使用自定义过滤 (> 1.10)

This piece of code will be executed every time the table is drawn. 每次绘制表格时都会执行这段代码。 The method takes the first and second name from the fields and creates a regular expression. 该方法从字段中获取名字和名字,并创建一个正则表达式。 The data[0].replace removes the spaces in between first and second names and coverts to an array which is later used to test again the regex. data [0] .replace删除名字和第二个名字之间的空格,并隐蔽到一个数组,该数组随后用于再次测试正则表达式。

$.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var firstName = $('#firstNameTextBox').val();
        var secondName = $('#secondNameTextBox').val();
        var fnRegex = new RegExp(firstName, 'i');
        var snRegex = new RegExp(secondName, 'i');
        var name = data[0].replace(/[^\w\s]|_/g, function ($1) { return ' ' + $1 + ' ';}).replace(/[ ]+/g, ' ').split(' '); // courtesy: http://stackoverflow.com/a/6162630
        return fnRegex.test(name[0]) && snRegex.test(name[1]);
    }
);

Explanation: 说明:

var fnRegex = new RegExp(firstName, 'i'); 

This will create a regex like this as you type in first name textfield 在您输入名字文本字段时,这将创建一个这样的正则表达式

/J/i
/Je/i 
/Jef/i
/Jeff/i

The same is with second name textfield 与名字文本字段相同

var snRegex = new RegExp(secondName, 'i');

Since the table data looks like this Jefferey Sam . 由于表数据看起来像Jefferey Sam The below .replace() method line will break it into an array like this ["Jeffrey", "Sam"] so that regex can check individually on each element. 下面的.replace()方法行将把它分成一个像["Jeffrey", "Sam"]这样的数组["Jeffrey", "Sam"]以便正则表达式可以单独检查每个元素。

var name = data[0].replace(/[^\w\s]|_/g, function ($1) { 
    return ' ' + $1 + ' ';
})
.replace(/[ ]+/g, ' ')
.split(' ');

$.fn.datatable.ext.search iterates over each row and adds those rows into the table which return true. $.fn.datatable.ext.search遍历每行并将这些行添加到返回true的表中。 (which is the last line) (这是最后一行)

return fnRegex.test(name[0]) && snRegex.test(name[1]);

both first name and second name regular expression test against the first value of the array (first name) and the second value of the array (second name). 名字和名字正则表达式均针对数组的第一个值(名字)和数组的第二个值(第二名字)进行测试。 regex.test will return true or false. regex.test将返回true或false。

If the user types Jeff in first name text field and Sa in second field, the below is how $.fn.datatable.ext.search.push will look like for each row 如果用户在名字文本字段中输入Jeff并在第二个字段中输入Sa ,则下面是每行$.fn.datatable.ext.search.push外观

return /Jeff/i.test("Jefferey") && /Sa/i.test("Sa"); // true && true ===> true (so this will be added);
return /Jeff/i.test("Linsu") && /Sa/i.test("Lessi"); // false && false ===> false (so this will NOT be added).
return ..... // so on for the rest of the rows

Here is a demo http://jsfiddle.net/dhirajbodicherla/189Lp6u6/2/ 这是一个演示http://jsfiddle.net/dhirajbodicherla/189Lp6u6/2/


Versions < 1.10 版本<1.10

The draw method alone is changed to below 单独的绘制方法更改为以下

var myTable = $('').dataTable() with (lowercase D)
myTable.api().draw();

Here is a demo http://jsfiddle.net/dhirajbodicherla/189Lp6u6/3/ 这是一个演示http://jsfiddle.net/dhirajbodicherla/189Lp6u6/3/

Hope this helps 希望这可以帮助

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

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