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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

144319 Apr 30,2019 02:56 PM UTC May 2,2019 02:00 PM UTC ASP.NET MVC - EJ 2 1
loading
Tags: Grid
Richard Werning
Asked On April 30, 2019 02:56 PM UTC

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

Thavasianand Sankaranarayanan [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon