-->

Telerik Grid Row Custom Formatting on either bit/i

2019-09-16 17:17发布

问题:

I'd prefer for either string or int but would settle for it based on bit.

The Goal is if the value of a String field == 'blah blah blah' that it will turn that whole row grey(blue table with red and green already used on it so I'm open to any other colour suggestion)

I've tried

.RowAction(row =>
    {
        if (row.DataItem.[Bound Data col Name] == "[String value]")
        {
            row.HtmlAttributes["style"] = "background:grey;";
        }
    }) 

and

.RowAction(row =>
    {
        if (row.DataItem.[Bound Data col Name] == "[String value]")
        {
            row.Grid.HtmlAttributes["style"] = "background:grey;";
        }
    }) 

and

.ClientEvents(events => events.OnRowDataBound("onRowDataBound"))
function onRowDataBound(e) {
    if (e.dataItem.[Bound Data col Name] == "[String value]") {
        e.row.style.backgroundColor = "grey";
    }
}

all of these didn't work in the slightest and these are the only clear examples for mvc on how to do this so anybody got any ideas?

回答1:

Well took forever but telerik finally admitted that there was a bug with Cellaction on the open source version which they had address on the commercial version (http://www.telerik.com/community/forums/aspnet-mvc/grid/cell-action-issue-evidence-provided.aspx), it only took 2 locked topics until they finally accepted what I'd been telling them the whole time.

Long story short if you want to do something like this an encounter issues with Cellaction then either buy the commercial version or follow the steps below.

Simple add an extra field into your DTO for each cell you want to change the colour of, in my case I had 6 dates which depending on how close to present date they got would vary their colour.

You would then in the controller do all the date calculations and depending on how close it got would record the colour to it's corresponding DTO item.

for example

public String DateDisplay1
public String DateDisplay1Colour

then you would hide the colour item on the grid and use it's value to change the colour of the row which DateDisplay1 is on.

so if DateDisplay1 is on row1 then in the onRowDataBound(e) function I would have something like

if (e.dataItem.DateDiplay1Colour == "Green") {

        e.row.cells[1].style.backgroundColor = "#7EDF7D";
    }

this way you can still display the date in it's cell and then display the colour based off it's corresponding hidden value.

then same can be done with admin powers so if a user isn't an admin then

row.cells[6].innerHTML = '<readonly>' + "Edit" + '</readonly>';

thus getting rid of the link allowing a user to edit that record.

Hope this helps some people and I hope telerik gets round to unlocking my topics so I can add on the solution I found.