Solved

jQuery Datatables - table filter position help

Posted on 2014-07-21
6
1,700 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 43

Expert Comment

by:Rob
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 43

Accepted Solution

by:
Rob 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Rob
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

679 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