2

I want to Select multiple items in a column filter. The following example allows you to filter on columns using dropdowns:

http://datatables.net/release-datatables/examples/api/multi_filter_select.html

However, I would like to be able to select multiple items in the column filter, possibly with a checkbox beside each item in the dropdown. e.g. in the example, I would Tick 'A' and 'C' for 'CSS grade' so that only these grades are displayed in the table.

How can I get multi select column filters either using the DataTables plugin or otherwise?

3 Answers 3

1

You could build your own filter div with selectlists,checkboxes and whatever and use fnFilter to request filtered data from the server. For example:

$("#mycheckbox").click(function () {
     var dt = $('#mytable').dataTable({ "bRetrieve": true });
     dt.fnFilter($("#mycheckbox").is(':checked'), 1);
});

Posts:

sSearch_1 : true/false
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, could I have a quick fiddle for this?
1

It is really easy, asked in various ways multiple times - but surprisingly I could not find an exact match. If you have a that corresponds to your columns :

<select class="form-control" id="ddlSearch">
    <option value="0">Name</option>
    <option value="1">Position</option>
    <option value="2">Office</option>
    <option value="3">Age</option>
    <option value="4">Start date</option>
    <option value="5">Salary</option>
</select>

then implement this javascript to get seacrh in seperate columns

var table;
$(document).ready(function() {
    table = $('#example').DataTable({
        pageLength: 100,
        dom: 'lrtip',
    });
    var column_no = 0;
    $('#ddlSearch').on('change',function(){
        column_no = Number($(this).val());
    });

    $( '#txtSearch' ).on( 'input', function () {
        if ( table.columns([column_no]).search() !== $( '#txtSearch' ).val() ) {
            table.columns([column_no]).search( $( '#txtSearch' ).val() ).draw();
        }
    } );
});

for example try this https://jsfiddle.net/07rnpgs1/

All the best

Comments

0

You may check out this DataTables plug-in. It provides multiple criteria selection per column, as well as union selection across several columns.

Here is the short working DEMO:

$(document).ready(function () {
	//Source data definition	
	var tableData = [{
			name: 'Clark Kent',
			city: 'Metropolis',
			race: 'cryptonian'
		}, {
			name: 'Bruce Wayne',
			city: 'Gotham',
			race: 'human'
		}, {
			name: 'Steve Rogers',
			city: 'New York',
			race: 'superhuman'
		}, {
			name: 'Peter Parker',
			city: 'New York',
			race: 'superhuman'
		}, {
			name: 'Thor Odinson',
			city: 'Asgard',
			race: 'god'
		}, {
			name: 'Jonathan Osterman',
			city: 'New York',
			race: 'superhuman'
		}, {
			name: 'Walter Kovacs',
			city: 'New Jersey',
			race: 'human'
		}, {
			name: 'Arthur Curry',
			city: 'Atlantis',
			race: 'superhuman'
		}, {
			name: 'Tony Stark',
			city: 'New York',
			race: 'human'
		}, {
			name: 'Scott Lang',
			city: 'Coral Gables',
			race: 'human'
		}, {
			name: 'Bruce Banner',
			city: 'New York',
			race: 'superhuman'
		}
	];
	//DataTable definition	
	window.dataTable = $('#mytable').DataTable({
			sDom: 'tF',
			data: tableData,
			columns: [{
					data: 'name',
					title: 'Name'
				}, {
					data: 'city',
					title: 'City'
				}, {
					data: 'race',
					title: 'Race'
		
			}]
	});

});
<!doctype html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.mfilter.tk/js/mfilter.min.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
  <link rel="stylesheet" type="text/css" href="https://cdn.mfilter.tk/css/mfilter.min.css">
</head>
<body>
  <table id="mytable"></table>
</body>
</html>

3 Comments

Thanks for posting but your example probably doesn't work properly, there are no column filtering elements.
Click filter icon
There is no filter icon, see this screenshot.

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.