While working one of the requirements I came across a scenario where I needed to call a jQuery function after loading all the rows from the data view. I implemented it using OnPostRender.
After that I was thinking of integrating JSLink with some of the very useful jQuery plugins. In this article I am showing how to integrate a jQuery table sorter plugin with SharePoint 2013 JSLink. I have used the tablesorter plugin by Christian Bach for demo purposes.
http://mottie.github.io/tablesorter/docs/index.html
Download the package from the preceding maintained table sorter site and upload the required CSS and JavaScript under the SiteAssets folder on the root site.
I have created a custom list with the following columns. Added these columns in default view. You can create a separate view also.
Then added some test contents to show the functionality working.
Created one JSLink (Student_Filter_Script.js) file that will alter the UI of the view. The JavaScript Link file is as in the following.
- (function () {
- var overrideCtx = {};
- overrideCtx.Templates = {};
- overrideCtx.Templates.Header = "<div class='pager'>Page: <select class='gotoPage'></select><img src='/sites/Demo/SiteAssets/addons/pager/icons/first.png' class='first' alt='First' title='First page' /><img src='/sites/Demo/SiteAssets/addons/pager/icons/prev.png' class='prev' alt='Prev' title='Previous page' /><span class='pagedisplay'></span> <!-- this can be any element, including an input --><img src='/sites/Demo/SiteAssets/addons/pager/icons/next.png' class='next' alt='Next' title='Next page' /><img src='/sites/Demo/SiteAssets/addons/pager/icons/last.png' class='last' alt='Last' title= 'Last page' /><select class='pagesize'><option selected='selected' value='10'>10</option><option value='20'>20</option><option value='30'>30</option><option value='40'>40</option></select></div><table id='myTable' class='tablesorter'><thead><tr><th>Name</th><th>Major</th><th>Sex</th><th>English</th><th>Hindi</th><th>Calculus</th><th>Geometry</th></tr></thead><tfoot><tr><th>Name</th><th>Major</th><th>Sex</th><th>English</th><th>Hindi</th><th>Calculus</th><th>Geometry</th></tr></tfoot><tbody>";
- overrideCtx.Templates.Footer = "</tbody></table><div class='pager'>Page: <select class='gotoPage'></select><img src='/sites/Demo/SiteAssets/addons/pager/icons/first.png' class='first' alt='First' title='First page' /><img src='/sites/Demo/SiteAssets/addons/pager/icons/prev.png' class='prev' alt='Prev' title='Previous page' /><span class='pagedisplay'></span> <!-- this can be any element, including an input --><img src='/sites/Demo/SiteAssets/addons/pager/icons/next.png' class='next' alt='Next' title='Next page' /><img src='/sites/Demo/SiteAssets/addons/pager/icons/last.png' class='last' alt='Last' title= 'Last page' /><select class='pagesize'><option selected='selected' value='10'>10</option><option value='20'>20</option><option value='30'>30</option><option value='40'>40</option></select></div>";
- overrideCtx.Templates.Item = CustomDemoItem;
- overrideCtx.OnPostRender = [];
- overrideCtx.OnPostRender.push(function(){
- $(function()
- {
-
- var pagerOptions = {
-
- container: $(".pager"),
-
- output: '{startRow} - {endRow} / {filteredRows} ({totalRows})',
-
-
- fixedHeight: true,
-
-
- removeRows: false,
-
- cssGoto: '.gotoPage'
- };
-
-
-
- $("table")
- .tablesorter({
- theme: 'blue',
- headerTemplate : '{content} {icon}',
- widthFixed: true,
- widgets: ['zebra', 'filter']
- })
-
-
-
- .tablesorterPager(pagerOptions);
-
- });
- });
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
-
- function CustomDemoItem(ctx)
- {
- var html = "";
- html = "<tr>";
- html += "<td>" + ctx.CurrentItem.Title + "</td>";
- html += "<td>" + ctx.CurrentItem.Name + "</td>";
- html += "<td>" + ctx.CurrentItem.Sex + "</td>";
- html += "<td>" + ctx.CurrentItem.English + "</td>";
- html += "<td>" + ctx.CurrentItem.Hindi + "</td>";
- html += "<td>" + ctx.CurrentItem.Calculus + "</td>";
- html += "<td>" + ctx.CurrentItem.Geometry + "</td>";
- html += "</tr>";
- return html;
- }
I uploaded this JavaScript link file in the master page gallery with properties as shown in the following image.
I added references for the following CSS and JavaScript files in the page layout. I added that in the page layout but you can add it to the master page depending on your requirements.
- <!-- jQuery file -->
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
- <!-- css stylesheet refrences -->
- <link href="/sites/Demo/SiteAssets/css/theme.blue.css" rel="stylesheet" type="text/css" />
- <link href="/sites/Demo/SiteAssets/addons/pager/jquery.tablesorter.pager.css" rel="stylesheet" type="text/css" />
- <!-- Tablesorter js -->
- <script type="text/javascript" src="/sites/Demo/SiteAssets/js/jquery.tablesorter.js"></script>
- <script type="text/javascript" src="/sites/Demo/SiteAssets/addons/pager/jquery.tablesorter.pager.js"></script>
- <script type="text/javascript" src="/sites/Demo/SiteAssets/js/jquery.tablesorter.widgets.js"></script>
Edit the page to add a list view web part to the page and edit the XSLTListView web part to set the newly created JavaScript link file.
Set the value as in the following.
- ~sitecollection/_catalogs/masterpage/JS_Link/Student_Filter_Script.js
Save the web part and you will see the beautiful effect of smooth client-side sorting pagination, filtering and sorting using a JSLink file.
Note
The complete contribution, development and license rights remain with the team that have developed the tablesorter plugin. This article is just to show the integration of SharePoint 2013 JSLink with the jQuery plugin.