Introduction
Read my first two post in order to get basics idea on Client Side rendering using JSLink.
- Client Side Rendering using JSLink – Post 01 – Starting Over
- Client Side Rendering using JSLink – Post 02 – Customize List Forms
In last two post we discussed basics on JSLink. Now lets look bit deeper. In this post I will illustrate; what are the fields we can override and use in template. I will give you examples for each and every property. Below are the properties that we can use to override in our custom template.
- Header
- Footer
- View
- Body
- Group
- Item
- Fields
- OnPreRender
- OnPostRender
I have created an announcement list instance in order to illustrate the examples. My list instance displays as follows. Please note that I have grouped the list items by Created.
I have linked my JavaScript file for the above list instance. Now lets get into the examples.
Header
Overrides the Header of the list instance. The header area is described by the below image.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- // Change the header
- overrideCtx.Templates.Header = "<b>This is a custom header</b>";
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
Results
Footer
We can add a footer in the list instance.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- // Change the footer
- overrideCtx.Templates.Footer = "<b>This is a custom footer</b>";
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
Results
Fields
Visit my first blog post and see the example. You can understand how to override the fields. If you want to retrieve the actual value of a field you can retrieve by “<#= ctx.CurrentItem.FieldName#>” or by ctx.CurrentItem.FieldName.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- overrideCtx.Templates.Fields = {
- 'Body': { 'View': '<div style="color:red; background-color: green"><#= ctx.CurrentItem.Body #></div>' }
- };
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
Results
Group
Overriding Group takes 7 parameters. Using those parameters you can change the look and feel of the group. I will just show you a basic.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- overrideCtx.Templates.Group = customGroup;
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
- function customGroup(ctx, group, groupId, listItem, listSchema, level, expand) {
- var html = '<div style="color:red">' + listItem[group] + ' </div>';
- return html;
- }
Results
Body
Overrides the body.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- overrideCtx.Templates.Body = customBody;
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
- function customBody(ctx) {
- return String.format("Hello world from {0}", ctx.ListTitle);
- }
Results
Item
The Item template points to the function that displays each Item in the list.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- overrideCtx.Templates.Header = "<B><#=ctx.ListTitle#></B>" +
- "<ul>";
- // This template is assigned to the CustomItem function.
- overrideCtx.Templates.Item = customItem;
- overrideCtx.Templates.Footer = "</ul>";
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
- // This function builds the output for the item template.
- // It uses the context object to access announcement data.
- function customItem(ctx) {
- // Build a listitem entry for every announcement in the list.
- var ret = "<li>" + ctx.CurrentItem.Title + "</li>";
- return ret;
- }
Results
OnPreRender
OnPreRender event fires before the DOM is loaded. In my example I will show how to set a custom list name.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- //Over ride the header
- overrideCtx.Templates.Header = "<#=ctx.ListTitle#>";
- //Set the List title on pre render event
- overrideCtx.OnPreRender = function a() {
- ctx.ListTitle = "Title from PreRender";
- };
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
Results
OnPostRender
OnPostRender event fires after DOM is loaded. So you can change the values after DOM load. Here in our example I have changed the background color.
Code
- (function () {
- // Initialize the variable that stores the objects.
- var overrideCtx = {};
- overrideCtx.Templates = {};
- //OnPostRender call postRenderHandler function.
- overrideCtx.OnPostRender = postRenderHandler;
- // Register the template overrides.
- SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
- })();
- function postRenderHandler(ctx) {
- var rows = ctx.ListData.Row;
- for (var i = 0; i < rows.length; i++) {
- var isApproved = rows[i]["Title"] == "Title 001";
- if (isApproved) {
- var rowElementId = GenerateIIDForListItem(ctx, rows[i]);
- var tr = document.getElementById(rowElementId);
- tr.style.backgroundColor = "#ada";
- }
- }
- }
Results
Conclusion
So the basics are covered and if you are familiar with JavaScript and CSOM you can do more on Client side rendering with JSLink. In future posts I will show some examples how you can use client side rendering.
Great post ! Thank you !
ReplyDeleteI would like to customize the header of a listview but keeping the header columns. How can I achieve this ? Where are the files SharePoint used to render header ?
Nice post. It has details information about customizing List using JS Link.
ReplyDeletewhat if i have two listviews on the same page and I want to render the second one however the first one is being rendered instead?
ReplyDeleteI think this post may help you. In case if it didn't workout just inform; I'll give a try.
Deletehttp://therelentlessfrontend.com/2014/05/01/having-multiple-jslink-based-webparts-on-the-same-page-overrides-all-the-other-templates-in-sp-2013/
Hi, I am trying to apply jslink to a that I created for the discussion list. I can-t get to work the overriding for Fields and neither Item. It never executes the code inside the functions. What can be wrong?
ReplyDeletefunction WaitRegisterCustomview(){
SP.SOD.executeOrDelayUntilScriptLoaded(_RegisterCustomView, 'clienttemplates.js');
}
function _RegisterCustomView() {
var viewContext = {};
viewContext.Templates = {};
viewContext.BaseViewID = 3;
viewContext.ListTemplateType = 108;
viewContext.Templates.Header = 'custom header';
viewContext.Templates.Fields = {
'Subcategory': { 'View':
function(ctx){
return 'custom text';
}
},
'Body':{'View':'Test body'}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(viewContext);
}
As I know JS link is not working for Discussion and Calendar. If you get any solutions please let me know.
DeleteJS Link will work for Calendar and discussion.Only thing is that it wont work for the Calendar View as such .It will very well work with the All Items view of the calendar. Same case with Discussion borad.
DeleteQuite Good post...!
ReplyDeleteThanks.
DeleteVery good post!
ReplyDeleteI am trying to figure out how to make a custom template for displaying a item, supposing it has 3 fields, Title, Shape and Color.
DIV.BOX
_H1 ctx.[].Title /H1
_DIV
__DIV.LEFT ctx.[].Shape /DIV
__DIV.RIGHT ctx.[].Color /DIV
_/DIV
/DIV
Do I have to conjugate
Templates.Body and Template.Item?
I am coding to SPO, no Server available to me in VSTO and I could not find MSDN / Technet references on SPClientTemplates.TemplateManager ...
Can u help me? any link would be appreciated.
Thank u!!
I am facing a problem with JSlink. I need to use both Field override in a group by view which is being rendered using Item Override . In short I am trying to use Field override in a view being rendered using Gruop overide and Item override.
ReplyDeleteField override alone is working for me. But when i use it with the Group over ride and Item override, it fails.
itemCtx.Templates.Header = headers;
itemCtx.Templates.Group = groupfun;
itemCtx.Templates.Item = itemfun;
itemCtx.Templates.Fields = { "Advisory": { "View": renderSymbol } };
Anyway someone can help ?I actually got a workaround,I removed the field override and handled the rendering of the field in item override function. But I need to know if its the case that either we should use field override or item override-and both donot work at one go ! .Thanks in advance.
ReplyDeleteHi, I want to customize list's fields. Item and header override works, but field overrode not working. Even the field override event handler not triggered.
ReplyDeletehi, i want to add data in multiline text type column, guide me how to do it using CSR.
ReplyDeleteIt is perfect time to make some plans for the future and it is time to be happy. I've read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I want to read more things about it!USB名入れ
ReplyDeletehi
ReplyDeleteIf I use this text "overrideCtx.OnPreRender = window.ImprovedListView.Paging.onPreRender;"
and overrideCtx.OnPostRender will not work.
Can you tell me why, thank you!
Is it possible to header calls an async function? Thanks
ReplyDelete