Techsavy
asked on
jQuery Datatables - table filter position help
hi,
I am using Jquery datatables for filtering table contents using table headers. the api is here:
http://www.datatables.net/
The issue I am having is that, the table filters are positioned at the bottom of the table.
I have attached the table structure and the script. Can anyone please suggest a remedy so that the filters show right above the table headers and also, I need a way to clear the filters too.
Table Structure
Script:
I am using Jquery datatables for filtering table contents using table headers. the api is here:
http://www.datatables.net/
The issue I am having is that, the table filters are positioned at the bottom of the table.
I have attached the table structure and the script. Can anyone please suggest a remedy so that the filters show right above the table headers and also, I need a way to clear the filters too.
Table Structure
<table id="myTable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>
Name
</th>
<th>
col 2
</th>
<th>
col 3
</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>col 2</th>
<th>col 3</th>
</tr>
</tfoot>
<tbody>
<tr>
......
</tr>
<tr>
....
</tr>
</tbody>
</table>
Script:
$(document).ready(function() {
var table = $('#myTable').DataTable(
{
"dom": '<"wrapper"ipftl>'
}
);
$("#myTable tfoot th").each( function ( i ) {
var select = $('<select><option value=""></option></select>')
.prependTo( $(this).empty() )
.on( 'change', function () {
var val = $(this).val();
table.column( i )
.search( val ? '^'+$(this).val()+'$' : val, true, false )
.draw();
} );
table.column( i ).data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
} );
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
Thank you for the solution.
When I implement this the Click event on "clear filters" button, it is clearing the dropdown controls as well. I need to refresh the page to bring back the controls.
Any thoughts?
Thank you for the solution.
When I implement this the Click event on "clear filters" button, it is clearing the dropdown controls as well. I need to refresh the page to bring back the controls.
Any thoughts?
Strange... what browser are you viewing it with? I'm using chrome and I'm not seeing that
ASKER
I see this behavior on any browser. It may be because I am using this script on a sharepoint aspx page.
I was able to make a workaround by adding window.reload() that will work for me.
Thank you for your solution.
I was able to make a workaround by adding window.reload() that will work for me.
Thank you for your solution.
ASKER
Very Easy to follow
I've removed your tfoot and put the contents above the headers in the thead:
Open in new window
Then just change your js:
Open in new window