how to load value in datatables search, doing ajax query?

how to load value in datatables search, doing ajax query?

cris19ncris19n Posts: 55Questions: 18Answers: 0

How can I load the value in "oSearch" before the data is loaded, but this value is retrieved from an ajax query?

I need to do an ajax query to put the value in the search field.

I can do a function within the search.

something like that:
"oSearch": {"sSearch": /*here ajax query, to get the value, I need to search datatables on its first load*/}

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited October 2020 Answer ✓

    Use a jQuery ajax request to fetch the search term. In the success function initialize Datatables with the search term, something like this:

        $.ajax({
            url : 'myUrl',
            success : function (data) {
                // extract the search term from data
                $('#myTable').DataTable({
                    search: {search: mySearchTerm},
                    // Remaining datatable options.
                });
            }
        });
    

    Kevin

This discussion has been closed.