In this article we will show how you can add the custom column filters on the JQuery DataTable which retrieved the data from the SharePoint List using the REST Api.
In my previous article I showed you how to retrieve the data from the SharePoint list using REST api and bind it to the JQuery Datatable.
Articles on the Jquery DataTable and SharePoint REST API
- Load the Data in JQuery DataTable from SharePoint List using REST API
- Custom DropDown Column Filter in JQuery Datatable
- Free Text Search on Column in JQuery Datatable
- Get Data in JQuery DataTable from SharePoint List using $skip, $top, $inlinecount, $orderby parameters
We will use the same customers list we used in my previous article except that we will add two more columns to it viz, ‘Organization’ and ‘Role’. I have assigned some data to these columns for the existing records.
Make sure you add the organization and Role column in the js file to be retrieved from the SharePoint.
tableContent += '<td>' + objArray[i].Organization + '</td>';
tableContent += '<td>' + objArray[i].RolesValue + '</td>';
Let’s pick up our CustomerJqueryDatatable.txt and add the panel to hold our dropdowns for organization and roles column. You can place this code above our CustomerPanel div. <div id="filterPnl">
<table style="width:100%">
<tr>
<td style="width:50%;">Organization : <span id="orgDropDown"></span></td>
<td style="width:50%;">Roles : <span id="roleDropdown"></span></td>
</tr>
</table>
</div>
</br /><hr /> </br />
CustomerJqueryDatatable.txt should look something like this now.
<script type="text/javascript" src="../SiteAssets/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.9/css/jquery.dataTables.min.css" rel="Stylesheet" type="text/css" />
<script type="text/javascript" src="../SiteAssets/CustomerJqueryDatatable.js"></script>
<div id="filterPnl">
<table style="width:100%">
<tr>
<td style="width:50%;">Organization : <span id="orgDropDown"></span></td>
<td style="width:50%;">Roles : <span id="roleDropdown"></span></td>
</tr>
</table>
</div>
</br /><hr /> </br />
<div id="CustomerPanel">
<table style="width: 100%;">
<tr>
<td>
<div id="CustomerGrid" style="width: 100%"></div>
</td>
</tr>
</table>
</div>
Open the CustomerJqueryDatatable.js file in SharePoint Designer. Now here we will use the initComplete function and this.api().columns(column index)of the JQuery DataTable to get the column values for the organization and roles to bind it to our dropdown.
