Solved

jQuery Datatables - table filter position help

Posted on 2014-07-21
6
1,480 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn how to dynamically set the form action using jQuery.

706 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