0

I am using a code that's floating around forums like this to color code a sharepoint list. However if I switch to the grouped list view the code stops working. From research, it seems that is due to a ms-gb class that is added to the grouped view. Is there a way to use my current code and just add the class information somewhere to make it work?

 <script type="text/javascript">
 SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
      OnPostRender: function(ctx) {

        var statusColors =  {
           'My Stuff' : 'DarkSeaGreen',  
           'Blank' : '#FF7B61',
           'Test' : 'LightSteelBlue', 
           'Hello' : 'Plum', 
           'Boat' : 'YellowGreen',    
           'Mouse' : 'DarkGray' 
        };

        var rows = ctx.ListData.Row;
       for (var i=0;i<rows.length;i++)
        {
           var status = rows[i]["Color"];
           var rowId = GenerateIIDForListItem(ctx, rows[i]);
           var row = document.getElementById(rowId); 
           row.style.backgroundColor = statusColors[status];
        }
      }
    }); 

 });

 </script>

I don't know if its not finding the rows to loop through, or if its got the right row but not getting the right element to change.

1 Answer 1

0

This may not the simplest solution but it should work.

    <script type="text/javascript">
    SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function () {

        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
            OnPostRender: function (ctx) {

                var statusColors = {
                    'My Stuff': 'DarkSeaGreen',
                    'Blank': '#FF7B61',
                    'Test': 'LightSteelBlue',
                    'Hello': 'Plum',
                    'Boat': 'YellowGreen',
                    'Mouse': 'DarkGray'
                };

                var rows = ctx.ListData.Row;
                for (var i = 0; i < rows.length; i++) {
                    var status = rows[i]["Color"];
                    var rowId = GenerateIIDForListItem(ctx, rows[i]);
                    var row = document.getElementById(rowId);
                    row.style.backgroundColor = statusColors[status];
                }
            }
        });

    });

    SP.SOD.executeOrDelayUntilScriptLoaded(CustomJS, 'INIT.js');
    function CustomJS() {
        var _ExpCollGroup = ExpCollGroup;
        ExpCollGroup = ExpCollGroup = function (groupName, imgName, evt, noAjax) {
            if (evt != null)
                g_ExpGroupNeedsState = true;
            if (document.getElementById("titl" + groupName) == null)
                return;
            var viewTable = (document.getElementById("titl" + groupName)).parentNode;

            if (viewTable == null)
                return;
            var ctxNum = groupName.substr(0, groupName.indexOf("-"));
            var ctxCur = window["ctx" + ctxNum];

            if (!Boolean(ctxCur))
                return;
            if (!ctxCur.StateInitDone)
                ctxInitItemState(ctxCur);
            if (ctxCur.SelectAllCbx == null)
                ctxCur.SelectAllCbx = getSelectAllCbxFromTable(viewTable);
            var tbodyTags = viewTable.getElementsByTagName("TBODY");
            var numElts = tbodyTags.length;
            var len = groupName.length;
            var img = document.getElementById(imgName);

            if (img == null)
                return;
            var srcPath = img.src;
            var imgSrc = img.getAttribute('src');
            var index = srcPath.lastIndexOf("/");
            var commonClusterSrcPath = GetThemedImageUrl("spcommon.png");
            var collapseImgClass = fRightToLeft ? "ms-commentcollapsertl-icon" : "ms-commentcollapse-icon";
            var collapseSpanClass = fRightToLeft ? "ms-commentcollapsertl-iconouter" : "ms-commentcollapse-iconouter";
            var expandImgClass = fRightToLeft ? "ms-commentexpandrtl-icon" : "ms-commentexpand-icon";
            var expandSpanClass = fRightToLeft ? "ms-commentexpandrtl-iconouter" : "ms-commentexpand-iconouter";
            var oldExpandSrcPath = '/_layouts/15/images/plus.gif';
            var oldCollapseSrcPath = '/_layouts/15/images/minus.gif';
            var fOpen = false;
            var displayStr;

            if (imgSrc == oldExpandSrcPath || imgSrc == commonClusterSrcPath && img.className == expandImgClass || g_ExpInitializing) {
                fOpen = true;
                displayStr = "";
                if (imgSrc == commonClusterSrcPath) {
                    img.className = collapseImgClass;
                    img.parentNode.className = collapseSpanClass;
                }
                else
                    img.src = oldCollapseSrcPath;
            }
            else {
                fOpen = false;
                displayStr = "none";
                if (imgSrc == commonClusterSrcPath) {
                    img.className = expandImgClass;
                    img.parentNode.className = expandSpanClass;
                }
                else
                    img.src = oldExpandSrcPath;
            }
            var j = 0;

            for (var i = 0; i < numElts; i++) {
                var childObj = tbodyTags[i];

                if (childObj.id != null && childObj.id.length > len + 4 && groupName == (childObj.id.slice(4)).substr(0, len)) {
                    if (fOpen) {
                        index = childObj.id.indexOf("_", len + 4);
                        if (index != -1) {
                            index = childObj.id.indexOf("_", index + 1);
                            if (index != -1)
                                continue;
                        }
                    }
                    var previousDisplay = childObj.style.display;

                    childObj.style.display = displayStr;
                    var itemCount = 0;

                    if (Boolean(childObj.getAttribute("selectableRows"))) {
                        itemCount = Number(childObj.getAttribute("selectableRows"));
                        //custom logic
                       // currentGroupRows = childObj.rows;
                        //custom logic done
                    }
                    if (typeof FV4UI != "undefined" && FV4UI() && Boolean(itemCount)) {
                        if (!fOpen) {
                            if (previousDisplay != "none")
                                ctxCur.TotalListItems -= itemCount;
                            EnsureScriptParams("core.js", "DeselectCollapsedGroup", ctxCur, childObj);
                            EnsureScriptParams("core.js", "UpdateSelectAllCbx", ctxCur, true);
                        }
                        else {
                            ctxCur.TotalListItems += itemCount;
                            EnsureScriptParams("core.js", "UpdateSelectAllCbx", ctxCur, false);
                        }
                    }
                    if (fOpen && childObj.id.substr(0, 4) == "titl") {
                        var imgObj = document.getElementById("img_" + childObj.id.slice(4));
                        var imgObjSrc = imgObj.getAttribute('src');

                        if (imgObjSrc == commonClusterSrcPath) {
                            imgObj.className = expandImgClass;
                            imgObj.parentNode.className = expandSpanClass;
                        }
                        else {
                            imgObj.src = oldExpandSrcPath;
                        }
                    }
                    var groupID = "tbod" + groupName;
                    var child;

                    if (childObj.id.substr(0, groupID.length) == groupID) {
                        if (noAjax) {
                            for (j = 0; j < childObj.childNodes.length; j++) {
                                child = childObj.childNodes[j];
                                child.style.display = displayStr;
                            }
                        }
                        if (childObj.childNodes.length == 0) {
                            var nextTBody = childObj.nextSibling;

                            while (nextTBody != null) {
                                if (nextTBody.tagName == "TBODY" && nextTBody.id == "") {
                                    for (j = 0; j < nextTBody.childNodes.length; j++) {
                                        child = nextTBody.childNodes[j];
                                        child.style.display = displayStr;
                                        if (typeof FV4UI != "undefined" && FV4UI()) {
                                            HandleSingleGroupByRow(ctxCur, child, fOpen);
                                            UpdateSelectAllCbx(ctxCur, true);
                                        }
                                    }
                                    break;
                                }
                                else
                                    nextTBody = nextTBody.nextSibling;
                            }
                        }
                    }
                }
            }
            if (FV4UI())
                EnsureScriptParams("core.js", "UpdateCtxLastSelectableRow", ctxCur, viewTable);
            if (!noAjax && !g_ExpGroupParseStage) {
                if (g_ExpGroupNeedsState && ExpGroupFetchWebPartID(groupName) != null && !Boolean(ctxCur.noGroupCollapse)) {
                    if (fOpen) {
                        AddGroupToCookie(groupName);
                    }
                    else {
                        RemoveGroupFromCookie(groupName);
                    }
                }
                if (fOpen) {
                    var tbody = document.getElementById("tbod" + groupName + "_");

                    if (tbody != null) {
                        var isLoaded = tbody.getAttribute("isLoaded");

                        if (isLoaded == "false") {
                            ExpGroupFetchData(groupName, evt);
                        }
                    }
                }
            }
            //custom logic

            setTimeout(
                function () {
                    //alert('custom logic')
                    var statusColors = {
                        'My Stuff': 'DarkSeaGreen',
                        'Blank': '#FF7B61',
                        'Test': 'LightSteelBlue',
                        'Hello': 'Plum',
                        'Boat': 'YellowGreen',
                        'Mouse': 'DarkGray'
                    };

                    var rows = ctxCur.ListData.Row;
                    for (var i = 0; i < rows.length; i++) {
                        if (rows[i]["Color"] != undefined) {
                            var status = rows[i]["Color"];
                            var rowId = GenerateIIDForListItem(ctx, rows[i]);
                            var row = document.getElementById(rowId);
                            row.style.backgroundColor = statusColors[status];
                        }
                    }

                },
            500);

        }
    }
</script>

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.