filter(): Where I have to put the piece of code ?
filter(): Where I have to put the piece of code ?

/* I have a very simple test, but I don't know where to place the filter, this is my JS: */
/*
* Editor client script for DB table test
* Created by http://editor.datatables.net/generator
*/
(function($){
$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
ajax: 'php/table.test.php',
table: '#test',
fields: [
{
"label": "nombre:",
"name": "nombre"
},
{
"label": "edad:",
"name": "edad"
},
{
"label": "estado:",
"name": "estado"
}
]
} );
var table = $('#test').DataTable( {
dom: 'Bfrtip',
ajax: 'php/table.test.php',
columns: [
{
"data": "nombre"
},
{
"data": "edad",
render: $.fn.dataTable.render.number( ',', '.', 0, '' )
},
{
"data": "estado"
}
],
select: true,
lengthChange: false,
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
]
} );
} );
}(jQuery));
/* I gues my filter have to look something like this: */
var table = $('#test').DataTable();
var filteredData = table
.column( 2 )
.data()
.filter( function ( value, index ) {
return value > "NYC" ? true : false;
} );
/* can somebody help?, where I have to put this code? */
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
At what point do you want to get that filtered data? What what do you want to do with it?
The above won't work as it will attempt to get the data from the table before the Ajax data has been loaded (keep in mind that ajax is async). If you just want to check
filteredData
you could put it intoinitComplete
and thenconsole.log()
the result.Allan
It is a filter ¡I want to filter the data to show before rendering the data!
And you can't simply reduce the data returned to the client-side by the server-side (i.e. apply a WHERE statement if you have an SQL database backing this)? That would be the most efficient way to do this.
If you can't do that, then use
ajax.dataSrc
as a function and remove the data from the returned array.The
filter()
method is not the correct method to use here - as its documentation says:Allan