Home > Microsoft SharePoint, SharePoint2013 > SharePoint 2013: How to change the group rendering in JSLink

SharePoint 2013: How to change the group rendering in JSLink

Hi Friends,

It was very painful to change the list view web parts UI using XSLT changes in MOSS and SP2010. A new exciting client side rendering (CSR) feature provided in SharePoint 2013. We have seen it is pretty good and easy to implement in Content Search web part with display templates. Another addition is for list view rendering and it is called as JS link. Using JS link we can change the list view rendering with a JavaScript file easily. It will also help to debug the list view rendering from client side and easy to develop JavaScript than XSLT.

Scenario:  If we added a grouping condition in list view and we want to change the default rendering of grouping.A requirement to change the rendering of the group in JSLink.I tried multiple options and not able to found any help related to group rendering. After debugging in developer tool I found a way.

Override group template using following line of JS code-

overrideCtx.Templates.Group = CustomGroup;

Add following function which will change the group rendering-

function CustomGroup(ctx, group, groupId, listItem, listSchema, level, expand) {

var html = '<div style="font-weight:bold">' + listItem[group] + ' :: </div>';

return html;

}

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

Happy coding….

Advertisements
  1. November 2, 2013 at 1:55 pm

    Great find on the Group method!

    If folks are looking for a good starting point to CSR or JS Link from the browser side (non-VS) I’ve got a post here as well:
    http://www.idubbs.com/blog/2012/js-link-for-sharepoint-2013-web-partsa-quick-functional-primer/

    • IJ
      January 27, 2014 at 9:55 am

      Great post!
      But I got an empty body. I can see html that I return in CustomGroup function but not the list of items. Could you help me ? Here is my code:

      (function () {
      var overrideCtx = {};
      overrideCtx.Templates = {};
      overrideCtx.Templates.Group = CustomGroup;
      SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
      })();

      function CustomGroup(ctx, group, groupId, listItem, listSchema, level, expand)
      {
      var html = ‘test ‘ +listItem[group]+ ‘ : ‘;
      return html;
      }

      Thanks IJ

  2. January 28, 2014 at 6:28 am

    Hi IJ,

    We are clearing overrideCtx.Templates first and the setting new templates in next steps.
    so we need to provide item template with group template like following-

    overrideCtx.Templates.Item = customItem;

    function customItem(ctx) {
    // Build a listitem entry for every announcement in the list.
    var ret = “

    ” + ctx.CurrentItem.Title + “

    “;
    return ret;
    }

    Regards,
    Prasad Pathak

  3. IJ
    February 3, 2014 at 8:00 pm

    Thank you for share these informations . They’re very useful!
    Another little question : is it possible in “customItem” or in “onpostreaderFunc” function to get the current user logged in ?
    Thanks in advance for your help!
    IJ

  4. IJ
    February 4, 2014 at 5:15 pm

    Solution :
    _spPageContextInfo.UserId
    Thanks

  5. April 28, 2014 at 9:28 pm

    This seems to be exactly what I need – I want to add an icon next to each group header. However, when I add the code to my list view, each group header displays as [object Object] with no items beneath. I’ve tried loading it as its own function and adding it to my existing function that renders list items. I even tried loading your code completely by itself, without any other code in the JS file.

    It’s probably something I’m doing wrong, but any ideas where I should look?

    • April 29, 2014 at 4:42 am

      Hi Omegacron,

      I have tried and working with following code –

      function CustomGroup(ctx, group, groupId, listItem, listSchema, level, expand) 
      {
      var html = '<div style="background-image:url(http://www.test.com/myImage.jpg);width:10px"></div><div style="font-weight:bold">' + listItem[group] + ' :: </div>';
      return html;
      }

      Also you can add a debugger in IE developer tool and check, what exactly [object Object] you are getting and which property of the object to use for display.

      Regards,
      Prasad Pathak

      • April 29, 2014 at 1:53 pm

        I think maybe it’s the way I’m declaring my object. I’m trying something like this:

        
        (function () {
        	var overrideCtx = {};
        	overrideCtx.Templates = {};
        	overrideCtx.Templates.Group = RenderGroup;
        	SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
        }) ();
        
        function RenderGroup(ctx, group, groupId, listItem, listSchema, level, expand) {
        	var html = "";
        	html += "";
        	html += "" + listItem[group] + " :: ";
        	return html;
        }
        
  6. April 29, 2014 at 1:57 pm

    Oops, lost my html tags there. Same basic div structure you had, though.

  7. April 29, 2014 at 2:00 pm

    Also, the field I’m grouping by in the list view is a lookup column. Will that make a difference?

  8. Rao
    February 9, 2015 at 4:23 pm

    Could you please provide the full code for this ? I am not sure what I am doing wrong in this. Please have a look at my code and suggest me. Thanks in advance !

    /*
    * This function builds the output for the item template.
    * Uses the Context object to access announcement data.
    */
    function CustomItem(ctx) {
    // Build a listitem entry for every announcement in the list.
    var ret = + ctx.CurrentItem.Title;
    return ret;
    };

    function CustomGroup(ctx, group, groupId, listItem, listSchema, level, expand) {
    var html = ” + listItem[group] + ‘ :’;

    return html;
    };

    (function () {
    /*
    * Initialize the variable that store the overrides objects.
    */
    var overrideCtx = {};
    overrideCtx.Templates = {};

    // Assign functions or plain html strings to the templateset objects:

    // This template is assigned to the CustomItem function.

    overrideCtx.Templates.Group = CustomGroup;
    overrideCtx.Templates.Item = CustomItem;

    overrideCtx.BaseViewID = 1;
    overrideCtx.ListTemplateType = 101;

    // Register the template overrides.

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

    })();

  9. Rao
    February 9, 2015 at 5:27 pm

    It worked. After reading your another article and the small mathematical correction in var ret. (i.e., var ret += ctx.CurrentItem.Title;) Thanks. 🙂

    https://prasadpathak.wordpress.com/2014/07/10/sharepoint-2013-item-template-not-called-in-jslink-with-group-rendering/

  1. July 10, 2014 at 5:20 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: