Display SharePoint List Data In Jquery Data Table

Introduction

Everybody knows that technology is changing rapidly. There are many things that we can integrate together. In this article, we are going to see, how to integrate the data table with SharePoint.

So, what is a data table?

As per Data Table’s official site definition:
“Data Table is a plug-in for the jQuery JavaScript library. It is a highly flexible tool, based upon the foundations of the progressive enhancement and will add the advanced interaction controls to any HTML table.”

Scenario

We are starting our scenario.

Before starting with this post, we must have some basic knowledge of the following:

  • HTML Tables
  • Rest API in SharePoint
  • JavaScript Objects

First of all, create a SharePoint list, where we are going to retrieve our data and display it in our data table. For this, go to your SPO site and create a custom list. In my case, my list name is Employee, given below:

Employee

Now, add some columns to display in our list.

column

Some of the default columns are always there, when we add our custom columns to the list. Now, add some dummy data to our list, as shown below:

list

Note: Change the list view as per your requirement.

Now, we move to the functionality part, that suggests how we can get the data from SharePoint list. For this, we need two files; one is HTML file to render the data and another one is JS file to get the data from SharePoint list.

Go to Data Table CDN to get the required JS and CSS files.

We need to follow CDN scripts, that are required to be used in our functionality,

Now, create the HTML file in a text editor (In our case, we are using Sublime Text 3).

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>WiFi Home</title>
  5. <script type=“text/javascript” src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js></script> //External js file to get data from SharePoint List
  6.     <script type=“text/javascript” src=“/SiteAssets/GetData_Wifi.js”></script>
  7.     <link rel=“stylesheet” type=“text/css” href=https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css>
  8.     <link rel=“stylesheet” type=“text/css” href=https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/dataTables.jqueryui.min.css>
  9.     <script type=“text/javascript” src=https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js></script>
  10. </head>
  11. <body>
  12.     <table id=“table_id” class=“display” cellspacing=“0” width=“100%”>
  13.         <thead>
  14.             <tr>
  15.                 <th>Name</th>
  16.                 <th>Position</th>
  17.                 <th>Office</th>
  18.                 <th>Age</th>
  19.                 <th width=“18%”>Start date</th>
  20.                 <th>Salary</th>
  21.             </tr>
  22.         </thead>
  23.         <tfoot> </tfoot>
  24.     </table>
  25.     </div>
  26. </body>
  27. </html>

Now, we have to create JavaScript file (GetData_Wifi.js) to get the data from SharePoint list

For this, write a function to get the data, using REST API.

  1. function loadMyItems() {
  2.     var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
  3.     var oDataUrl = siteUrl + “/_api/web/lists/getbytitle(‘Employee’)/items?$select=Name,Position,Office,Age,StartDate,Salary”;
  4.     $.ajax({
  5.         url: oDataUrl,
  6.         type: “GET”,
  7.         dataType: “json”,
  8.         headers: {
  9.             “accept”“application/json;odata=verbose”
  10.         },
  11.         success: mySuccHandler,
  12.         error: myErrHandler
  13.     });
  14. }

Now, the next thing is how to bind this data to our view (HTML) via data table?

For this, let’s put our success code in mySuccessHandler function (this is a part of asynchronous programming).

  1. function mySuccHandler(data) {
  2.     try {
  3.         var dataTableExample = $(‘#table_id’).DataTable();
  4.         if (dataTableExample != ‘undefined’) {
  5.             dataTableExample.destroy();
  6.         }
  7.         dataTableExample = $(‘#table_id’).DataTable({
  8.             scrollY: 300,
  9.             “aaData”: data.d.results,
  10.             “aoColumns”: [{
  11.                 “mData”“Name”
  12.             }, {
  13.                 “mData”“Position”
  14.             }, {
  15.                 “mData”“Office”
  16.             }, {
  17.                 “mData”“Age”
  18.             }, {
  19.                 “mData”“StartDate”,
  20.                 “render”: function(mData) {
  21.                     var date = new Date(mData);
  22.                     var month = date.getMonth() + 1;
  23.                     return (month.length > 1 ? month : “0” + month) + “/” + date.getDate() + “/” + date.getFullYear();
  24.                 }
  25.             }, {
  26.                 “mData”“Salary”,
  27.                 “render”: function(mData) {
  28.                     var sal = new Object(mData);
  29.                     var commaSep = mData.toString().split(“.”);
  30.                     commaSep[0] = commaSep[0].replace(/\B(?=(\d{3})+(?!\d))/g, “,”);
  31.                     sal.rup = “<span>₹</span>”;
  32.                     return ((sal.rup) + ” “ + commaSep.join(“.”));
  33.                 }
  34.             }]
  35.         });
  36.     } catch (e) {
  37.         alert(e.message);
  38.     }
  39. }
  40. function myErrHandler(data, errMessage) {
  41.     alert(“Error: “ + errMessage);
  42. }

Our final JS file will look, as follows:

  1. $(document).ready(function() {
  2.     loadMyItems();
  3. });
  4. function loadMyItems() {
  5.     var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
  6.     var oDataUrl = siteUrl + “/_api/web/lists/getbytitle(‘Employee’)/items?$select=Name,Position,Office,Age,StartDate,Salary”;
  7.     $.ajax({
  8.         url: oDataUrl,
  9.         type: “GET”,
  10.         dataType: “json”,
  11.         headers: {
  12.             “accept”“application/json;odata=verbose”
  13.         },
  14.         success: mySuccHandler,
  15.         error: myErrHandler
  16.     });
  17. }
  18. function mySuccHandler(data) {
  19.     try {
  20.         var dataTableExample = $(‘#table_id’).DataTable();
  21.         if (dataTableExample != ‘undefined’) {
  22.             dataTableExample.destroy();
  23.         }
  24.         dataTableExample = $(‘#table_id’).DataTable({
  25.             scrollY: 300,
  26.             “aaData”: data.d.results,
  27.             “aoColumns”: [{
  28.                 “mData”“Name”
  29.             }, {
  30.                 “mData”“Position”
  31.             }, {
  32.                 “mData”“Office”
  33.             }, {
  34.                 “mData”“Age”
  35.             }, {
  36.                 “mData”“StartDate”,
  37.                 “render”: function(mData) {
  38.                     var date = new Date(mData);
  39.                     var month = date.getMonth() + 1;
  40.                     return (month.length > 1 ? month : “0” + month) + “/” + date.getDate() + “/” + date.getFullYear();
  41.                 }
  42.             }, {
  43.                 “mData”“Salary”,
  44.                 “render”: function(mData) {
  45.                     var sal = new Object(mData);
  46.                     var commaSep = mData.toString().split(“.”);
  47.                     commaSep[0] = commaSep[0].replace(/\B(?=(\d{3})+(?!\d))/g, “,”);
  48.                     sal.rup = “<span>₹</span>”;
  49.                     return ((sal.rup) + ” “ + commaSep.join(“.”));
  50.                 }
  51.             }]
  52.         });
  53.     } catch (e) {
  54.         alert(e.message);
  55.     }
  56. }
  57. function myErrHandler(data, errCode, errMessage) {
  58.     alert(“Error: “ + errMessage);
  59. }

Now, move to SharePoint part. First, upload the above JavaScript and HTML file to Site Assets library of SharePoint or whatever library you want.

Create a Webpart Page (Named DataTable) and store it in the pages or the site pages library.

Add a content editor to DataTable Page. Now, give HTML file reference to the content editor into Content Editor.

Content Editor

Click OK and save the page.

Now, our final output is shown below:

output

It’s Cool!
Now, play with the UI, provided by the data table plugin.

Conclusion: Thus, in this way, we see how we can work with DataTables and SharePoint together.

Did I miss anything that you may think is required? Maybe this is useful for someone. I hope you like it. Please share your valuable suggestions and feedback.

This Post Has 3 Comments

  1. Hi, try to implement with Grouping list

    1. Didn’t get you what grouping list means.
      Please explain breafly…

Leave a Reply

Close Menu