I'm working on the jqgrid which gets bound dynamically. I have got the required output. But i wrote a big code which affects my performance and its not readable.
I need someone who can look into this and optimize my code simple.
Thanks in advance.
I have reproduced in my working code here
$.each($.parseJSON(columnsData).Table1, function () {
// debugger;
//Push the column name.
colHeader.push(this.Name);
//Check the datatype of the column.
switch (this.Datatype) {
case 'number':
if (this.DefaultValue != null && this.DefaultValue != "") {
// debugger;
colname.push({
name: this.Name, index: this.Name, width: 100, align: 'left', formatter: 'number', sortable: true, editable: false, sorttype: 'int', hidden: JSON.parse(this.IsHidden), editoptions: {
defaultValue: this.DefaultValue
}, editrules: { required: JSON.parse(this.IsRequired) }
});
}
else {
colname.push({
name: this.Name, index: this.Name, width: 100, align: 'left', formatter: 'number', sortable: true, editable: false, sorttype: 'int', hidden: JSON.parse(this.IsHidden), editrules: { required: JSON.parse(this.IsRequired) }
});
}
lastFieldName = this.Name.toString(); //Store the fieldName.
break;
case 'DateTime':
if (this.DefaultValue != null && this.DefaultValue != "") {
//If datetime then enable datepicker in the filter and edit form.
colname.push({
name: this.Name, search: true, index: this.Name, width: 100, stype: "text", editable: true, hidden: JSON.parse(this.IsHidden), searchoptions: {
dataInit: function (el) {
$(el).datepicker({
dateFormat: 'm/d/yy', maxDate: 0, changeMonth: true, changeYear: true, onSelect: function (dateText, inst) {
setTimeout(function () {
$('#TransactionsGrid')[0].triggerToolbar();
}, 50);
}
});
}
}, editoptions: {
dataInit: function (el) {
$(el).datepicker({
dateFormat: 'm/d/yy', maxDate: 0, changeMonth: true, changeYear: true
});
}, defaultValue: this.DefaultValue, readonly: 'readonly'
}, editrules: { required: JSON.parse(this.IsRequired) }
});
}
else {
colname.push({
name: this.Name, search: true, index: this.Name, width: 100, stype: "text", editable: true, hidden: JSON.parse(this.IsHidden), searchoptions: {
dataInit: function (el) {
$(el).datepicker({
dateFormat: 'm/d/yy', maxDate: 0, changeMonth: true, changeYear: true, onSelect: function (dateText, inst) {
setTimeout(function () {
$('#TransactionsGrid')[0].triggerToolbar();
}, 50);
}
});
}
}, editoptions: {
dataInit: function (el) {
$(el).datepicker({
dateFormat: 'm/d/yy', maxDate: 0, changeMonth: true, changeYear: true
});
}
}, editrules: { required: JSON.parse(this.IsRequired) }
});
}
lastFieldName = this.Name.toString();
break;
case 'dropdown':
if (this.DefaultValue != null && this.DefaultValue != "") {
// debugger;
if (this.ValueType == "F") {
colname.push({
name: this.Name, index: this.Name, width: 100, edittype: "select", editable: true, hidden: JSON.parse(this.IsHidden),
//formatter: imageFormatter, unformat: imageUnFormat,
/*(Set tooltip of the gridcell)
cellattr: function (rowId, val, rawObject, cm, rdata) {
if (rawObject[cm.name + "_Title"] == "") {
return 'title="' + rawObject[cm.name] + '"';
}
else
return 'title="' + val + ' (' + rawObject[cm.name + "_Title"] + ')"';
},*/
//IF dropdown then bind the values during edit form.
editoptions: { value: ':Select;' + this.ValueList.slice(0, -1), defaultValue: this.DefaultValue }, editrules: { required: JSON.parse(this.IsRequired) }, stype: 'select'
, searchoptions: { value: ':All;' + this.ValueList.slice(0, -1) }, align: 'left', sortable: true
});
}
else {
colname.push({
name: this.Name, index: this.Name, width: 100, edittype: "select", label: this.ValueId, hidden: JSON.parse(this.IsHidden),
//IF dropdown then bind the values during edit form.
editoptions: { value: ':Select;' + this.ValueList.slice(0, -1), defaultValue: this.DefaultValue }, editrules: { required: JSON.parse(this.IsRequired) }, stype: 'select'
, searchoptions: { value: ':All;' + this.ValueList.slice(0, -1) }, align: 'left', sortable: true
});
}
}
else {
if (this.ValueType == "F") {
colname.push({
name: this.Name, index: this.Name, width: 100, edittype: "select", editable: true, hidden: JSON.parse(this.IsHidden),
//IF dropdown then bind the values during edit form.
editoptions: { value: ':Select;' + this.ValueList.slice(0, -1) }, editrules: { required: JSON.parse(this.IsRequired) }, stype: 'select'
, searchoptions: { value: ':All;' + this.ValueList.slice(0, -1) }, align: 'left', sortable: true
});
}
else {
colname.push({
name: this.Name, index: this.Name, width: 100, edittype: "select", label: this.ValueId, hidden: JSON.parse(this.IsHidden),
//IF dropdown then bind the values during edit form.
editoptions: { value: ':Select;' + this.ValueList.slice(0, -1) }, editrules: { required: JSON.parse(this.IsRequired) }, stype: 'select'
, searchoptions: { value: ':All;' + this.ValueList.slice(0, -1) }, align: 'left', sortable: true
});
}
}
break;
default:
if (this.DefaultValue != null && this.DefaultValue != "") {
colname.push({
name: this.Name, index: this.Name, width: 100, align: 'left', sortable: true, editable: true, hidden: JSON.parse(this.IsHidden), editrules: { required: JSON.parse(this.IsRequired) }
});
}
else {
colname.push({
name: this.Name, index: this.Name, width: 100, align: 'left', sortable: true, editable: true, hidden: JSON.parse(this.IsHidden), editrules: { required: JSON.parse(this.IsRequired) }
});
}
break;
}
});
jQuery("#TransactionsGrid").jqGrid({
data: $.parseJSON(gridData).BuildTransactionsDataTable,
datatype: "local",
hoverrows: false,
colNames: colHeader,
colModel: colname,
id: 'TransactionId',
rowNum: 10,
rownumbers: true,
sortname: '_id',
viewrecords: true,
sortorder: 'desc',
caption: "Transaction Details",
height: '250px',
gridview: true,
ignoreCase: true
});
Since the code is too large to view, i have made the fiddle. Kindly look into that
Updated:
The cases i have to handle in my controller and when used the same in client(this.DataType
) side the code will be a big one.
// Code:
case FieldStyleModel.FieldType.Date:
case FieldStyleModel.FieldType.DropDownCalendar:
case FieldStyleModel.FieldType.DateWithoutDropDown:
case FieldStyleModel.FieldType.DateWithSpin:
drColumnDetails["Datatype"] = "date";
break;
case FieldStyleModel.FieldType.DateTime:
case FieldStyleModel.FieldType.DateTimeWithoutDropDown:
case FieldStyleModel.FieldType.DateTimeWithSpin:
drColumnDetails["Datatype"] = "datetime";
break;
case FieldStyleModel.FieldType.DropDown:
case FieldStyleModel.FieldType.DropDownList:
case FieldStyleModel.FieldType.DropDownValidate:
drColumnDetails["Datatype"] = "dropdown";
break;
case FieldStyleModel.FieldType.URL:
drColumnDetails["Datatype"] = "hyperlink";
break;
case FieldStyleModel.FieldType.IntegerNonNegative:
case FieldStyleModel.FieldType.IntegerNonNegativeWithSpin:
case FieldStyleModel.FieldType.IntegerPositive:
case FieldStyleModel.FieldType.IntegerPositiveWithSpin:
drColumnDetails["Datatype"] = "number";
break;
case FieldStyleModel.FieldType.Integer:
case FieldStyleModel.FieldType.IntegerWithSpin:
drColumnDetails["Datatype"] = "integer";
break;
case FieldStyleModel.FieldType.Time:
case FieldStyleModel.FieldType.TimeWithSpin:
case FieldStyleModel.FieldType.TimeZone:
drColumnDetails["Datatype"] = "Time";
break;
case FieldStyleModel.FieldType.CheckBox:
drColumnDetails["Datatype"] = "checkbox";
break;
default:
drColumnDetails["Datatype"] = "string";
break;
See Question&Answers more detail:
os