Link to home
Start Free TrialLog in
Avatar of Techsavy
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

<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>

Open in new window



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>' )
        } );
    } );
} );

Open in new window

Avatar of Rob
Rob
Flag of Australia image

Something like this: http://jsbin.com/gitaz/1/

I've removed your tfoot and put the contents above the headers in the thead:

			<thead>
				<tr>
					<td>Name</td>
					<td>col 2</td>
					<td>col 3</td>
				</tr>
				<tr>
					<th>
						Name
					</th>
					<th>
						col 2
					</th>
					<th>
						col 3
					</th>
				</tr>
			</thead>

Open in new window


Then just change your js:
$("#myTable thead td").each(...)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Techsavy
Techsavy

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?
Strange... what browser are you viewing it with? I'm using chrome and I'm not seeing that
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.
Very Easy to follow