Archive

Posts Tagged ‘jQuery’

Angular JS: Calling angular function from jQuery

Hi Friends,

Angular JS and jQuery are best companion and in most of the cases we need to use both of those to achieve some functionality.

I my case as well I was using jQuery UI tabs with Angular JS. Where tabs are manually added but in my case I wanted to call an angular function when tabs are getting changed.

The problem is tabs changed event was non-angular section and $scope was not available in the jQuery tab change event.

Solution:

We need to provide the ID property for controller div where we have defined ng-controller.

<div class="form-group" ng-controller="getTestCtrl" id="ctrlTest">

Then we can call the method from non-angular part of the JavaScript as well, in my case jQuery UI tabs change event.

angular.element('#ctrlTest').scope().GetTestData();

angular.element('#ctrlTest').scope().$apply();

Make sure to call $apply() method which triggers a $digest cycle after calling the function as we are not in Angular cope hence we need  to explicitly call $apply method to force the UI binding.

SharePoint 2013: jQuery table pagination sorter integration with JSLink

January 24, 2014 5 comments

Hi Friends,

While working one of the requirements I came across a scenario where I need to call a jQuery function after loading all the rows from data view. I implemented it using OnPostRender which I shown in my previous post.

After that I am thinking of integrating JSLink with some of the very useful jQuery plugin. In this post I am showing how to integrate a jQuery table sorter plugin with SharePoint 2013 JSLink.I have used tablesorter plugin by Christian Bach for demo purpose. http://mottie.github.io/tablesorter/docs/index.html

Download the package from above maintained table sorter site and upload the required css and js under SiteAssets folder on root site.

I have created a custom list with following columns. Added these columns in default view. You can create separate view also.

listsettings1

Added some test contents to show the functionality working.

listscontent

Created one JSLink(Student_Filter_Script.js) file which will alter the UI of the view. JS Link file is as 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(){ /* invoke your plugin code here to ensure the plugin has the content to work against */
 $(function(){

// define pager options
 var pagerOptions = {
 // target the pager markup - see the HTML block below
 container: $(".pager"),
 // output string - default is '{page}/{totalPages}'; possible variables: {page}, {totalPages}, {startRow}, {endRow} and {totalRows}
 output: '{startRow} - {endRow} / {filteredRows} ({totalRows})',
 // if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty
 // table row set to a height to compensate; default is false
 fixedHeight: true,
 // remove rows from the table to speed up the sort of large tables.
 // setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled.
 removeRows: false,
 // go to page selector - select dropdown that sets the current page
 cssGoto: '.gotoPage'
 };

// Initialize tablesorter
 // ***********************
 $("table")
 .tablesorter({
 theme: 'blue',
 headerTemplate : '{content} {icon}', // new in v2.7. Needed to add the bootstrap icon!
 widthFixed: true,
 widgets: ['zebra', 'filter']
 })

// initialize the pager plugin
 // ****************************
 .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;
}

Uploaded this JS link file in master page gallery with properties as shown in bellow image.

JsLink_MasterPageGallery

I have added references for following css and JavaScript files in page layout, I have added in page layout but you can add in master page according to your requirement.

<!-- 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>&nbsp;
<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 list view web part on page and edit the XSLTListView web part to set newly created JS link file.

XSLT_JSLink_ref

Set the value as 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.

JSLinkView

Note: complete contribution, development and license rights are with the team who have developed tablesorter plugin. This post is just to show integration of SharePoint 2013 JSLink with jQuery plugin.

SharePoint 2013: Calling jQuery function on rendering complete event in JSLink

December 2, 2013 7 comments

Hello Friends,

Once again a new finding I wanted to share with you related to JSLink in SharePoint 2013.

Scenario:  I am displaying some data from list using a XSLTListView web part in SharePoint 2013 page. I have selected JSLink option to give a matching UI to that data.Data is getting rendered properly with new UI using my custom JSLink file but I wanted to call a jQuery function after loading of all the rows gets completed so that my jQuery call will not fail and will get the rendered rows properly.

Solution:

We need to provide a handler on OnPostRender event or we can call the function directly as shown in following code, though no huge difference between those two –

Option 1 –

(function () {
 var overrideCtx = {};
 overrideCtx.Templates = {};
 overrideCtx.Templates.Header = "";
 overrideCtx.Templates.Footer = "";
 overrideCtx.Templates.Item = CustomDemoItem;
 overrideCtx.OnPostRender = [];
 overrideCtx.OnPostRender.push(function()
 {
 //call your ready function here
 });
 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

Option 2 –

(function () {
 var overrideCtx = {};
 overrideCtx.Templates = {};
 overrideCtx.Templates.Header = "";
 overrideCtx.Templates.Footer = "";
 overrideCtx.Templates.Item = CustomDemoItem;
 overrideCtx.OnPostRender = [];
 overrideCtx.OnPostRender.push(function(){OnPostRenderFunc(); });
 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

function OnPostRenderFunc(renderCtx) {
 //call your ready function here
}

Hope this will help some of the developer looking for same kind of answer.

Happy coding….