How to do custom filtering with Datatables and server-side processing

前端 未结 5 1712
面向向阳花
面向向阳花 2021-02-02 17:30

I am using Datatables to display tabular data in my Web application, and have configured it to make use of server-side processing, i.e. query the server via AJAX for filtered da

5条回答
  •  悲哀的现实
    2021-02-02 18:13

    Finally did it after spending hours!

    I will post the complete method here for everyone's help.

    One needs to use fnServerParams option, which allows adding custom parameters to be sent in the XMLHttpRequest sent to the server. For example:

    Here is the coffescript I used:

    jQuery ->
      table = $('#logs').dataTable
        bProcessing: true
        bServerSide: true
        sAjaxSource: $('#logs').data('source')
        fnServerParams: (aoData) ->
          applicationName = $("#applicationName").val()
          aoData.push
            name: "applicationName"
            value: applicationName
    
          return
    
      $("#applicationName").on "change", ->
        table.fnDraw()
        return
    

    My HTML file contains the input element with id applicationName. Note the fnDraw() element I used to enable redraw request whenever input value changes.

提交回复
热议问题