shv22
shv22

Reputation: 690

Event to highlight a cell in enhanced grid

I am using dojo toolkit version 1.10. We have a problem where we need to highlight a particular cell. We can able to highlight a row on onStyleRow. But is there any way to highlight a particular cell in enhanced grid?

EDITED:

For formatter I have made this. This is my formatter -

var cellformatter = function(value){

     color = "green";

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

And I am binding this to available grid structure I have.

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

    gridData.formatter = cellformatter ;

}

And in grid definition I am adding it to structure. -

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

But If I do it data won't show. If I use as a string in formatter value I can see it is coming in alert but if a function is used it is not at all coming. I dont know what is the problem here.

Adding an image

Here you can see an excel sheet row is highlighted but not the first cell of the row. Likewise I want some style to be added to a particular cell. Not to whole row or a column

Upvotes: 2

Views: 1028

Answers (3)

shv22
shv22

Reputation: 690

We can use the onStyleRow event itself here. We just need to know the column number and the row number.

Here is the code

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

Upvotes: 0

bajji
bajji

Reputation: 1291

New answer. you can remove styling on click of column if you want. this example shows how to format the column adding 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;
}

Upvotes: 1

bajji
bajji

Reputation: 1291

Here is a working jsfiddle for you

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

Code

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

    });
});

Upvotes: 1

Related Questions