restrict search results using server-side search processing

restrict search results using server-side search processing

rldean1rldean1 Posts: 141Questions: 66Answers: 1
edited November 2019 in DataTables 1.10

OH MY GOD! I actually got server-side search working with a MS SQL stored procedure!!!! Pretty snazzy!!

So, a couple of questions:

  1. Is there a way to stop "all" records from being returned when the webapp is opened? "All" results are returned on init, which I assume is because the search filter is literally blank.... in other words, unfiltered.
  2. Is there a way to better control what happens during a global search in the DT search bar? For example, typing 1 letter searches everything at once, immediately.... Is there a way to conduct the search after n amount of letters are typed, or perhaps after the user hits enter?

Answers

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949

    Is there a way to stop "all" records from being returned when the webapp is opened?

    Sounds like your webapp isn't fully processing the parameters sent from the clinet as described here:
    https://datatables.net/manual/server-side

    Likely you will want to use LIMIT and OFFSET with your mysql queries to return only the rows for the requested page.

    Is there a way to better control what happens during a global search in the DT search bar?

    One option is to use searchDelay. Another is to take over the global search input with your own event and use a change event. Something similar to this example:
    http://live.datatables.net/jorexujo/1/edit

    Kevin

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1
    edited November 2019

    Thank you, I will check out searchDelay and the other example for Question 2.

    Regarding Question 1... Is there a way to prevent the initial request for data on DT initialization? Like.... init the table, but don't get data until the user starts searching... (blank table on init)?

    OR

    Would it be better just to change the SQL to say, "if the search was null, return nothing"?

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949

    The draw parameter will be 1 the for the first ajax request. You could key off that and simply return something like this:

    {
      "draw": 1,
      "recordsTotal": 0,
      "recordsFiltered": 0,
      "data": []
    }
    

    Haven't tried that but it seems like it would work :smile:

    Kevin

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1

    Just FYI I ended up using deferLoading: [0]

  • kthorngrenkthorngren Posts: 21,328Questions: 26Answers: 4,949

    Forgot about that option :smile:

    Kevin

This discussion has been closed.