Datatable from SP REST API Object

$(document).ready(function () { var RestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('Document Information')/Items?&expand=FieldValueAsText"; $.ajax({ url: RestUrl, method: "GET", headers: { "accept": "application/json;odata=verbose", }, success: function (data) { if (data.d.results.length > 0) { $('#DatatableGrid').append(GenerateTableFromJson(data.d.results)); var oTable = $('#DataView').dataTable({ "iDisplayLength": 25, "sPaginationType": "full_numbers", "columnDefs": [{ "targets": 0, "data": "Test", "render": function (Test, type, full) { //Used to create Title link to document var n = Test.lastIndexOf("/"); var m = Test.lastIndexOf("."); var newTemp = Test.substring(n + 1, m); return "<a target='_blank' href='" + Test + "'><p>" + newTemp + "</p></a>"; } }, { "targets": 0, "className": "dt-center", orderable: false }, { "targets": 1, "className": "dt-center" }, { "targets": 2, "className": "dt-center" }, { "targets": 3, "className": "dt-center" }, { "targets": 4, "className": "dt-center" } ], }); } else { $('#DatatableGrid').append("<span>No information found.</span>"); } }, error: function (data) { $('#DatatableGrid').append("<span>Error Retrieving Information. Error : " + JSON.stringify(data) + "</span>"); } }); function GenerateTableFromJson(objArray) { var tableContent = '<table id="DataView" class="cell-border display "><thead><tr>' + '<th>Document Title</th>' + '<th>Project Location</font></th>' + '<th>Project Type</th>' + '<th>Document Type</th>' + '<th>Document Date</th>' + '</tr></thead>' + '<tbody>'; for (var i = 0; i < objArray.length; i++) { var AnyColumn = objArray[i].AnyColumn; tableContent += '<tr>'; tableContent += '<td>' + AnyColumn + '</td>'; tableContent += '</tr>'; } $('#DatatableGrid').empty(); return tableContent; } });
Used in conjunction with: https://codepad.co/snippet/TcI26USg

This is an outline for constructing a datatable from JSON information received from SharePoint's REST API.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.