Code:
<html>
<head>
<title>Test Grid</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css"/>
<!-- GC -->
<!-- LIBS -->
<script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
<!-- ENDLIBS -->
<script type="text/javascript" src="../../ext-all.js"></script>
<style type="text/css">
html, body {
font: normal 12px verdana;
margin: 0;
padding: 0;
border: 0 none;
}
</style>
<script type="text/javascript">
Ext.ns('Ext.grid.plugins');
Ext.grid.plugins.FixedRowNo = Ext.extend(Object, {
init: function(grid){
this.grid = grid;
grid.store.on('add', this.trimRow, this);
},
trimRow: function(store){
var showStore = function(s) {
for (var i = 0; i < s.getCount(); i++) {
var r = s.getAt(i);
console.log((i+1), r.get('company'));
};
}
showStore(store);
var max = this.grid.maxRow;//store.lastOptions.params.limit;
if (max) {
var count = store.getCount();
if (count > max) {
for (var i = count; i > max; i--) {
//store.removeAt(i-1);
var record = store.getAt(i-1);
store.remove(record);
console.log("removed=",record);
}
}
}
showStore(store);
}
});
Ext.preg('fixedrowno', Ext.grid.plugins.FixedRowNo);
Ext.onReady(function() {
var myData = [
['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],
['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],
];
/**
* Custom function used for column renderer
* @param {Object} val
*/
function change(val){
if(val > 0){
return '<span style="color:green;">' + val + '</span>';
}else if(val < 0){
return '<span style="color:red;">' + val + '</span>';
}
return val;
}
/**
* Custom function used for column renderer
* @param {Object} val
*/
function pctChange(val){
if(val > 0){
return '<span style="color:green;">' + val + '%</span>';
}else if(val < 0){
return '<span style="color:red;">' + val + '%</span>';
}
return val;
}
// create the data store
var store = new Ext.data.ArrayStore({
fields: [
{name: 'company'},
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
]
});
// manually load local data
store.loadData(myData);
// create the Grid
var grid = new Ext.grid.GridPanel({
store: store,
plugins: ['fixedrowno'],
tbar: [{
text: 'add',
handler: function() {
// access the Record constructor through the grid's store
var p = new store.recordType({company: 'Wal-Mart Stores, Inc.', price: 45.45, change: 0.73, pctChange: 1.63, lastChange: new Date()});
store.insert(0, p);
}
}, {
text: 'remove',
handler: function() {
store.removeAt(store.getCount()-1);
}
}],
columns: [
new Ext.grid.RowNumberer(),
{id:'company',header: 'Company', width: 160, sortable: true, dataIndex: 'company'},
{header: 'Price', width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
{header: 'Change', width: 75, sortable: true, renderer: change, dataIndex: 'change'},
{header: '% Change', width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
{header: 'Last Updated', width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
],
stripeRows: true,
autoExpandColumn: 'company',
height: 250,
width: 600,
title: 'Array Grid',
maxRow: myData.length,
// config options for stateful behavior
stateful: true,
stateId: 'grid'
});
// render the grid to the specified div in the page
grid.render('grid-example');
});
</script>
</head>
<body>
<div id="grid-example"></div>
</body>
</html>
From the above example, when you click 'add' button which insert a new record to the top of store and then the 'add' event fires, I found that the store really removed the last record, but the last record is still on the screen. is it a bug?