0

I currently have custom filter buttons that use the search API function of the database.

HTML

  <ul>
                    <li class="btn"><a href="#" data-type="all">All</a></li>
                    <li class="btn"><a href="#" data-type="video">Video</a></li>
                    <li class="btn"><a href="#" data-type="image">Image</a></li>
                    <li class="btn"><a href="#" data-type="upload">Upload Dash</a></li>
                    <li class="btn"><a href="#" data-type="link">Code Link</a></li>
                  </ul>

JQuery

 var table = $('#rlist').dataTable( {
        "processing": true,
        "serverSide": true,
        "order": [[ 0, "asc" ]],
        "sDom": "<'row'<'col-sm-6'l><'col-sm-6'T <'clearfix'>f>r>t<'row'<'col-sm-5'i><'col-sm-7'p>>",
        "sPaginationType": "full_numbers",
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "ajax": baseurl+"media/ajax_fetch"       

    });

    $('ul').on('click', 'a', function() {

          table
            .columns(1)
            .search($(this).text())
            .draw();
        });

        $('ul').on('click', 'a.all', function() {

          table
            .search('')
            .columns(1)
            .search('')
            .draw();
        });

The problem is I don't want to use the search API, I want to send the button data-type attribute to the server side where I can manipulate manually not interfering with the search function.

How do I do this?

1 Answer 1

1

I use a similar functionality with my current project. Basically, you need to modify your click events to take the element's data-type attribute, call an ajax with that data, then clear the table and add the new searched data.

You will need to clear the client side table everytime you search doing it through this approach though.

$('ul').on('click', 'a', function() {
  var elementType = $(this).attr("data-type");
  $.ajax({
    type: "POST",
    //assuming server url is something like '/someURL/{elementType}'
    url: urlToServer + elementType, 
    success: function(response) {
      if (response) {
        table.clear().rows.add(response).draw();
      }
      //no data back from server
      else table.clear();
    }
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I have pagination set up, does this update pagination? For example if the filter has 20 total results but i'm only showing 10 per page? Will datatables update pagination?
If you use .clear() as I suggest, any settings you set during datatables initialization will be retained. This includes things like rendering functions, pagination settings, sorting settings, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.