ExtJS Grid renderer usage
Excerpt from: http://www.cnblogs.com/ljian/archive/2011/10/27/2226959.html
var cm = new Ext.grid.ColumnModel (
[
New Ext.grid.RowNumberer ({header: "", width:20, align: ' center '}),
{header: ', align: ' center ', dataindex: ' Accountandroseid ', width:50, Sortable:true, hidden:true},
{header: ', align: ' center ', dataindex: ' Useraccountid ', width:50, Sortable:true, hidden:true},
{header: ' account ', align: ' center ', dataindex: ' UserAccountName ', width:200, sortable:true},
{header: ' role name ', align: ' center ', dataindex: ' Userrolename ', width:200, sortable:true},
{header: ' status ', align: ' center ', dataindex: ' Useraccountstateid ', width:200, Sortable:true, Hidden:true, Renderer:func tion ()}
]
Renderer can format the data that the column displays or display the final data as you customize it (as I understand it now)
First look at the parameters in the Renderer:function ()
Renderer:function (value, Cellmeta, record, RowIndex, ColumnIndex, store) {
}
1.value is the value of the current cell
2.cellmeta saved is Cellid cell id,id do not know what to do, it seems to be a column number, CSS is the CSS style of this cell.
3.record is all the data in this line, what you want, record.data["id") so you get it.
4.rowIndex is the line number, not the meaning of the number from the beginning, but the calculation of the results after paging.
The 5.columnIndex column number is too simple.
6.store, this is very powerful, actually this is the time you construct the form to pass the DS, that is, all the data in the table, you can call, alas, too powerful.
Find a blog post online, very good, intuitive and clear:
<meta http-equiv= "Content-type" c>
<title>03.grid</title>
<link rel= "stylesheet" type= "Text/css" href= "Http://www.cnblogs.com/../resources/css/ext-all.css"/>
<script type= "Text/javascript" src= "Http://www.cnblogs.com/adapter/ext/ext-base.js" ></script>
<script type= "Text/javascript" src= "Http://www.cnblogs.com/ext-all.js" ></script>
<script type= "Text/javascript" >
Ext.onready (function () {
/*
var cm = new Ext.grid.ColumnModel ([
{header: ' number ', Dataindex: ' ID '},
{header: ' Gender ', dataindex: ' Sex ', renderer:function (value) {
if (value = = ' Male ') {
Return "<span style= ' color:red;font-weight:bold; ' > Red Man </span> ";
} else {
Return "<span style= ' color:green;font-weight:bold; ' > Green Women </span> ";
}
}},
{header: ' name ', Dataindex: ' Name '},
{header: ' description ', dataindex: ' Descn '}
]);
*/
function Rendersex (value) {
if (value = = ' Male ') {
Return "<span style= ' color:red;font-weight:bold; ' > Red male </span> ";
} else {
Return "<span style= ' color:green;font-weight:bold; ' > Green female </span> ";
}
}
function Renderdescn (value, Cellmeta, record, RowIndex, ColumnIndex, store) {
var str = "<input type= ' button ' value= ' View Details ' >";
return str;
}
var cm = new Ext.grid.ColumnModel ([
{header: ' number ', Dataindex: ' ID '},
{header: ' Sex ', dataindex: ' Sex ', renderer:rendersex},
{header: ' name ', Dataindex: ' Name '},
{header: ' description ', dataindex: ' Descn ', RENDERER:RENDERDESCN}
]);
var data = [
[' 1 ', ' Male ', ' name1 ', ' descn1 '],
[' 2 ', ' female ', ' name2 ', ' descn2 '],
[' 3 ', ' Male ', ' name3 ', ' descn3 '],
[' 4 ', ' Female ', ' name4 ', ' descn4 '],
[' 5 ', ' Male ', ' name5 ', ' descn5 ']
];
var store = new Ext.data.Store ({
Proxy:new Ext.data.MemoryProxy (data),
Reader:new Ext.data.ArrayReader ({}, [
{name: ' ID '},
{name: ' Sex '},
{name: ' Name '},
{name: ' DESCN '}
])
});
Store.load ();
var Grid = new Ext.grid.GridPanel ({
Autoheight:true,
Renderto: ' Grid ',
Store:store,
cm:cm
});
});
</script>
<body>
<script type= "Text/javascript" src= ". /examples.js "></script>
<div id= "Grid" ></div>
</body>
ExtJS Grid renderer Usage