For example .....
<e-grid-column field="DateOfBirth" headerText="Date Of Birth" type="date" textAlign="Right" width="250" format="dd/MM/yyyy" template="#ageTemplate"
What does the scrip section look like if i just want to calculate and display the age in a column:
<script id="ageTemplate" type="text/x-template">
????
</script>
Regards
|
<div>
<ejs-grid id="Grid" dataSource="ViewBag.DataSource" allowPaging="true">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" textAlign="Right" width="120"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
<e-grid-column field="OrderDate" format="yMd" headerText="Order Date" type="date" format="yMd" width="10%"></e-grid-column>
<e-grid-column field="ShippedDate" format="yMd" headerText="Shipped Date" width="150"></e-grid-column>
<e-grid-column headerText="Days to Dispatch" template="#dispatchtemplate" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
</div>
<script id="dispatchtemplate" type="text/template">
// here you can use custom helper function to perform your calculation
<td>${calculateDate(data)} days</td>
</script>
<script>
function calculateDate(args) {
// here you will get each row data and can perform your calculation and return the calculated value
return Math.abs(args.ShippedDate.getDate() - args.OrderDate.getDate());
}
</script>
|
Ok so I have centralized my calculateAge function in js, so that I can reuse it. I obviously want to calculate age on the DateOfBirth date picker when I edit in the grid. I had a create, read, write and destroy functions on the DateOfBirth column :
<e-grid-column field="DateOfBirth" headerText="Date Of Birth" type="date" textAlign="Right" width="250" format="dd/MM/yyyy"
edit="@(new {create = "createDateOfBirth", read = "readDateOfBirth", destroy = "destroyDateOfBirth", write = "writeDateOfBirth"})"></e-grid-column>
and obviously my Age column.....
<e-grid-column headerText="Age" template="#ageTemplate" width="150"></e-grid-column>
How do write a onChange event on the DateOfBirth picker to set the Age in the grids edit modal mode?
|
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" height="315" allowPaging="true">
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
<e-grid-columns>
. . . . . .
<e-grid-column field="DateOfBirth" headerText="DateOfBirth" format="dd/MM/yyyy" edit="@(new {create = "create", read = "read", destroy = "destroy", write = "write"})" width="120"></e-grid-column>
. . . . . .
</e-grid-columns>
</ejs-grid>
<script>
var elem;
var dObj;
function create(args) {
elem = document.createElement('input');
return elem;
}
function write(args) {
dObj = new ej.calendars.DatePicker({
value: new Date(args.rowData[args.column.field]),
change: function (args) {
//the change event triggers when the value is changed
}
});
dObj.appendTo(elem);
}
function destroy() {
dObj.destroy();
}
function read(args) {
return dObj.value;
}
</script>
|
|
Index.cshtml
function cellInfo(args) {
if (args.column.field === 'Freight') {
args.data.Freight = new Date().getYear() - args.data.OrderDate.getYear();
args.cell.innerText = args.data.Freight;
}
}
function actionBegin(args) {
if (args.requestType === "save") {
args.data.Freight = new Date().getYear() - args.data.OrderDate.getYear();
}
}
<ejs-grid id="Grid" allowPaging="true" load="onLoad" actionBegin="actionBegin" queryCellInfo="cellInfo" toolbar="@(new List<string>() {"Add", "Edit", "Update", "Delete" })">
<e-data-manager url="/Home/UrlDataSource" adaptor="UrlAdaptor" insertUrl="/Home/Insert" updateUrl="/Home/Update" removeUrl="/Home/Remove"></e-data-manager> |