简体   繁体   English

Kendo树形列表-尝试设置列模板

[英]Kendo treelist - trying to set a column template

I'm working with a Kendo treelist widget, and disappointed to see there's no rowTemplate option as there is on the Kendo grid. 我正在使用剑道树形列表小部件,但对于剑道网格上没有rowTemplate选项感到失望。

I see a columnTemplate option (ie http://docs.telerik.com/kendo-ui/api/javascript/ui/treelist#configuration-columns.template ), but this will affect the entire column. 我看到了columnTemplate选项(即http://docs.telerik.com/kendo-ui/api/javascript/ui/treelist#configuration-columns.template ),但这会影响整个专栏。

However, I need to drill into each cell value and set a css color property based on a ratio ( ie If value/benchmark < .2 , assign <span style='color:red;'> , but my color value is dynamic. 但是,我需要钻取每个单元格值并根据比率设置css color属性(即,如果value/benchmark < .2 ,则分配<span style='color:red;'> ,但是我的颜色值是动态的。

There's a dataBound: and dataBinding: event on the treelist , but I'm still trying to figure out how to intercept each cell value and set the color once I've done my calculation. 有一个dataBound:dataBinding:在事件treelist ,但我仍然试图找出如何拦截每个单元格值并设置颜色,一旦我做我的计算。

 var treeOptions = { dataSource: ds, columns: colDefs, selectable: true, scrollable: true, resizable: true, reorderable: true, height: 320, change: function (e) { // push selected dataItem var selectedRow = this.select(); var row = this.dataItem(selectedRow); }, dataBound: function (e) { console.log("dataBinding"); var ds = e.sender.dataSource.data(); var rows = e.sender.table.find("tr"); } }; 

and this is where I'm building out the `colDefs' object (column definitions): 这就是我要构建`colDefs'对象(列定义)的地方:

 function parseHeatMapColumns(data, dimId) { // Creates the Column Headers of the heatmap treelist. // typeId=0 is 1st Dimension; typeId=1 is 2nd Dimension var column = []; column.push({ "field": "field0", "title": "Dimension", headerAttributes: { style: "font-weight:" + 'bold' + ";" }, attributes : { style: "font-weight: bold;" } }); var colIdx = 1; // start at column 1 to build col headers for the 2nd dimension grouping _.each(data, function (item) { if (item.typeId == dimId) { // Dimension values are duplicated, so push unique values (ie trade types may have dupes, whereas a BkgLocation may not). var found = _.find(column, { field0: item.field0 }); if (found == undefined) { column.push({ field: "field2", title: item.field0, headerAttributes: { style: "font-weight:" + 'bold' } ,template: "<span style='color:red;'>#: field2 #</span>" }); colIdx++; } } }); return column; } 

**** UPDATE **** In order to embed some logic within the template : **** UPDATE ****为了在template嵌入一​​些逻辑:

 function configureHeatMapColumnDefs(jsonData, cols, model) { var colDef = ''; var dimId = 0; var colorProp; var columns = kendoGridService.parseHeatMapColumns(jsonData, dimId); // iterate columns and set color property; NB: columns[0] is the left-most "Dimension" column, so we start from i=1. for (var i = 1; i <= columns.length-1; i++) { columns[i]['template'] = function (data) { var color = 'black'; if (data.field2 < 1000) { color = 'red'; } else if (data.field2 < 5000) { color = 'green'; } return "<span style='color:" + color + ";'>" + data.field2 + "</span>"; }; } return columns; } 

Advice is appreciated. 意见表示赞赏。 Thanks, Bob 谢谢,鲍勃

In the databound event you can iterate through the rows. 在数据绑定事件中,您可以遍历各行。 For each row you can get the dataItem associated with it using the dataitem() method ( http://docs.telerik.com/kendo-ui/api/javascript/ui/treelist#methods-dataItem ) 对于每一行,您都可以使用dataitem()方法获得与其关联的dataItem( http://docs.telerik.com/kendo-ui/api/javascript/ui/treelist#methods-dataItem

Once you have the dataitem, calculate your ration and if the row meets the criteria for color, change the cell DOM element: 获得数据项后,计算您的定量,如果行符合颜色标准,则更改单元格DOM元素:

dataBound: function (e) {
    var that = e.sender;
    var rows = e.sender.table.find("tr");
    rows.each(function(idx, row){
    var dataItem = that.dataItem(row);
    var ageCell = $(row).find("td").eq(2);
    if (dataItem.Age > 30) {
        //mark in red 
         var ageText = ageCell.text();
         ageCell.html('<span style="color:red;">' + ageText + '</span>');                            
    }
}

DEMO 演示

UPDATE: you can also do this with a template: 更新:您也可以使用模板执行此操作:

                $("#treelist").kendoTreeList({
                    dataSource: dataSource,
                    height: 540,
                    selectable: true,
                    columns: [
                        { field: "Position"},
                        { field: "Name" },
                        { field: "Age",
                         template: "# if ( data.Age > 30  ) { #<span style='color:red;'> #= data.Age # </span>  #}else{# #= data.Age # #}#"
                        }
                    ],

                });

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM