This post was most recently updated on August 2nd, 2024
Below I share code to apply filter option to SharePoint list using the jquery data table.
Steps: Page level
- Create page and add following datatable css and js.<link rel=”stylesheet” type=”text/css” href=”https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css” />
<script src=”https://code.jquery.com/jquery-1.12.4.js” type=”text/javascript”></script>
<script type=”text/javascript” src=”https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js”></script> - Create table with header according to SharePoint list. <div class=”Filters”><div><span>ID FIlter</span><span class=”idFilterOption”></span> <span>Adjustment Type FIlter</span><span class=”requestFilterOption”></span></div></div>
<table id=”table_id” class=”display” role=”grid” width=”100%” cellspacing=”0″>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Adjustment Type</th>
<th>Adjustment Requested</th>
<th>Unique No</th>
</tr>
</thead>
</table>
Steps: JS Level
Step 1: We need to get item count of threshold exceed list/normal SharePoint list by GetItemCount () function.
1234567891011121314151617//Step 1. get total item count of threshold exceed list.function GetItemCount(siteurl, ListName){var ItemCount='';$.ajax({url: siteurl+"/_api/web/lists/GetByTitle('"+ListName+"')/ItemCount",method: "GET",async: false,headers: { "Accept": "application/json; odata=verbose" },success: function (data) {ItemCount = data.d.ItemCount;},error: function (data) {console.log(data);}});return ItemCount;}- Step 2: Next step create rest service url by splitting rest call according to item count of GetItemCount () function. Check item count if item count is above 5000 then we split calling with 1000 item per call top=1000 for better performance. Otherwise we call as usual i.e. top=5000.//Step 2. create rest service url if item count is above 5000 then we split calling with 1000 item per call top=1000 for better performance. Otherwise we call as usual 5000 items top=5000
12345678910111213function createRestUrl(siteurl, ItemCount, ListName) {if(ItemCount<=5000) {//Item count less than 5000 so we limit it as usual 5000var listServiceUrl = siteurl+ "/_api/web/lists/GetByTitle('" + ListName + "')/Items?$select=Title,ID&$top=5000";} else {//Item count more than 5000 so we split it in 1000 item per callvar listServiceUrl = siteurl+ "/_api/web/lists/GetByTitle('" + ListName + "')/Items?$select=Title,ID&$top=1000";}//Step 3: Rest call to procerss each items of list$.when(processList(listServiceUrl,ItemCount)).done(function () { });} - Step 3: Process each item of threshold list by repeatedly calling getJSONDataFromUrl () function. GetJSONDataFromUrl () function gives Ajax json object. //Step 3: Rest call to procerss each items of list
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091function processList(nextUrl,ItemCount) {var dfd = new $.Deferred();if (nextUrl == undefined) {dfd.resolve();return;}//Step 4: Repetative call to getJSONDataFromUrl() to get Ajax Json object.getJSONDataFromUrl(nextUrl).done(function (listItems) {TotalItemCount = TotalItemCount+listItems.d.results.length;var items = listItems.d.results;var next = listItems.d.__next;$.when(getLargeList(next,ItemCount)).done(function (){dfd.resolve();});var tableTdHTML='';//Process each column data into tableTDHTML$.each(items, function(index, obj){if ($.inArray(obj.ID, idArray)== -1) //push unique id in idArrayidArray.push(obj.ID);if ($.inArray(obj.AdjustmentType, AdjustmentTypeArray)== -1) // push unique Adjustment Type into AdjustmentTypeArrayAdjustmentTypeArray.push(obj.AdjustmentType);tableTdHTML +='<tr><td>'+obj.ID+'</td><td>'+obj.Title+'</td><td>'+obj.AdjustmentType+'</td><td>'+obj.AdjustmentRequested+'</td><td>'+obj.Age+'</td></tr>';});//Create datatable objectvar table = $('#table_id').DataTable({//Process each filter array data and append to respective filter htmlinitComplete: function () {this.api().columns([0,2]).every( function (index) { //[0,2] indicate column header positionvar column = this;if(index === 0){var column = this;var select = $('<select><option value="">Show all</option></select>').appendTo( $('.idFilterOption').empty() ).on( 'change', function () {var val = $.fn.dataTable.util.escapeRegex($(this).val());column.search( val ? '^'+val+'$' : '', true, false ).draw();});$.each(idArray, function (index, obj) {select.append( '<option value="'+obj+'">'+obj+'</option>' );});}if(index === 2){var select = $('<select><option value="">Show all</option></select>').appendTo( $('.requestFilterOption').empty() ).on( 'change', function () {var val = $.fn.dataTable.util.escapeRegex($(this).val());column.search( val ? '^'+val+'$' : '', true, false ).draw();});$.each(AdjustmentTypeArray, function ( d, j ) {select.append( '<option value="'+j+'">'+j+'</option>' );});}});}});table.rows.add($(tableTdHTML)).draw(); //add row dynamically into datatables object});return dfd.promise();}//Step 4: Repetative call to getJSONDataFromUrl() to get Ajax Json object.function getJSONDataFromUrl(endpoint) {return jQuery.ajax({url: endpoint,method: "GET",headers: {"Accept": "application/json; odata=verbose","Content-Type": "application/json; odata=verbose"}});}
Final Screen shot without filtration
Screen Shot with filter
Reference:
getLargeList function is missing and also both arrays have not been defined.
Yilmaz, thank you for your feedback, we will soon update the article.