We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to color a cell using Template for a lookup column?

I've asked a similar question before, but what I learned there doesn't seem to apply to this question:

What I'm trying to achieve is something similar to how "Status" displays in this demo of yours:


I want to color the text for a cell, based off a value that is in the ViewBag datasource for a cell.  In the datasource, along with "value" and "code" is an end user customizable "DisplayColor" field.  I want to use that value to color a span with a rounded border (similar to a chip) that contains the foreignKeyValue.  But during my initial work, I can't even get the foreignKeyValue to show in the span.

My code is similar to the following. Coloring the cell works, sort of. I need to fix the style for the span not the cell, and do the rounded border.  But I can't figure out how to display the foreignKeyValue.

    @(Html.EJS().Grid<CaseGridVm>("CaseListGrid")
                .DataSource(ds => { ds.Url(Url.Action("LoadRecords")).Adaptor("UrlAdaptor"); })
                .Columns(col => {
                    col.Field(p => p.Id).IsPrimaryKey(true).IsIdentity(true).Visible(false).Add();
                    col.Field("CaseNumber").HeaderText("Case ID").Width(130).Add();
                    col.Field("ServiceId").HeaderText("Service").Width(100).Type("number").Template("#serviceTemplate")
                        .Filter(new { type = "CheckBox" }) // for FilterType.Menu
                        .ForeignKeyField("value").ForeignKeyValue("code").DataSource((IEnumerable<object>)ViewData["ServiceId"]).Add();
                })
                .QueryCellInfo("queryCellInfoCaseListGrid")
                .Render()
    )

<script type="text/x-template" id="serviceTemplate">
    <div id="status" class="servicetemp">
        <span class="servicetext">${foreignKeyValue}</span>
    </div>
</script>

<script type="text/javascript">
    var serviceList;
    
    $(document).ready(function () {
        serviceList = @Html.Raw(Json.Encode(ViewData["ServiceId"]));
    });

    function queryCellInfoCaseListGrid(args) {
        if (args.column.field === 'ServiceId') {
            var service = serviceList.find(x => x.value === args.data.ServiceId);
            if (service != null && service.color != "") {
                console.log(args.cell);
                args.cell.style.backgroundColor = service.DisplayColor;
                args.cell.style.color = "#FFFFFF";
            }
        }
    }
</script>

Thank you,
Rich W

1 Reply

TS Thavasianand Sankaranarayanan Syncfusion Team May 2, 2019 02:00 PM UTC

Hi Richard, 

Thanks for contacting Syncfusion Support. 

We have validated your requirement and you can achieve your requirement by selecting only the template content and applying the styles. Please refer the  below code snippet for your reference. 

<script type="text/x-template" id="serviceTemplate"> 
    <div id="status" class="servicetemp"> 
        <span class="servicetext">${foreignKeyValue}</span> 
    </div> 
</script>     

function queryCellInfoCaseListGrid(args) { 
        if (args.column.field === 'ServiceId') { 
            var service = serviceList.find(x => x.value === args.data.ServiceId); 
            if (service != null && service.color != "") { 
                console.log(args.cell); 

                var spanContent = args.cell.querySelector(‘.servicetext’);  //Selecting the span content only  
                spanContent.backgroundColor = service.DisplayColor;     //Apply the styles to spanContent 
                spanContent.style.color = "#FFFFFF"; 
            } 
        } 
    } 

Please get back to us if you need further assistance. 

Regards, 
Thavasianand S. 


Loader.
Live Chat Icon For mobile
Up arrow icon