Can't search for values in Textboxes if the column hasn't been sorted yet

Can't search for values in Textboxes if the column hasn't been sorted yet

novacaranovacara Posts: 4Questions: 0Answers: 0
edited December 2011 in General
I'm having basically the same problem as this guy http://datatables.net/forums/discussion/2723/sorting-on-input-fields/p1 but no one ever proposed a solution and I do need one.

When the page loads I can't search any values in the datagrid that are IN a textbox. If I sort a column, I can then search any values in textboxes ONLY IN THAT COLUMN. I would have to manually sort all the columns to be able to search in all of them. I am assuming this is due to the way the datatables update itself after a sort. Is there any way to have it do that update after the page is loaded or programatically auto-sort all the columns so that all textboxes in all columns are searchable as soon as the page is loaded?

Below is my code
[code]
$('.datagrid').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"sDom": '<"top"fl>rt<"bottom"ip><"clear">',
"aoColumns": [
null,
{ "sSortDataType": "dom-text" },
{ "sSortDataType": "dom-text", "sType": "numeric" },
{ "sSortDataType": "dom-text" },
{ "sSortDataType": "dom-text", "sType": "numeric" },
{ "sSortDataType": "dom-text" },
{ "sSortDataType": "dom-text", "sType": "numeric" },
{ "sSortDataType": "dom-text", "sType": "numeric" },
{ "sSortDataType": "dom-text", "sType": "numeric" },
null]
});
[/code]

Replies

  • novacaranovacara Posts: 4Questions: 0Answers: 0
    edited December 2011
    I forgot to include my formatting code.

    [code]
    $.fn.dataTableExt.afnSortData['dom-text'] = function (oSettings, iColumn) {
    var aData = [];
    $('td:eq(' + iColumn + ') input', oSettings.oApi._fnGetTrNodes(oSettings)).each(function () {
    aData.push(this.value);
    });
    return aData;
    }
    [/code]
  • novacaranovacara Posts: 4Questions: 0Answers: 0
    It works the same (incorrect) way on this example:

    http://datatables.net/release-datatables/extras/AutoFill/inputs.html
  • novacaranovacara Posts: 4Questions: 0Answers: 0
    Finally figured this out for anyone in the future who is wondering. I knew it couldn't be THAT hard.

    [code]
    oTable.fnSort([[3, 'asc']]);
    oTable.fnSort([[2, 'asc']]);
    oTable.fnSort([[1, 'asc']]);
    [/code]
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    The sort that you've added reads in the values which are then used for the filtering, which is why that works. There isn't a direct equivalent of afnSortData for filtering data, but the ideal solution would be to bind a key event to the inputs that will update the internal DataTables store on each keypress.

    Good to hear you've got a workaround, but I'll keep this in mind for future enhancements, particularly if I do a full editable plug-in.

    Allan
  • sprockettsprockett Posts: 12Questions: 0Answers: 0
    im facing this issue too, and used the temporary work around.
    However, if you edit any of the input text boxes, the search will no longer be accurate.

    A workaround to that would be to makesure that column is resorted "onkeyup" of any of the text boxes.
  • sprockettsprockett Posts: 12Questions: 0Answers: 0
    Hi, can I check which particular function updates the DataTables store?

    I am currently automatically doing a sort everytime a text field changes... However, this screws up the bStateSave...

    Is there a way to update the Datatables store (with new items in the textfields) without the need for sorting?
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    > Is there a way to update the Datatables store (with new items in the textfields) without the need for sorting?

    Currently no - this is a limitation of this feature at the moment, something that I'll be looking at addressing. Really what I think is needed is a plug-in API method that you can call to update the internal data when you find that something has changed externally.

    This is the block of code that does the data pull at the moment: https://github.com/DataTables/DataTables/blob/master/media/src/core/core.sort.js#L29 . That should get pulled out into a function and made accessible through a plug-in.

    Allan
  • sprockettsprockett Posts: 12Questions: 0Answers: 0
    Thanks for the reply Allan :)
    Really appreciate all the work you've put into what I think is one of the the most useful JS libraries ever!
  • sprockettsprockett Posts: 12Questions: 0Answers: 0
    An update:
    I made a separate _fnForceDataUpdate / fnForceDataUpdate functions that contain code that Allan mentioned (which is basically the fnSort code, but without actual sorting logic / re-drawing).

    This helps to refresh the datastore so you can search on form input fields.
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Superb - nice one :-). That sounds absolutely ideal.

    Out of interest, when are you calling it? On 'blur' or 'change' from an input field?

    Allan
  • sprockettsprockett Posts: 12Questions: 0Answers: 0
    Thanks allan :) Yes, on "blur" is best.... (no point to do it on keyup)....
  • kapacitykapacity Posts: 2Questions: 0Answers: 0
    sprockett any chance of you sharing your fnForceDataUpdate implementation? I am working through this now, and getting more familiar with the datatables implementation. Thanks.
This discussion has been closed.