Ext.lingo.JsonGrid.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Ext JS Library 2.0.2
  3. * Copyright(c) 2006-2008, Ext JS, LLC.
  4. * licensing@extjs.com
  5. *
  6. * http://www.extjs.com/license
  7. *
  8. * @author Lingo
  9. * @since 2008-03-23
  10. * http://code.google.com/p/anewssystem/
  11. */
  12. Ext.namespace("Ext.lingo");
  13. /**
  14. * 拥有CRUD功能的表格.
  15. *
  16. * @param config 需要的配置{}
  17. */
  18. Ext.lingo.JsonGrid = Ext.extend(Ext.grid.GridPanel, {
  19. // 初始化
  20. initComponent: function() {
  21. //this.useHistory = this.useHistory !== false;
  22. Ext.applyIf(this, {
  23. urlPagedQuery: "pagedQuery.do",
  24. urlLoadData: "loadData.do",
  25. urlSave: "save.do",
  26. urlRemove: "remove.do",
  27. pageSize: 15,
  28. loadMask: true,
  29. stripeRows: true
  30. });
  31. this.buildColumnModel();
  32. this.buildRecord();
  33. this.buildDataStore();
  34. if (this.createHeader !== false) {
  35. this.buildToolbar();
  36. }
  37. // 设置baseParams
  38. this.setBaseParams();
  39. Ext.lingo.JsonGrid.superclass.initComponent.call(this);
  40. this.on('rowdblclick', this.edit, this);
  41. //右键菜单
  42. this.on('rowcontextmenu', this.contextmenu, this);
  43. this.loadMask = true;
  44. },
  45. // 初始化ColumnModel
  46. buildColumnModel: function() {
  47. this.sm = new Ext.grid.CheckboxSelectionModel();
  48. var columnHeaders = new Array();
  49. columnHeaders[0] = new Ext.grid.RowNumberer();
  50. columnHeaders[1] = this.sm;
  51. for (var i = 0; i < this.formConfig.length; i++) {
  52. var col = this.formConfig[i];
  53. if (col.hideGrid === true) {
  54. continue;
  55. }
  56. columnHeaders.push({
  57. header: col.fieldLabel,
  58. sortable: col.sortable,
  59. dataIndex: col.name,
  60. renderer: col.renderer
  61. });
  62. }
  63. this.cm = new Ext.grid.ColumnModel(columnHeaders);
  64. this.cm.defaultSortable = true;
  65. this.columnModel = this.cm;
  66. },
  67. buildRecord: function() {
  68. this.dataRecord = Ext.data.Record.create(this.formConfig);
  69. },
  70. buildDataStore: function() {
  71. this.store = new Ext.data.Store({
  72. proxy : new Ext.data.HttpProxy({url:this.urlPagedQuery}),
  73. reader : new Ext.data.JsonReader({
  74. root : "result",
  75. totalProperty : "totalCount",
  76. id : "id"
  77. }, this.dataRecord),
  78. remoteSort : true
  79. });
  80. // this.store.setDefaultSort("id", "DESC");
  81. this.on('render', function() {
  82. var o = {
  83. params: {
  84. start: 0,
  85. limit: this.pageSize
  86. }
  87. };
  88. this.store.load.defer(10, this.store, [o]);
  89. }, this);
  90. },
  91. buildToolbar: function() {
  92. //
  93. var checkItems = new Array();
  94. for (var i = 0; i < this.formConfig.length; i++) {
  95. var meta = this.formConfig[i];
  96. if (meta.showInGrid === false) {
  97. continue;
  98. }
  99. var item = new Ext.menu.CheckItem({
  100. text : meta.fieldLabel,
  101. value : meta.name,
  102. checked : true,
  103. group : "filter",
  104. checkHandler : this.onItemCheck.createDelegate(this)
  105. });
  106. checkItems.push(item);
  107. }
  108. this.filterButton = new Ext.Toolbar.MenuButton({
  109. iconCls : "refresh",
  110. text : this.formConfig[0].fieldLabel,
  111. tooltip : "选择搜索的字段",
  112. menu : checkItems,
  113. minWidth : 105
  114. });
  115. // 输入框
  116. this.filter = new Ext.form.TextField({
  117. 'name': 'filter'
  118. });
  119. this.tbar = new Ext.Toolbar([{
  120. id : 'add',
  121. text : '新增',
  122. iconCls : 'add',
  123. tooltip : '新增',
  124. handler : this.add.createDelegate(this)
  125. }, {
  126. id : 'edit',
  127. text : '修改',
  128. iconCls : 'edit',
  129. tooltip : '修改',
  130. handler : this.edit.createDelegate(this)
  131. }, {
  132. id : 'del',
  133. text : '删除',
  134. iconCls : 'delete',
  135. tooltip : '删除',
  136. handler : this.del.createDelegate(this)
  137. }, '->', this.filterButton, this.filter]);
  138. this.filter.on('specialkey', this.onFilterKey.createDelegate(this));
  139. // 把分页工具条,放在页脚
  140. var paging = new Ext.PagingToolbar({
  141. pageSize: this.pageSize,
  142. store: this.store,
  143. displayInfo: true,
  144. displayMsg: '显示第 {0} 条到 {1} 条记录,一共 {2} 条',
  145. emptyMsg: "没有记录",
  146. plugins: [new Ext.ux.PageSizePlugin()]
  147. });
  148. this.bbar = paging;
  149. },
  150. // 设置baseParams
  151. setBaseParams : function() {
  152. // 读取数据
  153. /*
  154. this.store.on('beforeload', function() {
  155. this.store.baseParams = {
  156. filterValue : this.filter.getValue(),
  157. filterTxt : this.filterTxt
  158. };
  159. }.createDelegate(this));
  160. */
  161. },
  162. // 监听模糊搜索框里的按键
  163. onFilterKey: function(field, e) {
  164. var filterTxt = this.store.baseParams.filterTxt;
  165. if (typeof filterTxt == 'undefined' || filterTxt == '') {
  166. Ext.Msg.alert('提示', '请先选择搜索的字段');
  167. return;
  168. }
  169. if(e.getKey() == e.ENTER && field.getValue().length > 0) {
  170. this.store.baseParams.filterValue = field.getValue();
  171. } else if (e.getKey() == e.BACKSPACE && field.getValue().length() === 0) {
  172. this.store.baseParams.filterValue = '';
  173. } else {
  174. return;
  175. }
  176. // delete this.store.lastOptions.params.meta;
  177. this.store.reload();
  178. },
  179. // 检测至少选择一个
  180. checkOne: function() {
  181. var selections = this.getSelections();
  182. if (selections.length == 0) {
  183. Ext.MessageBox.alert("提示", "请选择一条的记录!");
  184. return false;
  185. } else if (selections.length != 1) {
  186. Ext.MessageBox.alert("提示", "不能选择多行!");
  187. return false;
  188. }
  189. return true;
  190. },
  191. // 检测必须选择一个
  192. checkMany: function() {
  193. var selections = this.getSelections();
  194. if (selections.length == 0) {
  195. Ext.MessageBox.alert("提示", "请至少选择一条的记录!");
  196. return false;
  197. }
  198. return true;
  199. },
  200. batchSubmit: function(url) {
  201. if (this.checkMany()) {
  202. Ext.Msg.confirm("提示", "是否确定?", function(btn, text) {
  203. if (btn == 'yes') {
  204. var selections = this.getSelections();
  205. var ids = new Array();
  206. for(var i = 0, len = selections.length; i < len; i++){
  207. try {
  208. // 如果选中的record没有在这一页显示,remove就会出问题
  209. selections[i].get("id");
  210. ids[i] = selections[i].get("id");
  211. //this.store.remove(selections[i]);//从表格中删除
  212. } catch (e) {
  213. }
  214. //if (this.useHistory) {
  215. // this.grid.selModel.Set.clear();
  216. //}
  217. }
  218. this.body.mask('提交数据,请稍候...', 'x-mask-loading');
  219. Ext.Ajax.request({
  220. url : url + '?ids=' + ids,
  221. success : function() {
  222. this.body.unmask();
  223. Ext.MessageBox.alert('提示', '操作成功!');
  224. this.refresh();
  225. }.createDelegate(this),
  226. failure : function(){
  227. this.el.unmask();
  228. Ext.MessageBox.alert('提示', '操作失败!');
  229. }
  230. });
  231. }
  232. }.createDelegate(this));
  233. }
  234. },
  235. // 弹出添加对话框,添加一条新记录
  236. add : function() {
  237. if (!this.dialog) {
  238. this.createDialog();
  239. }
  240. this.formPanel.getForm().reset();
  241. this.dialog.show(Ext.get("add"));
  242. },
  243. // 弹出修改对话框
  244. edit : function() {
  245. if (!this.dialog) {
  246. this.createDialog();
  247. }
  248. if (this.checkOne()) {
  249. this.dialog.show(Ext.get('edit'));
  250. this.formPanel.load({
  251. url: this.urlLoadData + "?id=" + this.getSelections()[0].id,
  252. success: this.formLoadSuccess.createDelegate(this)
  253. });
  254. }
  255. },
  256. formLoadSuccess: function(form, action) {
  257. },
  258. // 删除记录
  259. del: function() {
  260. this.batchSubmit(this.urlRemove);
  261. },
  262. // 创建弹出式对话框
  263. createDialog : function() {
  264. var formItems = [];
  265. for (var i = 0, len = this.formConfig.length; i < len; i++) {
  266. var item = this.formConfig[i];
  267. if (item.hideForm) {
  268. continue;
  269. }
  270. item.anchor = '90%';
  271. formItems.push(item);
  272. }
  273. var reader = new Ext.data.JsonReader({}, this.formConfig);
  274. this.formPanel = new Ext.form.FormPanel({
  275. defaultType: 'textfield',
  276. labelAlign: 'right',
  277. labelWidth: 70,
  278. frame: true,
  279. autoScroll: true,
  280. title: '详细信息',
  281. reader: reader,
  282. url: this.urlSave,
  283. items: formItems,
  284. buttons: [{
  285. text: '确定',
  286. handler: function() {
  287. if (this.formPanel.getForm().isValid()) {
  288. this.formPanel.getForm().submit({
  289. waitTitle: "请稍候",
  290. waitMsg : '提交数据,请稍候...',
  291. success: function() {
  292. this.dialog.hide();
  293. this.refresh();
  294. },
  295. failure: function() {
  296. },
  297. scope: this
  298. });
  299. }
  300. }.createDelegate(this)
  301. },{
  302. text: '取消',
  303. handler: function() {
  304. this.dialog.hide();
  305. }.createDelegate(this)
  306. }]
  307. });
  308. this.dialog = new Ext.Window({
  309. layout: 'fit',
  310. width: this.dlgWidth ? this.dlgWidth : 400,
  311. height: this.dlgHeight ? this.dlgHeight : 300,
  312. closeAction: 'hide',
  313. items: [this.formPanel]
  314. });
  315. },
  316. // 选中搜索属性选项时
  317. onItemCheck : function(item, checked) {
  318. if(checked) {
  319. this.filterButton.setText(item.text + ':');
  320. this.store.baseParams.filterTxt = item.value;
  321. this.store.baseParams.filterValue = '';
  322. }
  323. },
  324. // 弹出右键菜单
  325. // 修改,和批量删除的功能
  326. // 多选的时候,不允许修改就好了
  327. contextmenu : function(grid, rowIndex, e) {
  328. e.preventDefault();
  329. e.stopEvent();
  330. var updateMenu = new Ext.menu.Item({
  331. iconCls : 'edit',
  332. id : 'updateMenu',
  333. text : '修改',
  334. handler : this.edit.createDelegate(this)
  335. });
  336. var removeMenu = new Ext.menu.Item({
  337. iconCls : 'delete',
  338. id : 'removeMenu',
  339. text : '删除',
  340. handler : this.del.createDelegate(this)
  341. });
  342. var selections = this.getSelections();
  343. if (selections.length > 1) {
  344. updateMenu.disable();
  345. }
  346. var menuList = [updateMenu, removeMenu];
  347. this.grid_menu = new Ext.menu.Menu({
  348. id : 'mainMenu',
  349. items : menuList
  350. });
  351. var coords = e.getXY();
  352. grid.getSelectionModel().selectRow(rowIndex);
  353. this.grid_menu.showAt([coords[0], coords[1]]);
  354. },
  355. // 刷新表格数据
  356. refresh : function() {
  357. this.store.reload();
  358. },
  359. onDestroy : function(){
  360. if(this.rendered){
  361. if (this.grid_menu) {
  362. try {
  363. this.grid_menu.destory();
  364. } catch(e) {
  365. console.error(e);
  366. }
  367. }
  368. if (this.dialog) {
  369. this.dialog.destroy();
  370. }
  371. }
  372. Ext.lingo.JsonGrid.superclass.onDestroy.call(this);
  373. }
  374. });