Replacing text with icons

Replacing text with icons

TamrasTamras Posts: 12Questions: 5Answers: 0

Using datatable on SharePoint Online, is there a way to replace the text with icons based on values?
Example: Value = In, display <img src='/_layouts/images/KPIDefault-2.GIF'/>

I was using client side rendering but can't figure out how to do this in datatable.

(function() {
    var oFldCtx = {};
    oFldCtx.Templates = {};
    oFldCtx.Templates.Fields = {"InOut": {"View": overrideStatus} };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(oFldCtx);
})();

function overrideStatus(ctx) {
    var oStatus = ctx.CurrentItem.InOut;
    
    if (oStatus == 'In')    {
        return "<img src='/_layouts/images/KPIDefault-0.GIF'/>";
    }
    if (oStatus == 'Out-A')     {
        return "<img src='/_layouts/images/KPIDefault-2.GIF'/>";
    }
    if (oStatus == 'Out-P')     {
        return "<img src='/_layouts/images/KPIDefault-1.GIF'/>";
    }

}

This question has accepted answers - jump to:

Answers

  • TamrasTamras Posts: 12Questions: 5Answers: 0

    I already referred to this page but still unclear.

  • TamrasTamras Posts: 12Questions: 5Answers: 0

    Figured this out by adding:

                "columnDefs": [ {
                    "targets": 1,
                    "className": "IOBoard",
                    "createdCell": function (td, cellData, rowData, row, col) {
                        if(cellData == 'In') {
                            $(td).html("<img src='/_layouts/images/KPIDefault-0.GIF'/>");
                        }
                        if(cellData == 'Out-A') {
                            $(td).html("<img src='/_layouts/images/KPIDefault-2.GIF'/>");
                        }
                        if(cellData == 'Out-P') {
                            $(td).html("<img src='/_layouts/images/KPIDefault-2.GIF'/>");
                        }
    
                    }
                }],
    
    
  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    you probably should be using the render function in columnDefs or columns options

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Agreed - using a rendering function is how I would recommend you do that.

    Allan

  • TamrasTamras Posts: 12Questions: 5Answers: 0

    Thanks for the suggestion. I used render as follows:

    "aoColumns": [
                    { "mData": "Title" },
                    { "mData": "InOut", 
                        "render": function(data, type, row) {
                            if(data == 'In') {
                                return '<img src='+ "'/_layouts/images/KPIDefault-0.GIF'" + '/>';
                            }
                            if(data == 'Out-A') {
                                return '<img src='+ "'/_layouts/images/KPIDefault-2.GIF'" + '/>';
                            }
                            if(data == 'Out-P') {
                                return '<img src='+ "'/_layouts/images/KPIDefault-2.GIF'" + '/>';
                            }
                        }
                    },
    
This discussion has been closed.