Tuesday 21 February 2017

"No items" message in CSR - SharePoint 2013

CSR (Client Side Rendering) is one of the most powerful yet simple tools to modify list views as per the client's wish. Recently while I was implementing a CSR script, during testing I found that when the list filter view had no items, the page was looking empty and did not have any message to inform the user that there are no items.
On some research, it was found that the default "no items" message is part of the footer and not the body. Hence, we need to check the row length and then set the message accordingly in the footer function. Below is the code for the same.

(function () {
    var overrideCtx = {};
    overrideCtx.Templates = {};
    
    overrideCtx.Templates.Footer = CustomFooter;
}}();

function CFooter(ctx) {
var html = "";
if(ctx.ListData.Row.length > 0)
{
html = Your custom footer html comes here
}
else
{
html = "<div>There are no items to show.</div>";
}

This will ensure that the end user will not see an empty page when there are no items.

No comments:

Post a Comment