Extjs itself is a slow-loading JS framework, which requires more optimization by programmers. I have mentioned before Optimization of JS packaging
This time I will write about how to reduce redundant code, which is also an improvement. Running speed, including 1. Universal deletion code 2. Universal submission form 3. Universal initialization of Gird This time only write and delete code optimization
Post the code first
/**
* Get the selected record of a GridPanel
*/
function $getGdSelectedIds(grid, idName) {
var selRs = grid .getSelectionModel().getSelections();
var ids = Array();
for (var i = 0; i < selRs.length; i ) {
ids.push(eval("selRs [i].data." idName));
}
return ids;
}
/**
*Delete
*/
function $postDel(a) {
Ext .Msg.confirm("Message confirmation", "Are you sure you want to delete the selected record?",
function(b) {
if (b == "yes") {
Ext.getBody( ).mask("Deleting, please wait");
Ext.Ajax.request({
url: a.url,
params: {
ids: a.ids
},
timeout: 100000000,//default 30000 milliseconds
method: "POST",
success: function(c, d) {
Ext.getBody().unmask();
Ext.ux.Toast.msg("Operation information", "The record was successfully deleted!");
if (a.callback) {
a.callback.call(this);
return;
}
if (a.grid) {
a.grid.getStore().reload();
}
},
failure: function(c, d) {
Ext.getBody().unmask();
Ext.ux.Toast.msg("Operation information", "An operation error occurred, please contact the administrator! ");
}
});
}
});
}
/**
* Gird batch deletion operation
*/
function $delGridRs(a) {
var b = $getGdSelectedIds(a.grid, a.idName);
if (b.length == 0) {
Ext.ux.Toast.msg("Operation information", "Please select to delete record! ");
return;
}
var c = {
url: a.url,
ids: b,
grid: a.grid
} ;
$postDel(c);
}
The optimization result of a single deletion code was changed from the original 24 lines of code to 5 lines of code, and the code looks better.
var a = Ext.getCmp("PlanBookAllGrid");
//Single delete
$postDel({
url: __ctxPath "/traincost/multiDelPlanBook.action",
ids: b,
grid: a
});
The same goes for batch deletion
$delGridRs ({
url: __ctxPath "/traincost/multiDelPlanBook.action",
grid:c.gridPanel,
idName:'mainid'
});