0

Has anyone managed to incorporate the setting of the viewstyle into a CSR?

I have come across the following links:

Using CSR to change row color - how do I keep the view style shaded view?

http://net4windows.blogspot.com.au/2013/10/client-side-rendering.html

http://www.codeproject.com/Articles/620110/SharePoint-Client-Side-Rendering-List-Views

I have tried the following code, and the viewstyle (in particular the alternate coloured lines) is not displaying. I have confirmed that 17 is the correct number for our site.

(function () {
   var ctx = {};
   ctx.ViewStyle = 17; 
   ctx.Templates = {};
   ctx.ViewStyle = 17;
   SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
   ctx.ViewStyle = 17;
})();

It would have been useful to have used the inbuild style, but is the only option to render the lines individually?

UPDATE:

The particular problem that I am having is that on the view, the fields have a Group clause. I have discovered that using the grouping with a CSR removes the column headings. If I remove the grouping from the view, the rendering works as expected.

So how do I add the columns titles back in with both the render and the grouping functions?

I have added in some prerender code to see what is in the header HTML (class="ms-listviewtable"), but can see no column headers. This seems to contradict what was on the codeproject website.

Anyway, I've tried some code according to the site below to try and process the Current column titles through code similar to that allItemsRenderAnnouncements(ctx) on the website

http://www.getcodesamples.com/src/D32FDC2E/C612E6F5

function allItemsRenderAnnouncements(ctx) {
  // create item style
  var itemStyle = "width:600px;margin:12px;";
  var titleStyle = "background-color:black;color:white;padding:2px; padding-left:12px;font-size:1.25em;border-top-left-radius:16px";
  var bodyStyle = "border:black 1px solid;background-color:#ddd;color:#333;padding:4px;border-bottom-right-radius:16px";

  // create and return HTML for each item
  return "<div style='" + itemStyle + "'>" +
         "<div style='" + titleStyle + "'>" + ctx.CurrentItem.Title    + "</div>" +
         "<div style='" + bodyStyle + "'>" + ctx.CurrentItem.Body + "</div>" +
     "</div>";
}

and the fields ctx.CurrentItem.Title are coming up as '.', which seems to be related to the fact that the table is grouped, therefore no column headings?????

So, no luck with that idea.

My next hope was that if the ctx.ViewStyle = 17; couldn't stick, then perhaps I could work out what was in the HTML before the CTX.TEMPLATES were cleared using a prerender, save it to a variable, then in the subsequent Templates.Header call (or postrender call), reinstate that value as it was before in the prerender state.

Alternatively I use jquery to somehow retrieve this value using some code like $(“table.ms-???? ....
then restore the settings with this value in the CSR postrender or .Header call?

I'm really lost on this one. A very frustrating problem, that should be so simple.

0

Part of the problem I was having was that the column headings were not coming out either.

Solutions to both problems was posted here:

http://www.codeproject.com/Articles/620110/SharePoint-Client-Side-Rendering-List-Views?msg=5123895#xx5123895xx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.