5

I'm using jQuery datatables just to display some data, but know, i want to filtering those with multiple checkboxes.
My table contains this header: Host, Type, Record, Description, Free
And these are my checkboxes outside of the datatable:
Type: MX A CNAME
Free: 0 1 So at the begining I want to show all of the data. If someone now checks the checkbox MX, it should only show data with type MX. If someone checks MX, CNAME and 0, it should only show data with these filters. You know what I mean?
I've a plugins which can do this, but the checkboxes are under the columns, my checkboxes are outside the datatable.

Any idea?

2
  • Before I start: Are you using server sided processing on this? If not, can you prepare a plunker? Commented Apr 10, 2014 at 8:44
  • @mainguy No, i'm not using server sided processing. What is a plunker? Commented Apr 10, 2014 at 10:28

1 Answer 1

12

You should use this function:

function filterme() {
  //build a regex filter string with an or(|) condition
  var types = $('input:checkbox[name="type"]:checked').map(function() {
    return '^' + this.value + '\$';
  }).get().join('|');
  //filter in column 0, with an regex, no smart filtering, no inputbox,not case sensitive
  otable.fnFilter(types, 0, true, false, false, false);

  //build a filter string with an or(|) condition
  var frees = $('input:checkbox[name="free"]:checked').map(function() {
    return this.value;
  }).get().join('|');
  //now filter in column 2, with no regex, no smart filtering, no inputbox,not case sensitive
  otable.fnFilter(frees, 2, false, false, false, false);
}

Look at the comments to see what it does.

Call it from your html like this:

  <input onchange="filterme()" type="checkbox" name="free" value="0">0
  <input onchange="filterme()" type="checkbox" name="free" value="1">1

Plunker is a testbed for HTML and JS Snippets that greatly helps others to understand where your issue is located and to give a fast response on how to fix things. It simply works like html/js/css editor that renders your example on every keypress and is awesome.

Click here to see how I fixed your issue with Plunker

On the left side of the Plunker you will find the files I used in the process (index.html and script.js)

While this works and should give you an idea on how to continue, you should think about using radio buttons for the FREE filter, because it would make more sense.

Since this is a quiet complicated issue come back to me if something needs more explanation.

Sign up to request clarification or add additional context in comments.

9 Comments

I have problems with the free. I got data under record which contain 0 or 1, so he shows them up too.
What do you mean? 0 and 1 are covered by the two inputs. As I said, it would make more sense here to use either two radios which have either Free/Not Free or a single checkbox which returns Free True/Free Not True. Is that what you want? In the current setup checkmarks can be on both values 0 and 1 which will of course return all records.
Look at this Plunker which uses radio buttons, which are more logical: plnkr.co/edit/45hOwYG13VAooKfPPAw3?p=preview
Uh, now I understand. Then you should filter frees in the respective column only. In my example is used 2 as filter column which is the record column in your code. Limit to row 4 (starting count at zero) in this line: otable.fnFilter(frees, 4, false, false, false, false);
Now look at this last plunker which should match your data structure: plnkr.co/edit/gDQHtg7y3XyqN8InCiwI?p=preview Are you beginning to see how useful plunkers are?
|

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.