How can I add to the filter function?
How can I add to the filter function?
Hi there.
I am using datatables to present my data to the users but I am currently stuck.
What I am trying to achieve is this:
When the user presses the custom added search button the results presented are a mixture of my results and the normal results from the datatables.
This is my code related to this:
[code]
var bServerSearch = true;
var aServerSearchData = new Array();
var iServerSearchCompareColumn = 0;
var bSearchingServerSide = false;
var bFilterWhenServerSideFinished = false;
var oTable;
var iSearchWait = 0;
var iSearchWaitInterval;
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
if (bServerSearch) {
for (var i = 0; i < aServerSearchData.length; i++) {
if (aServerSearchData[i] == aData[iServerSearchCompareColumn]) return true;
}
}
}
);
$(document).ready(function(){
oTable = $('#table').dataTable({
"aoColumns": [
{"sWidth": "85px"},
{"sWidth": "139px"},
{"sType": "dato-norsk", "sWidth": "99px"},
{"sWidth": "381px"},
{"sWidth": "105px"},
{"sWidth": "129px"},
],
"sDom":'<"top"fp>rt<"bottom"p><"clear">',
"sPaginationType": "full_numbers",
"iDisplayLength": 20,
});
$(".first.paginate_button, .last.paginate_button").remove();
$('.dataTables_filter input').unbind('keypress keyup').bind('keypress keyup', function(e){
var item = $(this);
iSearchWait = 0;
if(!iSearchWaitInterval) iSearchWaitInterval = setInterval(function(){
if(iSearchWait>=3){
clearInterval(iSearchWaitInterval);
iSearchWaitInterval = '';
sSearchTerm = $(item).val();
aServerSearchData = new Array();
bSearchingServerSide = true;
$.ajax({
url:'search.ajax.php',
dataType:'json',
data: {'term':sSearchTerm},
success: function(data) {
aServerSearchData = data;
if (bFilterWhenServerSideFinished) oTable.fnFilter(sSearchTerm);
bSearchingServerSide = false;
bFilterWhenServerSideFinished = false;
}
});
iSearchWait = 0;
}
iSearchWait++;
},100);
});
$('').html('Search')
.attr('id', 'sok')
.appendTo('#table_filter label')
.bind('click', function(e){
var item = $(this);
searchTerm = $(item).val();
oTable.fnFilter(searchTerm);
if (bSearchingServerSide) bFilterWhenServerSideFinished = true;
});
});
[/code]
As you can see I fetch the data when the user is finished with writing in the hope that it gets fetched from the server before the user clicks the button. Now it only returns the results from the server. I want it to add the normal results as well. Is this possible?
I am using datatables to present my data to the users but I am currently stuck.
What I am trying to achieve is this:
When the user presses the custom added search button the results presented are a mixture of my results and the normal results from the datatables.
This is my code related to this:
[code]
var bServerSearch = true;
var aServerSearchData = new Array();
var iServerSearchCompareColumn = 0;
var bSearchingServerSide = false;
var bFilterWhenServerSideFinished = false;
var oTable;
var iSearchWait = 0;
var iSearchWaitInterval;
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
if (bServerSearch) {
for (var i = 0; i < aServerSearchData.length; i++) {
if (aServerSearchData[i] == aData[iServerSearchCompareColumn]) return true;
}
}
}
);
$(document).ready(function(){
oTable = $('#table').dataTable({
"aoColumns": [
{"sWidth": "85px"},
{"sWidth": "139px"},
{"sType": "dato-norsk", "sWidth": "99px"},
{"sWidth": "381px"},
{"sWidth": "105px"},
{"sWidth": "129px"},
],
"sDom":'<"top"fp>rt<"bottom"p><"clear">',
"sPaginationType": "full_numbers",
"iDisplayLength": 20,
});
$(".first.paginate_button, .last.paginate_button").remove();
$('.dataTables_filter input').unbind('keypress keyup').bind('keypress keyup', function(e){
var item = $(this);
iSearchWait = 0;
if(!iSearchWaitInterval) iSearchWaitInterval = setInterval(function(){
if(iSearchWait>=3){
clearInterval(iSearchWaitInterval);
iSearchWaitInterval = '';
sSearchTerm = $(item).val();
aServerSearchData = new Array();
bSearchingServerSide = true;
$.ajax({
url:'search.ajax.php',
dataType:'json',
data: {'term':sSearchTerm},
success: function(data) {
aServerSearchData = data;
if (bFilterWhenServerSideFinished) oTable.fnFilter(sSearchTerm);
bSearchingServerSide = false;
bFilterWhenServerSideFinished = false;
}
});
iSearchWait = 0;
}
iSearchWait++;
},100);
});
$('').html('Search')
.attr('id', 'sok')
.appendTo('#table_filter label')
.bind('click', function(e){
var item = $(this);
searchTerm = $(item).val();
oTable.fnFilter(searchTerm);
if (bSearchingServerSide) bFilterWhenServerSideFinished = true;
});
});
[/code]
As you can see I fetch the data when the user is finished with writing in the hope that it gets fetched from the server before the user clicks the button. Now it only returns the results from the server. I want it to add the normal results as well. Is this possible?
This discussion has been closed.
Replies
Allan