javascriptjquerykendo-uiclient-side-templatingknockout-templating

Kendo grid change style cell data


I have a very basic kendo grid. I am using template feature to style cell data. What I want to do is style "Edit" in red and "Delete" in green color.

GRID CODE

grid = $("#grid").kendoGrid({
        dataSource: {
            data: createRandomUserData(),
            schema: {
                model: {
                    id: 'Id',
                    fields: {
                        FirstName: {
                            type: "string"
                        },
                        Action: {
                            type: "string"
                        }
                    }
                }
            }
        },
        columns: [
            {
                field: "FirstName",
                title: "First Name"
            },
            {
                field: "Action",
                title: "Action",
                template: "<span style='color:red'>#: Action #</span>"
            }
        ]
    }).data("kendoGrid");

How can I do it. I am unable to separate cell data.

JSFiddle - http://jsfiddle.net/Sbb5Z/1338/


Solution

  • Instead of applying directly the color what I suggest you to do is define several CSS classes that do the styling.

    Example:

    .Edit {
        color: red;
    }
    
    .Delete {
        color: green;
    }
    
    .Edit.Delete {
        color: blue;
    }
    

    And specify in the template which class to use.

    template: "<span class='#: Action #'>#: Action #</span>"
    

    This uses red when they are Edit, green if Delete and blue if both.

    And you JSFiddle modified here : http://jsfiddle.net/OnaBai/298nZ/

    EDIT: If you want to split/format per word, then you need a little programming. Basically you might do it as follow.

    // Convert words separated by spaces into an array
    var words = data.Action.split(" ");
    // Iterate on array elements for emitting the HTML
    $.each(words, function(idx, word) {
        // emit HTML using template syntax
        <span class="#: word #">#: word #</span>
    });
    

    All this needs to be wrapped in a template and you get:

    <script type="text/kendo-script" id="template">
        # console.log("data", data, data.Action); #
        # var words = data.Action.split(" "); #
        # $.each(words, function(idx, word) { #
            <span class='#= word #'>#= word #</span>&nbsp;
        # }); #
    </script>
    

    And your grid definition:

    grid = $("#grid").kendoGrid({
        dataSource: {
            data: createRandomUserData(),
            schema: {
                model: {
                    id: 'Id',
                    fields: {
                        FirstName: {
                            type: "string"
                        },
                        Action: {
                            type: "string"
                        }
                    }
                }
            }
        },
        columns: [
            {
                field: "FirstName",
                title: "First Name"
            },
            {
                field: "Action",
                title: "Action",
                template: $("#template").html()
            }
        ]
    }).data("kendoGrid");
    

    The JSFiddle modified here : http://jsfiddle.net/298nZ/1/