Searching a text in specific columns only and filter rows based on entered text
Searching a text in specific columns only and filter rows based on entered text
miranch2002
Posts: 4Questions: 0Answers: 0
I would like to get help about datatable’s multi-column searching for a given input text. I know that single column and global filtering option is available in the datatable fnFilter() but I haven’t found anything for specific multi-column searching scenario. Please note that this is not server side processing.
Scenario:
Assume that I have 5 columns in a databale (Id, Primanry Name, Secondary Name, Other Name and Other Column), what I want is - for a given text I want to search only in 3 columns. For example, when I enter text "name" in the search textbox, I want to find out all the rows in the datatable that machtes the text in either "primary name" or "secondary name" or "other name" column. Please note that it is "OR" condition. The code I provided below ("_fnSetMultiFilterData"), it works like AND condition. But I don't want this "AND" condition.
Search Text: “name”
[code]
Id Primary Name Secondary Name Other Name OtherColumn
1 Some name NA
2 NA Some name
3 NA NA Some name
4 NA NA
5 Some name
[/code]
Search Result:
PLEASE NOTE THAT row#3 IS EXCLUDED FROM THE SEARCH RESULT. Even though the "name" word exists in row#3 in OtherColumn still it is not displayed because I am not searching Othercolumn.
[code]
Id Primary Name Secondary Name Other Name OtherColumn
1 Some name NA
2 NA Some name
5 Some name
[/code]
I tried the following code seeing example from forum (http://datatables.net/forums/discussion/1164/multi-column-filtering/p1#Form_Comment) but it didn't workout
[code]
//This is the code on .cshtml page
function fnFilterMultiple(oTable) {
var inst = $('#txtName').val();
var defaultFilters = { "c2": inst, "c4": inst, "c5": inst };
oTable.fnMultiFilter(defaultFilters);
}
$(document).ready(function () {
var oTable = $('#myGrid').dataTable({
"bJQueryUI": true,
"iDisplayLength": -1,
"sScrollY": "320px",
"sScrollX": "100%",
"sScrollXInner": "110%",
"bLengthChange": false,
"aoColumns": [
{ "sName": "c0" },
{ "sName": "c1" },
{ "sName": "c2" },
{ "sName": "c3" },
{ "sName": "c4" },
{ "sName": "c5" },
{ "sName": "c6" },
{ "sName": "c7" },
{ "sName": "c8" },
{ "sName": "c9" }
]
});
$('txtName').keyup(function () { fnFilterMultiple(oTable);
});//Ready end
[/code]
[code]
//I added the following lines of code in jquery.datatables-1.9.4.js
//I got this code from http://datatables.net/forums/discussion/1164/multi-column-filtering/p1#Form_Comment
_fnSetMultiFilterData = function (oSettings, fData) {
for (var key in fData) {
if (fData.hasOwnProperty(key)) {
//Loop through all the columns and configure the column w/ the matching name
for (var i in oSettings.aoColumns) {
if (oSettings.aoColumns[i].sName == key) {
/* Single column filter */
oSettings.aoPreSearchCols[i].sSearch = fData[key];
}
}
}
}
return oSettings;
}
this.fnMultiFilter = function (fData) {
var oSettings = _fnSettingsFromNode(this[DataTable.ext.iApiIndex]);
var oSettings = _fnSetMultiFilterData(oSettings, fData);
//Loop through all elements in fData and configure them in the settings.
_fnFilterComplete(oSettings, oSettings.oPreviousSearch, 1);
}
[/code]
Can anyone please provide any sample code? Thanks in advance and your help is much appreciated.
-Miran
Scenario:
Assume that I have 5 columns in a databale (Id, Primanry Name, Secondary Name, Other Name and Other Column), what I want is - for a given text I want to search only in 3 columns. For example, when I enter text "name" in the search textbox, I want to find out all the rows in the datatable that machtes the text in either "primary name" or "secondary name" or "other name" column. Please note that it is "OR" condition. The code I provided below ("_fnSetMultiFilterData"), it works like AND condition. But I don't want this "AND" condition.
Search Text: “name”
[code]
Id Primary Name Secondary Name Other Name OtherColumn
1 Some name NA
2 NA Some name
3 NA NA Some name
4 NA NA
5 Some name
[/code]
Search Result:
PLEASE NOTE THAT row#3 IS EXCLUDED FROM THE SEARCH RESULT. Even though the "name" word exists in row#3 in OtherColumn still it is not displayed because I am not searching Othercolumn.
[code]
Id Primary Name Secondary Name Other Name OtherColumn
1 Some name NA
2 NA Some name
5 Some name
[/code]
I tried the following code seeing example from forum (http://datatables.net/forums/discussion/1164/multi-column-filtering/p1#Form_Comment) but it didn't workout
[code]
//This is the code on .cshtml page
function fnFilterMultiple(oTable) {
var inst = $('#txtName').val();
var defaultFilters = { "c2": inst, "c4": inst, "c5": inst };
oTable.fnMultiFilter(defaultFilters);
}
$(document).ready(function () {
var oTable = $('#myGrid').dataTable({
"bJQueryUI": true,
"iDisplayLength": -1,
"sScrollY": "320px",
"sScrollX": "100%",
"sScrollXInner": "110%",
"bLengthChange": false,
"aoColumns": [
{ "sName": "c0" },
{ "sName": "c1" },
{ "sName": "c2" },
{ "sName": "c3" },
{ "sName": "c4" },
{ "sName": "c5" },
{ "sName": "c6" },
{ "sName": "c7" },
{ "sName": "c8" },
{ "sName": "c9" }
]
});
$('txtName').keyup(function () { fnFilterMultiple(oTable);
});//Ready end
[/code]
[code]
//I added the following lines of code in jquery.datatables-1.9.4.js
//I got this code from http://datatables.net/forums/discussion/1164/multi-column-filtering/p1#Form_Comment
_fnSetMultiFilterData = function (oSettings, fData) {
for (var key in fData) {
if (fData.hasOwnProperty(key)) {
//Loop through all the columns and configure the column w/ the matching name
for (var i in oSettings.aoColumns) {
if (oSettings.aoColumns[i].sName == key) {
/* Single column filter */
oSettings.aoPreSearchCols[i].sSearch = fData[key];
}
}
}
}
return oSettings;
}
this.fnMultiFilter = function (fData) {
var oSettings = _fnSettingsFromNode(this[DataTable.ext.iApiIndex]);
var oSettings = _fnSetMultiFilterData(oSettings, fData);
//Loop through all elements in fData and configure them in the settings.
_fnFilterComplete(oSettings, oSettings.oPreviousSearch, 1);
}
[/code]
Can anyone please provide any sample code? Thanks in advance and your help is much appreciated.
-Miran
This discussion has been closed.
Replies
http://www.datatables.net/release-datatables/examples/api/multi_filter.html
I want the functionality exactly same like global search BUT global search searches in all columns in the datatable for a given text. Assume that I have 5 columns in a databale, what I want is - for a given text I want to search only in 3 columns. For example, when I enter text "name" in the search textbox, I want to find out all the rows in the datatable that machtes the text in either "primary name" or "secondary name" or "other name" column. Please note that it is "OR" condition. The code I provided ("_fnSetMultiFilterData"), it works like AND condition. But I don't want this "AND" condition.
Explicit example:
Step 1: I type "name" in search text box
Step 2: Find all the rows in the datatable where "name" word exists in either "primary name" OR "secondary name" OR "other name" column in the databale.