在增强网格中突出显示单元格的事件
Event to highlight a cell in enhanced grid
我正在使用 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
})
但如果我这样做,数据将不会显示。如果我在格式化程序值中用作字符串,我可以看到它处于警报状态,但如果使用函数,它根本不会出现。我不知道这里有什么问题。
在这里您可以看到 excel sheet 行被突出显示,但不是该行的第一个单元格。同样,我希望将某些样式添加到特定单元格。不是整行或整列
这是一个适合你的 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";
}
});
});
新答案。如果需要,您可以在单击列时删除样式。此示例显示如何格式化添加 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;
}
我们可以在这里使用 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";
}
}
});
我正在使用 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
})
但如果我这样做,数据将不会显示。如果我在格式化程序值中用作字符串,我可以看到它处于警报状态,但如果使用函数,它根本不会出现。我不知道这里有什么问题。
在这里您可以看到 excel sheet 行被突出显示,但不是该行的第一个单元格。同样,我希望将某些样式添加到特定单元格。不是整行或整列
这是一个适合你的 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";
}
});
});
新答案。如果需要,您可以在单击列时删除样式。此示例显示如何格式化添加 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;
}
我们可以在这里使用 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";
}
}
});