Solved

jQuery Datatables - table filter position help

Posted on 2014-07-21
6
1,756 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Industry Leaders: 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!

Question has a verified solution.

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

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This article discusses how to implement server side field validation and display customized error messages to the client.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 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