2

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

shv22
  • 680
  • 5
  • 28

3 Answers3

1

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

    });
});
bajji
  • 1,271
  • 3
  • 15
  • 37
  • Thanks @Manjunatha but I think I was not clear. I want to have cell different styles on load itself like onRowStyle. Can we do that??? – shv22 Jul 28 '17 at 01:54
  • then you have to use formatter function to style the cell. or if you have declared your grid column through html, you can also style your cell using styles – bajji Jul 28 '17 at 14:45
  • I have tried using formatter function and adding it to layout but it is not working. Can you please provide an example – shv22 Jul 28 '17 at 15:29
  • Vote up for the answer it will surely help someone. – shv22 Jul 28 '17 at 21:47
1

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;
}
bajji
  • 1,271
  • 3
  • 15
  • 37
  • thanks for the effort. Here it is not highlighting a particular cell, it is highlighting a column. – shv22 Jul 28 '17 at 19:04
  • I'm unable to understand what you are looking for. You have to be more clear. Can you post an image which shows what you are looking for? – bajji Jul 28 '17 at 19:43
  • Sure I will do that. Just for explanation what onStyleRow does is on load it will add custom style to the whole row. Now, I want the same to apply custom style on load but it should be to only a particular cell. Like it is a combination of onCellClick and onStyleRow where onCellClick highlight the cell and onStyleRow highlight row on load. So, I want only one cell highlighted on load itself not a click event – shv22 Jul 28 '17 at 20:43
  • I have added the picture where a particular style is applied to cell on load irrespective it row has some other style. – shv22 Jul 28 '17 at 21:24
  • see if this works http://jsfiddle.net/bnqkodup/390/ always column 2 is colored. If so let me know so that I can post it as an answer. – bajji Jul 28 '17 at 21:53
  • Here column 2 is colored and it only show when we click on that. Otherwisewe can't know. Thats my problem I can also style by clicking on it but thats of no use. Requirement is color the individual cell on load. Lets say color only 2nd cell of 2nd column and 3rd row and not on click. – shv22 Jul 29 '17 at 04:21
  • I finally got an answer for that. – shv22 Jul 29 '17 at 10:43
  • if you find an answer, please post it so that others can be benefited. – bajji Jul 31 '17 at 21:31
0

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";
                 }
            }
        });
shv22
  • 680
  • 5
  • 28