You might have seen the Out of the box SharePoint list view web parts showing customized column formatting for fields like status, priority and approval etc. The old way was to write a custom xslt template for the list view which can be used on list view properties to show customize output. This require too much custom effort and can risk your existing functionality.
There is another way to customize the list view web parts by simple script commands using JSlink, below I am sharing the code for a simple JSLink file which can be used for formatting the list column output.
(function () { var priorityFiledContext = {}; priorityFiledContext.Templates = {}; priorityFiledContext.Templates.Fields = { "Overall_x0020_Health": { "View": priorityFiledTemplate } }; SPClientTemplates.TemplateManager.RegisterTemplateOverrides(priorityFiledContext); })(); function priorityFiledTemplate(ctx) { var priority = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; switch (priority) { case "Red": return "<span style='width:50px; background-color: red'> </span>" break;px case "Yellow": return "<span style='width:50px; background-color : yellow'> </span>" break; case "Green": return "<span style='width:50px; background-color : green'> </span>" } }
Below are the steps to add a JSlink file to list view web part.
- Edit the list view page, this will show the list web part in edit able mode.
- Open the web part properties for list view web part.
- Scroll down and expand the Miscellaneous node and enter the url of JSLink file as shown in below screenshot
- Press Ok to save the changes, and then save changes on the page and you can see output similar to below screenshot which showing different color codes status field.
You can also add image icons instead of color code by modifying the JSlink script.
No Comments