What was searched for?

What was searched for?

PC-GramPC-Gram Posts: 5Questions: 2Answers: 0

My user has used the search option and has made some changes to the data. On submit my program then reloads from backend (not using ajax), but now I want to apply the same search words again using the search.search option. But how can I on submit() read what was entered in the search field?

Answers

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406
    edited March 2022

    You see "search.value" in the parameters for server side processing:
    https://datatables.net/manual/server-side#Sent-parameters

    I use it here in the data callback:

    var fullTextSearchString;
    ....
    ajax: {
        url: '...',
        type: 'POST',
        data: function ( d ) {   
            fullTextSearchString = d.search.value;
        }
    },
    

    Since you are not using ajax you'll need to figure out how to get hold of that value in your code.

    Alternatively you can do it like this:
    https://datatables.net/reference/api/search()

    Something like this should also work if you execute this code on (pre)submit:

    var searchString = table.search();
    
  • PC-GramPC-Gram Posts: 5Questions: 2Answers: 0

    Using Ajax would solve the problem. However my data is never more than a few hundred rows, and I want to keep it simple.
    I can use the table.search() to get the currently applied global search, but what I get is what I initiated through the search.search option. It does not yield whatever the user enters in the seach input field.

    Any ideas?

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765
    edited March 2022

    I created this example that shows using search.search with a button to alert with table.search(). When the user searches for something different the result of table.search() is the updated search.
    http://live.datatables.net/yenikafu/1/edit

    If this isn't working for you please provide a link to your page or update the tet case or create your own to show the problem.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    I can use the table.search() to get the currently applied global search, but what I get is what I initiated through the search.search option. It does not yield whatever the user enters in the seach input field.

    Sounds like a timing issue. You should use an event handler to make sure you capture the final content of the search field when submitting and not the initial content which you set using the search.search option.

  • PC-GramPC-Gram Posts: 5Questions: 2Answers: 0

    My basic problem is now solved. it turned out that the functionality I needed is found in option stateSave: true

Sign In or Register to comment.