26.06.2017 Views

Bootstrap for ASP.NET MVC

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using the jQuery DataTables Plugin with <strong>Bootstrap</strong><br />

The TableTools extension<br />

The TableTools extension <strong>for</strong> jQuery DataTables adds a toolbar at the top of the table<br />

with which the user can copy to clipboard, export to CSV, and print to PDF the data<br />

inside the DataTable. It is a really simple way to give your users the functionality to<br />

export their data.<br />

To use the TableTools extension, per<strong>for</strong>m the following steps:<br />

1. Create a new folder in the root of your project called swf.<br />

2. Add the copy_csv_xls_pdf.swf ile to the swf folder. The .swf ile is<br />

included in the DataTables plugin download.<br />

3. Open the _Layout.cshtml ile and add a reference to the dataTables.<br />

tableTools.css ile:<br />

<br />

4. Open the view .cshtml ile and add a reference to the DataTables,<br />

DataTables <strong>Bootstrap</strong>, and TableTools extension JavaScript iles:<br />

<br />

<br />

<br />

5. Lastly, using jQuery, add an event handler to initialize the DataTables plugin<br />

and the TableTools extension after the page has loaded:<br />

<br />

$(document).ready(function () {<br />

var table = $('.table').dataTable();<br />

var tt = new $.fn.dataTable.TableTools(table);<br />

$(tt.fnContainer()).insertBe<strong>for</strong>e('div.dataTables_wrapper');<br />

});<br />

<br />

[ 156 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!