【发布时间】:2014-11-25 02:10:47
【问题描述】:
我正在研究动态绑定的 jqgrid。我得到了所需的输出。但是我写了一个大代码,影响了我的性能并且不可读。
我需要有人可以研究这个并简单地优化我的代码。
提前致谢。
我已经在我的工作代码中转载了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
});
由于代码太大而无法查看,我做了小提琴。请调查一下
更新:
我必须在我的控制器中处理的情况以及在客户端(this.DataType)中使用相同的情况时,代码将是一个大代码。
// 代码:
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;
【问题讨论】:
标签: jquery optimization jqgrid