ExtJs Simple Grid
Simple Grid Example
by LeeMellinger
HTML
<link rel="stylesheet" href="http://cdn.sencha.com/ext/gpl/4.2.0/resources/css/ext-all.css">
<!-- corresponding blog at http://blog.jardalu.com/2013/5/24/simple-grid-sample-extjs -->
<title>Simple Grid Sample for ExtJs</title>
<div id="example-grid"></div>
CSS
body {
padding: 50px 20px 0 20px;
}
JavaScript
Ext.require([
'Ext.data.*',
'Ext.grid.*'
]);
function getRandomDate() {
var from = new Date(1900, 0, 1).getTime();
var to = new Date().getTime();
return new Date(from + Math.random() * (to - from));
}
function createFakeData(count) {
var firstNames = ['Ed', 'Tommy', 'Aaron', 'Abe'];
var lastNames = ['Spencer', 'Maintz', 'Conran', 'Elias'];
var data = [];
for (var i = 0; i < count ; i++) {
var dob = getRandomDate();
var firstNameId = Math.floor(Math.random() * firstNames.length);
var lastNameId = Math.floor(Math.random() * lastNames.length);
var name = Ext.String.format("{0} {1}", firstNames[firstNameId], lastNames[lastNameId]);
data.push([name, dob]);
}
return data;
}
Ext.onReady(function(){
// Create a new model: Person
Ext.define('Person',{
extend: 'Ext.data.Model',
fields: [
'Name', 'dob'
]
});
// create the Data Store
var store = Ext.create('Ext.data.Store', {
model: 'Person',
autoLoad: true,
proxy: {
type: 'memory',
data: createFakeData(20),
reader: {
type: 'array'
}
}
});
// create the grid
Ext.create('Ext.grid.Panel', {
store: store,
columns: [
{text: "Name", width:120, dataIndex: 'Name'},
{text: "dob", width: 380, dataIndex: 'dob'}
],
renderTo:'example-grid',
width: 500,
height: 500
});
});