【问题标题】:Kendo grid change style cell data剑道网格更改样式单元格数据
【发布时间】:2014-04-07 13:05:35
【问题描述】:

我有一个非常基本的剑道网格。我正在使用模板功能来设置单元格数据的样式。我想要做的是红色的“编辑”样式和绿色的“删除”样式。

网格代码

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");

我该怎么做。我无法分离单元格数据。

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

【问题讨论】:

  • 关于您的 JSFiddle... 包含 Edit 和 Delete 的单元格呢?

标签: javascript jquery kendo-ui client-side-templating knockout-templating


【解决方案1】:

我建议您不要直接应用颜色,而是定义几个进行样式设置的 CSS 类。

例子:

.Edit {
    color: red;
}

.Delete {
    color: green;
}

.Edit.Delete {
    color: blue;
}

并在模板中指定使用哪个class

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

当它们是Edit 时使用red,如果Deleteblue 如果两者都使用green

你的 JSFiddle 在这里修改:http://jsfiddle.net/OnaBai/298nZ/

编辑:如果你想按单词分割/格式化,那么你需要一点编程。基本上你可以这样做。

// 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>
});

所有这些都需要包装在一个模板中,然后你会得到:

<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>

还有你的网格定义:

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");

这里修改的 JSFiddle:http://jsfiddle.net/298nZ/1/

【讨论】:

  • 太棒了!!但我不想要第三种颜色。当 Edit 和 Delete 都存在时,我希望 Edit 为红色,Delete 为绿色。
  • 非常棒!!谢谢你这么聪明的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-20
  • 2014-10-21
  • 1970-01-01
相关资源
最近更新 更多