I have a numeric column in a datatable. With a search, all numbers containing the number being searched are returned, so 1 brings back 1, 10, 11..
Is there a way to bring back just the number being searched on?
You can do this using regular expressions. For example "^1$" will match on only the character "1". You can also specify the column that you want to sort on to match sure you only do this on your numeric column. This is done by fnFilter(). See this discussion (on going :-) ) for further information about this:
Thanks for the reply, I have read the discussion, still not sure how to implement it. An example of using the regular expression with the search value like you mention would be great if you could point me to any.
This will search on column 0 (the first one) for the value and only the value of the input. If you want all values starting with the input value you could drop the $.
Almost there, you need to define what 'oTable' is. In this case you want it to be the result of your dataTable() initialisation call. Have a look at some of the API examples for this: http://datatables.net/examples/example_add_row.html
Again almost... The problem in this case is that oTables is a local variable to the ready() anonymous function. Therefore the keyup() anonymous function doesn't know anything about it. Just make the oTable variable global.
In that case, try removing the ^ and $ from the search and seeing if it matches what you would expect in column 0. If so then you can actually type ^banana etc into your search box and test that to build it up.
Tried that, typing ^ in the search box all row go. For example if I am searching for 1, entering ^1 returns no rows. Typing 1 matches on col 0 with id of 1, and on the second column, value 100
Replies
You can do this using regular expressions. For example "^1$" will match on only the character "1". You can also specify the column that you want to sort on to match sure you only do this on your numeric column. This is done by fnFilter(). See this discussion (on going :-) ) for further information about this:
http://datatables.net/forums/comments.php?DiscussionID=255&page=1#Item_6
Allan
Thanks for the reply, I have read the discussion, still not sure how to implement it. An example of using the regular expression with the search value like you mention would be great if you could point me to any.
Cheers
Danny
[code]
$('#single_column_filter').keyup( function () {
oTable.fnFilter( "^"+this.value+"$", 0, false );
} );
[/code]
This will search on column 0 (the first one) for the value and only the value of the input. If you want all values starting with the input value you could drop the $.
Hope that helps,
Allan
$('#enquiries').dataTable({
"aoColumns": [
{ "bSortable": true, "bSearchable": true, "sType": "numeric" },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true , "bSearchable": true }
],
"aaSorting": [[0, "desc"]]
});
$('#enquiries').keyup(function() {
oTable.fnFilter("^" + this.value + "$", 0, false);
});
});
Thanks
Danny
Allan
$(document).ready(function() {
var oTable;
oTable = $('#enquiries').dataTable({
"aoColumns": [
{ "bSortable": true, "bSearchable": true, "sType": "numeric" },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true }],
"aaSorting": [[0, "desc"]]
});
$('#enquiries').keyup(function() {
oTable.fnFilter("^" + this.value + "$", 0, false);
});
});
Allan
var oTable;
$(document).ready(function() {
oTable = $('#enquiries').dataTable({
"aoColumns": [
{ "bSortable": true, "bSearchable": true, "sType": "numeric" },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true },
{ "bSortable": true, "bSearchable": true }],
"aaSorting": [[0, "desc"]]
});
});
$('#enquiries').keyup(function() {
oTable.fnFilter("^" + this.value + "$", 0, false);
});
Thanks
Allan
Danny
Allan
I've just put up a demo showing regular expression filtering in action:
http://datatables.net/1.5-beta/examples/api/regex.html
Not that if you use the regular expression filter with the column index set to 1 you can do the following:
"ca" - results in 5 rows
"^ca" - results in 2 rows
"^ca$" - results in 0 rows
"^camino 1.0$" -results in 1 row
Hope you find this of some use,
Allan