用于突出显示增强网格中的单元格的事件

时间:2017-07-26 10:09:24

标签: javascript dojo dojox.grid.datagrid

我正在使用dojo工具包版本1.10。我们有一个问题需要突出显示特定的单元格。我们可以在 onStyleRow 上突出显示一行。但有没有办法突出增强网格中的特定单元格?

已编辑:

对于格式化程序我已经做到了。这是我的格式化程序 -

var cellformatter = function(value){

     color = "green";

     return "<span style=color:green>" + value +"</span>;"
}

我将此绑定到我拥有的可用网格结构。

for (var i=0;i<gridStructure.length;i++)
{
    var gridData = gridStructure[i];

    gridData.formatter = cellformatter ;

}

在网格定义中我将它添加到结构中。 -

var mygrid = new EnhancedGrid({
    id: 'grid',
    store: gridStore, //Data store passed as input
    structure: gridData, //Column structure passed as input
    autoHeight: true,
    autoWidth: true
})

但如果我这样做,数据将无法显示。如果我在格式化程序值中使用作为字符串,我可以看到它处于警报状态,但如果使用某个函数,它根本就不会出现。我不知道这里有什么问题。

Adding an image

在这里,您可以看到excel工作表行突出显示,但不是该行的第一个单元格。同样,我希望将某种样式添加到特定单元格中。不是整行或列

3 个答案:

答案 0 :(得分:1)

这是一个适合你的jsfiddle

http://jsfiddle.net/bnqkodup/347/

代码

HTML

<div id="container" class="claro">
    <div id="gridDiv"></div>
</div>

CSS

@import"../lib/dojo/resources/dojo.css";
 @import"../lib/dijit/themes/claro/claro.css";
 @import"../lib/dojox/grid/enhanced/resources/claro/EnhancedGrid.css";
 @import"../lib/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css";

/*Grid need a explicit width/height by default*/
 #grid {
    width: 1110px;
    height: 494px;
    color: #000000;
}

JS

dojo.require("dojox.grid.EnhancedGrid");
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojo.on");

dojo.ready(function (on) {
    /*set up data store*/
    var data = {
        identifier: 'id',
        items: []
    };
    var data_list = [{
        col1: "normal",
        col2: false,
        col3: 'But are not followed by two hexadecimal',
        col4: 29.91
    }, {
        col1: "important",
        col2: false,
        col3: 'Because a % sign always indicates',
        col4: 9.33
    }, {
        col1: "important",
        col2: false,
        col3: 'Signs can be selectively',
        col4: 19.34
    }];
    var rows = 60;
    for (var i = 0, l = data_list.length; i < rows; i++) {
        data.items.push(dojo.mixin({
            id: i + 1
        }, data_list[i % l]));
    }
    var store = new dojo.data.ItemFileWriteStore({
        data: data
    });

    /*set up layout*/
    var layout = [
        [{
            'name': 'Column 1',
            'field': 'id'

        }, {
            'name': 'Column 2',
            'field': 'col2'
        }, {
            'name': 'Column 3',
            'field': 'col3',
            'width': '230px'
        }, {
            'name': 'Column 4',
            'field': 'col4',
            'width': '230px'
        }]
    ];

    /*create a new grid:*/
    var grid = new dojox.grid.EnhancedGrid({
        id: 'grid',
        store: store,
        structure: layout,
        rowSelector: '20px'
    },
    document.createElement('div'));

    /*append the new grid to the div*/
    dojo.byId("gridDiv").appendChild(grid.domNode);

    /*Call startup() to render the grid*/
    grid.startup();

    dojo.on(grid,"CellClick",function(evt){

    /* <=search for the column here */
   var idx = evt.cellIndex;
   var cellNode = evt.cellNode;
   if(cellNode){
      cellNode.style.backgroundColor = "green";
    }

    });
});

答案 1 :(得分:1)

新答案。如果需要,可以在单击列时删除样式。此示例显示如何格式化添加href的列。

http://jsfiddle.net/bnqkodup/367/

<强> HTML

<div id="container" class="claro">
    <div id="gridDiv"></div>
</div>

<强> JS

dojo.require("dojox.grid.EnhancedGrid");
dojo.require("dojo.data.ItemFileWriteStore");
dojo.require("dojo.on");

dojo.ready(function (on) {
    /*set up data store*/
    var data = {
        identifier: 'id',
        items: []
    };
    var data_list = [{
        col1: "normal",
        col2: false,
        col3: 'But are not followed by two hexadecimal',
        col4: 29.91
    }, {
        col1: "important",
        col2: false,
        col3: 'Because a % sign always indicates',
        col4: 9.33
    }, {
        col1: "important",
        col2: false,
        col3: 'Signs can be selectively',
        col4: 19.34
    }];
    var rows = 60;
    for (var i = 0, l = data_list.length; i < rows; i++) {
        data.items.push(dojo.mixin({
            id: i + 1
        }, data_list[i % l]));
    }
    var store = new dojo.data.ItemFileWriteStore({
        data: data
    });
function formatLink(value){
        return '<a href="#">'+value+'</a>';
    }
    /*set up layout*/
    var layout = [
        [{
            'name': 'Column 1',
            'field': 'id',
            formatter: formatLink

        }, {
            'name': 'Column 2',
            'field': 'col2'
        }, {
            'name': 'Column 3',
            'field': 'col3',
            'width': '230px'
        }, {
            'name': 'Column 4',
            'field': 'col4',
            'width': '230px'
        }]
    ];

    /*create a new grid:*/
    var grid = new dojox.grid.EnhancedGrid({
        id: 'grid',
        store: store,
        structure: layout,
        rowSelector: '20px'
    },
    document.createElement('div'));

    /*append the new grid to the div*/
    dojo.byId("gridDiv").appendChild(grid.domNode);

    /*Call startup() to render the grid*/
    grid.startup();

    dojo.on(grid,"CellClick",function(evt){

    /* <=search for the column here */
   var idx = evt.cellIndex;
   var cellNode = evt.cellNode;
   if(cellNode){
      cellNode.style.backgroundColor = "green";
    }
    if(evt.cellIndex==1){
    //this.set('style','background-color:red;');
    }
    });
});

<强> CSS

@import"../lib/dojo/resources/dojo.css";
 @import"../lib/dijit/themes/claro/claro.css";
 @import"../lib/dojox/grid/enhanced/resources/claro/EnhancedGrid.css";
 @import"../lib/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css";

/*Grid need a explicit width/height by default*/
 #grid {
    width: 1110px;
    height: 494px;
    color: #000000;
}

答案 2 :(得分:0)

我们可以在这里使用onStyleRow事件。我们只需要知道列号和行号。

这是代码

dojo.connect(grid, "onStyleRow", function(row){
     var nd = dojo.query('td[idx="1"]'  /* <= put column index here */, row.node)[0];
     if(nd){
            if(nd.innerHTML == 'FALSE' /* <= put cell content here */){
                nd.style.backgroundColor = "red";
              }else if(nd.innerHTML == "TRUE"){
                 nd.style.backgroundColor = "green";
                 }
            }
        });