0

I have a task list with a calculated column that includes some code to change the background color of the list items based on their priority value, see below

enter image description here

Is there a way to hide the calculated column from the view but still have it run its code, if I remove the column from the view then obviously it won't run the code to color the list

Any help would be appreciated, thanks

=[Priority] 
&"<img src=""/_layouts/images/blank.gif"" onload=""{" 
&"var row=this;while(row.tagName!='TR'){row=row.parentNode}"
&"row.style.backgroundColor='#" 
&CHOOSE( RIGHT( LEFT( [Priority] , 2) , 1) , "F78181" , "BCF5A9" , "F3F781" , "81B1F8") 
&"'}"">"
5
  • Why not use JSLink to do this? You don't need to show the calculated column then.
    – Akhoy
    Commented Oct 20, 2015 at 4:19
  • What is the code you have now in your Calculated Column? I have plenty of examples available I can add to your code. Commented Oct 21, 2015 at 9:47
  • @DannyEngelman the current code I have is =[Priority] &"<img src=""/_layouts/images/blank.gif"" onload=""{" &"var row=this;while(row.tagName!='TR'){row=row.parentNode}" &"row.style.backgroundColor='#" &CHOOSE( RIGHT( LEFT( [Priority] , 2) , 1) , "F78181" , "BCF5A9" , "F3F781" , "81B1F8") &"'}"">" do you have an example that I could add to this to hide the calculated column, I need to keep the original priority column as this is a drop down list that can be edited in quick edit mode
    – mlr
    Commented Nov 25, 2015 at 1:33
  • You want this on a page with ONE List View webpart? Commented Nov 25, 2015 at 12:38
  • @DannyEngelman Not sure what you mean, this list is a task list that contains data for project tasks, it is the only list/web part on the page, the idea is that there is a priority column so the task can be set high, normal or low and color coordinated accordingly, so I still need to keep the original Priority column as this will be changed/updated but obviously need the CC as well for the code
    – mlr
    Commented Nov 26, 2015 at 1:09

3 Answers 3

1

Why not exclude the original Priority1 Column from the list, and wrap your Calculated Column in

<div style="text-align:left">
 ...
</div>

Now that is something CSR can not do..
With CSR You need the value displayed in the View in order to work with it.

You loose the fance sorting and filtering, so I usually display another less meaningfull column in the Calculated Column

You can also leave it blank, and/or even add code to hide the whole column (after it executed)

If you are moving to CSR - Client Side Rendering

Then at least write a function as generic as possible

  • no fieldname dependency
  • no language dependency
  • no external Image file dependencies

    function priorityFieldTemplate(ctx) {
        var fieldValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name],
            colorIndex=fieldValue.match(/(\d+)/g),//(extract x from '(x) Label'
            color=['none','red','orange','green','brown'][colorIndex],
            circleSize=15,
            H="<svg height={0} width={0}><circle cx={1} cy={1} r={1} fill='{2}'/></svg>";
        return String.format( H , circleSize, circleSize/2, color);
    }
    

ICSR

3
  • With my script above I still can sort and filter by the status column... updated my answer with the picture of sorting and filtering
    – Patrick
    Commented Oct 20, 2015 at 8:24
  • Nice. Where will you wrap this calculated column btw?
    – Akhoy
    Commented Oct 20, 2015 at 8:34
  • Because the CC is datatype=Number it is right-aligned, so to get it aligned to the left again add a DIV to you exisiting code. You could also add JS code ``this.parentNode.style.textAlign='left'; Commented Oct 21, 2015 at 8:27
0

I had similar task. I had to show multiline text field/column shorten. I did that with additional column that was calculated field and I hid it with help of javascript. Add script webpart page and in it just hide that class and that is all.

0

As Akhoy says, I personally recommend js-link.

Here are two links where you should see how it is done:

https://code.msdn.microsoft.com/office/Client-side-rendering-code-0a786cdd

and a blog post from myself (Statuscolumn changed to traffic light icons):

https://blog.afrait.com/traffic-light-column-in-custom-list-en/

Here the script, tried to match your requirements a little:

<script>    
(function () {
        var statusFieldCtx = {};


        statusFieldCtx.Templates = {};
        statusFieldCtx.Templates.Fields = {
            "Priority1": {
                "View": StatusFieldViewTemplate
            }
        };

        SPClientTemplates.TemplateManager.RegisterTemplateOverrides(
            statusFieldCtx
            );
    })();

    function StatusFieldViewTemplate(ctx) {

        var _statusValue = ctx.CurrentItem.Priority1;

         if (_statusValue == '(1) High')
         {
             return "<img src='http://sp13/teams/PublishingImages/Ampel/red.png'/>";
         }

         if (_statusValue == '(2) Normal')
         {
             return "<img src='http://sp13/teams/PublishingImages/Ampel/yellow.png'/>";
         } 

         if (_statusValue == '(3) Low')
         {
             return "<img src='http://sp13/teams/PublishingImages/Ampel/green.png'/>";
         }   

         if (_statusValue == '(4) Done')
         {
         return "<img src='http://sp13/teams/PublishingImages/Ampel/brown.png'/>";
         } 

    }
</script>

Maybe you will decide to choose js-link in the future. Also you can still sort and filter by the status column. See here:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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