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

Popular posts from this blog

The _spPageContextInfo

I f you are creating a SharePoint app using JavaScript and the Client side object model you need this friendly object. In the development of an app, you would require some basic properties- SharePoint as a framework provides these with the  _spPageContextInfo  object. _  spPageContextInfo  will provide these below properties:  webServerRelativeUrl  webAbsoluteUrl siteAbsoluteUrl serverRequestPath layoutsUrl webTitle webTemplate tenantAppVersion isAppWeb webLogoUrl webLanguage currentLanguage currentUICultureName currentCultureName env nid fid clientServerTimeDelta updateFormDigestPageLoaded siteClientTag crossDomainPhotosEnabled webUIVersion webPermMasks pagePersonalizationScope userId userLoginName systemUserKey alertsEnabled siteServerRelativeUrl allowSilverlightPrompt themedCssFolderUrl themedImageFileNames

Get Sum of Columns in a SharePoint list (Threshold edition)

It is a known fact that once the treshold limit is reached in SP, everything seems frozen.  At this point you have some options. Increase the limit using powershell or from the Central Admin OR Index the columns that you will like to operate on and create views based on those. However programmatically if you want to sum a list that has reached its threshold.  You can do so in batches as hown below:          protected   void  GetSummary( out   long  ideas,  out   long  votes,  out   long  comments,  out   long  transform)         {              long  ideasum = 0;              long columntoSum = 0;              long  commentS...