Nov 09, 2010, 10:55 AM
[CLOSED] Create GridPanel dinamically with javascript
Hi,
I'm having some problems when creating a GridPanel dinamically using javascript. Let me explain the situation.
1) The GridPanel I need to load must be created dinamically 'cause the datasource will be different.
2) To load the data I use a static method (DirectMethod) returning an object containg the information to create the Store and the ColumnModel (fields, width, header, dataindex, format) and the records to show to the user.
Basically I return an object like
THE PROBLEM:
1) When I serialize the result in my static method using JSON.Serialize the values in DATE column are returned as a String.
> Tested with Firefox, Chrome and Internet Explorer. The problem occurs only in Internet Explorer.
2) The values in VALUE_NULL column are returned as null, but the GridPanel displays a zero value. I need to display empty.
> Tested with Firefox, Chrome and Internet Explorer. The problem occurs in all browsers.
Follows the sample code:
I'm having some problems when creating a GridPanel dinamically using javascript. Let me explain the situation.
1) The GridPanel I need to load must be created dinamically 'cause the datasource will be different.
2) To load the data I use a static method (DirectMethod) returning an object containg the information to create the Store and the ColumnModel (fields, width, header, dataindex, format) and the records to show to the user.
Basically I return an object like
{
fields: {[
{header: 'Date', width: 200, dataindex: 'DATE', format: ''},
{header: 'Value', width: 120, dataindex: 'VALUE', format: ''},
{header: 'Null Value', width: 120, dataindex: 'VALUE_NULL', format: ''}
]},
records: {[...]}
}
3) I'm doing this way 'couse I don't want to execute a POST. Need my process to be as fast as it cans. So I can not go to the server and create the store and column model, it must to be done on client side.THE PROBLEM:
1) When I serialize the result in my static method using JSON.Serialize the values in DATE column are returned as a String.
> Tested with Firefox, Chrome and Internet Explorer. The problem occurs only in Internet Explorer.
2) The values in VALUE_NULL column are returned as null, but the GridPanel displays a zero value. I need to display empty.
> Tested with Firefox, Chrome and Internet Explorer. The problem occurs in all browsers.
Follows the sample code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
[DirectMethod]
public static object LoadData()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("DATE", typeof(DateTime));
table.Columns.Add("VALUE", typeof(decimal));
table.Columns.Add("VALUE_NULL", typeof(decimal));
table.Rows.Add(new DateTime(2010, 9, 25), 123.232, DBNull.Value);
table.Rows.Add(new DateTime(2010, 8, 15), 168.847, DBNull.Value);
table.Rows.Add(new DateTime(2010, 2, 16), 65.054, DBNull.Value);
table.Rows.Add(new DateTime(2010, 4, 30), 87.95, DBNull.Value);
table.Rows.Add(new DateTime(2010, 7, 22), 874.005, DBNull.Value);
table.Rows.Add(new DateTime(2010, 5, 09), 687.778, DBNull.Value);
return JSON.Serialize(table);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var alertDate = false;
function renderDate(value)
{
if (!alertDate && Ext.isEmpty(value))
{
alertDate = true;
alert('Date is null');
}
return value
}
function loadData()
{
var dateColumn = { dataIndex: 'DATE', header: 'Date', width: 210, xtype: 'datecolumn' };
var numberColumn = { dataIndex: 'VALUE', header: 'Value', width: 120, xtype: 'numbercolumn' };
var numberColumnNVL = { dataIndex: 'VALUE_NULL', header: 'Null Value (EMPTY)', width: 120, xtype: 'numbercolumn' };
var dateField = { name: 'DATE', type: 'date', allowBlank: true, defaultValue: '' };
var numberField = { name: 'VALUE', type: 'float', allowBlank: true, defaultValue: '' };
var numberFieldNVL = { name: 'VALUE_NULL', type: 'float', allowBlank: true, defaultValue: '' };
if (!Ext.isEmpty(grdDate.store))
grdDate.store.removeAll(false);
var store = new Ext.data.JsonStore({
id: grdDate.id + 'Store',
autoSave: false,
autoLoad: false,
fields: [dateField, numberField, numberFieldNVL]
});
var columnModel = new Ext.grid.ColumnModel({
id: grdDate.id + 'ColumnModel',
columns: [dateColumn, numberColumn, numberColumnNVL],
defaults: {
fixed: true,
hideable: false,
menuDisabled: true,
sortable: false
}
});
columnModel.setRenderer(0, renderDate);
grdDate.reconfigure(store, columnModel);
Ext.net.DirectMethods.LoadData({
success: function(result)
{
var table = Ext.decode(result);
display.setValue(result);
grdDate.store.loadData(table);
}
})
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<ext:ResourceManager runat="server" />
<ext:Button ID="btnLoad" runat="server" Text="Load">
<Listeners>
<Click Handler="loadData();" />
</Listeners>
</ext:Button>
<ext:GridPanel ID="grdDate" runat="server" Width="500" Height="300">
<Store>
<ext:Store runat="server" />
</Store>
<ColumnModel runat="server" />
</ext:GridPanel>
<br />
<br />
<ext:DisplayField ID="display" runat="server" />
</div>
</form>
</body>
</html>
Last edited by Daniil; Nov 11, 2010 at 9:15 AM.
Reason: Please use [CODE] tags, [CLOSED]