DataTables Column Filter in Header

To add column filter in DataTable you can go through my previous blog here. Now, we will move a step further and move column filter from footer to header.

Why do we need column filter in header?

If there are 50 to 100 records in a table then every time user needs to scroll down the page to use column filter. To make this more user friendly we can move column filter from footer to header.

The changes that we need to make for adding column filter in header is to move <tfoot> tag just after <thead> tag.

HTML:

<table id="courses">
	<thead>
		<tr>
			<th>Course Code</th>
			<th>Course Name</th>
			<th>Duration</th>
			<th>Status</th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th></th>
			<th></th>
			<th></th>
			<th></th>
		</tr>
	</tfoot>
	<tbody>
		@foreach($courses as $key => $value)
			<tr>
				<td>
				    <label>
					<input type="checkbox"
					name="selected_courses[]""
					value="{{$value['course_id']}}">
				    </label>
				    {{$value['course_code']}}
				</td>
				<td>{{$value['course_name']}}</td>
				<td>{{$value['duration']}}</td>
				<td>{{$value['status']}}</td>
			</tr>
		@endforeach
	</tbody>
</table>

This will output as:

columnFilterFooter

To move column filter from footer to header we need to add the following css:

tfoot {
    display: table-header-group;
}

The property display: table-header-group makes the <tfoot> element behave as a <thead> element.

columnFilterHeader

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s