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:
Now, add some columns to display in our list.
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:
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).
- <!DOCTYPE html>
- <html>
-
- <head>
- <title>WiFi Home</title>
- <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
- <script type="text/javascript" src="/SiteAssets/GetData_Wifi.js"></script>
- <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
- <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.12/css/dataTables.jqueryui.min.css">
- <script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
- </head>
-
- <body>
- <table id="table_id" class="display" cellspacing="0" width="100%">
- <thead>
- <tr>
- <th>Name</th>
- <th>Position</th>
- <th>Office</th>
- <th>Age</th>
- <th width="18%">Start date</th>
- <th>Salary</th>
- </tr>
- </thead>
- <tfoot> </tfoot>
- </table>
- </div>
- </body>
-
- </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.
- function loadMyItems() {
- var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
- var oDataUrl = siteUrl + "/_api/web/lists/getbytitle('Employee')/items?$select=Name,Position,Office,Age,StartDate,Salary";
- $.ajax({
- url: oDataUrl,
- type: "GET",
- dataType: "json",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: mySuccHandler,
- error: myErrHandler
- });
- }
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).
- function mySuccHandler(data) {
- try {
- var dataTableExample = $('#table_id').DataTable();
- if (dataTableExample != 'undefined') {
- dataTableExample.destroy();
- }
- dataTableExample = $('#table_id').DataTable({
- scrollY: 300,
- "aaData": data.d.results,
- "aoColumns": [{
- "mData": "Name"
- }, {
- "mData": "Position"
- }, {
- "mData": "Office"
- }, {
- "mData": "Age"
- }, {
- "mData": "StartDate",
- "render": function(mData) {
- var date = new Date(mData);
- var month = date.getMonth() + 1;
- return (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear();
- }
- }, {
- "mData": "Salary",
- "render": function(mData) {
- var sal = new Object(mData);
- var commaSep = mData.toString().split(".");
- commaSep[0] = commaSep[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- sal.rup = "<span>₹</span>";
- return ((sal.rup) + " " + commaSep.join("."));
- }
- }]
- });
- } catch (e) {
- alert(e.message);
- }
- }
-
- function myErrHandler(data, errMessage) {
- alert("Error: " + errMessage);
- }
Our final JS file will look, as follows:
- $(document).ready(function() {
- loadMyItems();
- });
-
- function loadMyItems() {
- var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
- var oDataUrl = siteUrl + "/_api/web/lists/getbytitle('Employee')/items?$select=Name,Position,Office,Age,StartDate,Salary";
- $.ajax({
- url: oDataUrl,
- type: "GET",
- dataType: "json",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: mySuccHandler,
- error: myErrHandler
- });
- }
-
- function mySuccHandler(data) {
- try {
- var dataTableExample = $('#table_id').DataTable();
- if (dataTableExample != 'undefined') {
- dataTableExample.destroy();
- }
- dataTableExample = $('#table_id').DataTable({
- scrollY: 300,
- "aaData": data.d.results,
- "aoColumns": [{
- "mData": "Name"
- }, {
- "mData": "Position"
- }, {
- "mData": "Office"
- }, {
- "mData": "Age"
- }, {
- "mData": "StartDate",
- "render": function(mData) {
- var date = new Date(mData);
- var month = date.getMonth() + 1;
- return (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear();
- }
- }, {
- "mData": "Salary",
- "render": function(mData) {
- var sal = new Object(mData);
- var commaSep = mData.toString().split(".");
- commaSep[0] = commaSep[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- sal.rup = "<span>₹</span>";
- return ((sal.rup) + " " + commaSep.join("."));
- }
- }]
- });
- } catch (e) {
- alert(e.message);
- }
- }
-
- function myErrHandler(data, errCode, errMessage) {
- alert("Error: " + errMessage);
- }
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.
Click OK and save the page.
Now, our final output is shown below:
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.