Solved

jQuery Datatables - table filter position help

Posted on 2014-07-21
6
1,596 Views
Last Modified: 2014-07-26
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

0
Comment
Question by:Techsavy
  • 3
  • 3
6 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40210850
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

0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 40210862
I've updated it here: http://jsbin.com/gitaz/5/

It now includes a clear filter button that clears the dropdown boxes and sets the filter to empty ""

$("#btnClear").on('click',function() {
		$('.filters').val([]);
		table.columns().search("");
		table.draw();
	});

Open in new window


<tfoot>
				<tr><td colspan='3'>
					<button id='btnClear'>Clear Filters</button>
				</td></tr>
			</tfoot>

Open in new window

0
 

Author Comment

by:Techsavy
ID: 40212160
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?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40213006
Strange... what browser are you viewing it with? I'm using chrome and I'm not seeing that
0
 

Author Comment

by:Techsavy
ID: 40221630
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.
0
 

Author Closing Comment

by:Techsavy
ID: 40221633
Very Easy to follow
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now