Numeric searches

Numeric searches

dannygdannyg Posts: 12Questions: 0Answers: 0
edited May 2009 in General
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?

Thanks
Danny

Replies

  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    Hi dannyg,

    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
  • dannygdannyg Posts: 12Questions: 0Answers: 0
    Hi 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
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    I don't have any examples of this, but the following might do the trick for you:

    [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
  • dannygdannyg Posts: 12Questions: 0Answers: 0
    Have I placed the code in the right place?

    $('#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
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    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

    Allan
  • dannygdannyg Posts: 12Questions: 0Answers: 0
    Ok, cheers for that. I would have thought this would work:

    $(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);
    });
    });
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    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.

    Allan
  • dannygdannyg Posts: 12Questions: 0Answers: 0
    I tried that way also!

    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);
    });
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    The code looks okay to me... Can you provide a link? If you don't want to make it public you can send it to me direct: www.datatables.net/contact .

    Thanks
    Allan
  • dannygdannyg Posts: 12Questions: 0Answers: 0
    sorry only got this working locally. cheers anyway

    Danny
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    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.

    Allan
  • dannygdannyg Posts: 12Questions: 0Answers: 0
    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
  • allanallan Posts: 63,578Questions: 1Answers: 10,483 Site admin
    edited May 2009
    Hi Danny,

    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
This discussion has been closed.