View Full Version : Example of using composite field of Date and Time fields as a grid column editor

Sep 06, 2010, 3:18 PM
This is an example following on from this thread:

Original problem:
- I was trying to figure out how I can edit data and time in the same column of an editable grid
- I tried a Composite field made up of Date and Time field.
- It kinda worked but the column needed to by dynamically resized when going into edit mode, and coming out of it.
- With help from the Ext.Net Dev team I was able to get it working
- So I posted a working sample.

But here I thought I'd post a slight modified version. The original sample I posted above had JavaScript in the various handlers. In this example below, it is the same JavaScript but moved into the script block so that the idea can be reused by both a grid generated by code behind and a grid generated with markup, more easily.

Important: The JavaScript itself is not final (needs more validation etc) but a useful pattern or structure perhaps.

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
if (!ExtNet.IsAjaxRequest)
Store store = GridPanel1.Store.Primary;
store.DataSource = new object[] {
new object[] {"Joe", DateTime.Now},
new object[] {"Bob", DateTime.Now},
new object[] {"Fred", DateTime.Now}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Ext.Net Example of CompositeField for Date and Time entry</title>

<script type="text/javascript">
var Examples = {
dateTime: function (value) {
return value.format ? value.format('Y-m-d H:i:s') : value;

dateTimeCompositeField: {
beforeStartEdit: function (gridPanel) {
var columnModel = gridPanel.getColumnModel();

this.originalWidthBeforeEditing = columnModel.getColumnWidth(this.col);
columnModel.setColumnWidth(this.col, 150);

startEdit: function (value) {
var items = this.field.items,
dateField = items.map[items.keys[0]],
timeField = items.map[items.keys[1]];


beforeComplete: function (gridPanel) {
var items = this.field.items,
dateField = items.map[items.keys[0]],
timeField = items.map[items.keys[1]],
newDate = dateField.getValue(),
newTime = Date.parseDate(timeField.getValue(), 'H:i:s');

gridPanel.getColumnModel().setColumnWidth(this.col , this.originalWidthBeforeEditing);

cancelEdit: function (gridPanel) {
gridPanel.getColumnModel().setColumnWidth(this.col , this.originalWidthBeforeEditing);
<form id="Form1" runat="server">
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
<ext:Store ID="Store1" runat="server">
<ext:RecordField Name="Name" />
<ext:RecordField Name="LastUpdated" Type="Date" />
<ColumnModel ID="ColumnModel1" runat="server">
<ext:Column Header="Name" DataIndex="Name" />
<ext:Column Header="Last Updated" DataIndex="LastUpdated" Width="110">
<Renderer Fn="Examples.dateTime" />
<ext:CompositeField ID="DateField1" runat="server">
<ext:DateField ID="CompositeDateField1" runat="server" />
<ext:TextField ID="CompositeTimeField1" runat="server" />
<EditorOptions AllowBlur="false">
<BeforeStartEdit Handler="Examples.dateTimeCompositeField.beforeStartEdit.ca ll(this, #{GridPanel1});" />
<StartEdit Handler="Examples.dateTimeCompositeField.startEdit.call(thi s, value);" />
<BeforeComplete Handler="Examples.dateTimeCompositeField.beforeComplete.cal l(this, #{GridPanel1});" />
<CancelEdit Handler="Examples.dateTimeCompositeField.cancelEdit.call(th is, #{GridPanel1});" />
</html>In the listeners, you will see I have used JavaScript's "call" method so that I can continue using "this" in the actual method (easier to spot in color-coded text editors and Visual Studio!).

Does anyone know of way to use "Fn" instead of "Handler" and pass in the GridPanel? The reason I ask is that using Handler creates an inline function, and in my case I just want to call another function, so it would be nice to avoid creating the inline function just for that). I've looked at the ExtJs documentation for startEdit and some of the other handlers, and it looks like the arguments they receive are fixed, so I am guessing we can't pass GridPanel to the method this way?

Sep 06, 2010, 4:08 PM
I should add a note here that in the composite field, the time bit is a TextField, not a TimeField, because (at least in my case) i need hours, minutes and seconds. If you don't need seconds then you can probably modify it to work with a TimeField. You will likely need to modify the JavaScript accordingly, because where "newTime" is being set, that may have to change.

Hilariously, I caught myself out on this as I was using the above example to create a code behind version and was adding a TimeField instead of a TextField :o

Sep 27, 2010, 5:23 AM
Can you show me how to add new record (have a date field in store) with javascript?

Jul 24, 2012, 9:40 PM
Here is the related discussion regarding Ext.NET v2.