Skip to main content

SharePoint DataTable

 <script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.js"></script>

<script src="https://momentjs.com/downloads/moment.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css" />

<table id="requests" class="display" cellspacing="0" width="100%">

  <thead>

    <tr>

      <th>ID</th>

      <th>Business unit</th>

      <th>Category</th>

      <th>Status</th>

      <th>Due date</th>

      <th>Assigned to</th>

    </tr>

  </thead>

</table>

<script>

  // UMD

  (function(factory) {

    "use strict";


    if (typeof define === 'function' && define.amd) {

      // AMD

      define(['jquery'], function ($) {

        return factory( $, window, document );

      });

    }

    else if (typeof exports === 'object') {

      // CommonJS

      module.exports = function (root, $) {

        if (!root) {

          root = window;

        }


        if (!$) {

          $ = typeof window !== 'undefined'

            ? require('jquery')

            : require('jquery')( root );

        }


        return factory($, root, root.document);

      };

    } else {

      // Browser

      factory(jQuery, window, document);

    }

  }

  (function($, window, document) {

    $.fn.dataTable.render.moment = function (from, to, locale) {

      // Argument shifting

      if (arguments.length === 1) {

        locale = 'en';

        to = from;

        from = 'YYYY-MM-DD';

      } else if (arguments.length === 2) {

        locale = 'en';

      }


      return function (d, type, row) {

        var m = window.moment(d, from, locale, true);


        // Order and type get a number value from Moment, everything else

          // sees the rendered value

          return m.format(type === 'sort' || type === 'type' ? 'x' : to);

        };

      };

    }));

</script>

<script>

$(document).ready(function() {

  $('#requests').DataTable({

    'ajax': {

      'url': "../_api/web/lists/getbytitle('IT Requests')/items?$select=ID,BusinessUnit,Category,Status,DueDate,AssignedTo/Title&$expand=AssignedTo/Title",

      'headers': { 'Accept': 'application/json;odata=nometadata' },

      'dataSrc': function(data) {

        return data.value.map(function(item) {

          return [

            item.ID,

            item.BusinessUnit,

            item.Category,

            item.Status,

            new Date(item.DueDate),

            item.AssignedTo.Title

          ];

        });

      }

    },

    columnDefs: [{

      targets: 4,

      render: $.fn.dataTable.render.moment('YYYY/MM/DD')

    }]

  });

});

</script>

Ref: https://github.com/SachchinAnnam/DataTable-in-SharePoint

Comments