ComboSetGrid.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. Ext.define('erp.view.sys.base.ComboSetGrid',{
  2. extend: 'Ext.grid.Panel',
  3. alias: 'widget.combosetgrid',
  4. columnLines: true,
  5. viewConfig: {stripeRows:true},
  6. //frame: true,
  7. /* plugins:[Ext.create('Ext.grid.plugin.CellEditing', {
  8. clicksToMoveEditor: 1,
  9. autoCancel: false
  10. })],*/
  11. border:true,
  12. caller:null,
  13. combofield:null,
  14. dockedItems: [{
  15. xtype: 'toolbar',
  16. dock: 'top',
  17. ui: 'footer',
  18. /* layout: {
  19. pack: 'left'
  20. },*/
  21. items: [{
  22. iconCls:'btn-add',
  23. text: '添加',
  24. toolTip:'添加条目',
  25. scope:this,
  26. listeners:{
  27. click:function(btn){
  28. btn.up('combosetgrid').addRecord(btn);
  29. }
  30. }
  31. //handler: Ext.bind(this.addRecord, this)
  32. }]
  33. }],
  34. initComponent : function(){
  35. var me=this;
  36. me.plugins = [me.cellEditingPlugin = Ext.create('Ext.grid.plugin.CellEditing',{
  37. clicksToEdit:1,
  38. listeners:{
  39. 'edit':function(editor,e,Opts){
  40. var record=e.record,update={
  41. dlc_id:record.data.dlc_id==0?null:record.data.dlc_id,
  42. dlc_value:e.value,
  43. dlc_display:e.value,
  44. dlc_fieldname:e.grid.field,
  45. dlc_caller:e.grid.caller
  46. };
  47. if(e.originalValue!=e.value && e.value){
  48. var param=new Array();
  49. param.push(Ext.JSON.encode(update));
  50. Ext.Ajax.request({
  51. url:basePath+'common/saveCombo.action',
  52. params: {
  53. gridStore:unescape(param.toString())
  54. },
  55. method : 'post',
  56. callback : function(options,success,response){
  57. var local=Ext.decode(response.responseText);
  58. if(local.success) {
  59. showResult('提示','修改成功!');
  60. record.commit();
  61. }else {
  62. showResult('提示',local.exceptionInfo);
  63. }
  64. }
  65. });
  66. }
  67. }
  68. }
  69. })];
  70. Ext.applyIf(me,{
  71. columns:me.getColumns(me),
  72. store:Ext.create('Ext.data.Store',{
  73. fields:[{name:'dlc_value',type:'string'},{name:'dlc_id',type:'int'}]
  74. })
  75. });
  76. this.callParent(arguments);
  77. this.getComboData(this);
  78. },
  79. getComboData: function(grid){
  80. var me = this;
  81. var params={
  82. caller:this.caller,
  83. field:this.field
  84. };
  85. //this.getGridColumnsAndStore(this, 'common/singleGridPanel.action', params, "");
  86. grid.setLoading(true);
  87. Ext.Ajax.request({
  88. url : basePath + 'common/getComboDataByCallerAndField.action',
  89. params: params,
  90. async: (grid.sync ? false : true),
  91. method : 'get',
  92. callback : function(options,success,response){
  93. grid.setLoading(false);
  94. if (!response) return;
  95. var res = new Ext.decode(response.responseText);
  96. if(res.exceptionInfo){
  97. showError(res.exceptionInfo);return;
  98. }
  99. grid.store.loadData(res.data);
  100. }
  101. });
  102. },
  103. setStore: function(grid, fields, data, groupField, necessaryField){
  104. Ext.each(fields, function(f){
  105. if(f.name.indexOf(' ') > -1) {// column有取别名
  106. f.name = f.name.split(' ')[1];
  107. }
  108. if(!Ext.isChrome){
  109. if(f.type == 'date'){
  110. f.dateFormat = 'Y-m-d H:m:s';
  111. }
  112. }
  113. });
  114. var modelName = 'ext-model-' + grid.id;
  115. Ext.define(modelName, {
  116. extend: 'Ext.data.Model',
  117. fields: fields
  118. });
  119. var config = {
  120. model: modelName,
  121. groupField: groupField,
  122. getSum: function(records, field) {
  123. if (arguments.length < 2) {
  124. return 0;
  125. }
  126. var total = 0,
  127. i = 0,
  128. len = records.length;
  129. if(necessaryField) {
  130. for (; i < len; ++i) {//重写getSum,grid在合计时,只合计填写了必要信息的行
  131. var necessary = records[i].get(necessaryField);
  132. if(necessary != null && necessary != ''){
  133. total += records[i].get(field);
  134. }
  135. }
  136. } else {
  137. for (; i < len; ++i) {
  138. total += records[i].get(field);
  139. }
  140. }
  141. return total;
  142. },
  143. getCount: function() {
  144. if(necessaryField) {
  145. var count = 0;
  146. Ext.each(this.data.items, function(item){//重写getCount,grid在合计时,只合计填写了必要信息的行
  147. if(item.data[necessaryField] != null && item.data[necessaryField] != ''){
  148. count++;
  149. }
  150. });
  151. return count;
  152. }
  153. return this.data.items.length;
  154. }
  155. };
  156. if(grid.buffered) {//grid数据缓存
  157. config.buffered = true;
  158. config.pageSize = 200;
  159. config.purgePageCount = 0;
  160. config.proxy = {
  161. type: 'memory'
  162. };
  163. } else {
  164. config.data = data;
  165. }
  166. var store = Ext.create('Ext.data.Store', config);
  167. store.each(function(item, x){
  168. item.index = x;
  169. });
  170. if(grid.buffered) {
  171. var ln = data.length, records = [], i = 0;
  172. for (; i < ln; i++) {
  173. records.push(Ext.create(modelName, data[i]));
  174. }
  175. store.cacheRecords(records);
  176. }
  177. return store;
  178. },
  179. loadNewStore: function(grid, param){
  180. var me = this;
  181. param=param||grid.params;
  182. grid.setLoading(true);//loading...
  183. Ext.Ajax.request({//拿到grid的columns
  184. url : basePath + "common/loadNewGridStore.action",
  185. params: param,
  186. method : 'post',
  187. callback : function(options,success,response){
  188. grid.setLoading(false);
  189. var res = new Ext.decode(response.responseText);
  190. if(res.exceptionInfo){
  191. showError(res.exceptionInfo);return;
  192. }
  193. var data = res.data;
  194. if(!data || data.length == 0){
  195. grid.store.removeAll();
  196. me.add10EmptyItems(grid);
  197. } else {
  198. grid.store.loadData(data);
  199. }
  200. //自定义event
  201. grid.addEvents({
  202. storeloaded: true
  203. });
  204. grid.fireEvent('storeloaded', grid, data);
  205. }
  206. });
  207. },
  208. removeDetail:function(grid,id,record){
  209. grid.setLoading(true);
  210. Ext.Ajax.request({
  211. url : basePath + 'common/deleteCombo.action',
  212. params: {
  213. id: id
  214. },
  215. method : 'post',
  216. callback : function(options,success,response){
  217. grid.setLoading(false);
  218. var localJson = new Ext.decode(response.responseText);
  219. if(localJson.exceptionInfo){
  220. showError(localJson.exceptionInfo);return;
  221. }
  222. if(localJson.success){
  223. showResult('提示','删除成功!');
  224. grid.getStore().remove(record);
  225. } else {
  226. delFailure();
  227. }
  228. }
  229. });
  230. },
  231. getColumns:function(grid){
  232. var editor={
  233. xtype:grid.editType||'textfield'
  234. };
  235. return [{
  236. width:grid.fieldWidth||200,
  237. dataIndex:'dlc_value',
  238. text:grid.title,
  239. editor:editor
  240. },{
  241. xtype:'actioncolumn',
  242. width:45,
  243. items:[
  244. {
  245. iconCls:'btn-delete',
  246. tooltip:'删除',
  247. width:50,
  248. handler:function(grid, rowIndex, colIndex) {
  249. Ext.Msg.confirm('删除数据?', '确定要删除当前选中行(行号:'+(rowIndex+1)+')?',
  250. function(choice) {
  251. if(choice === 'yes') {
  252. //var reviewStore = Ext.data.StoreMgr.lookup('reviewStore');
  253. var record = grid.getStore().getAt(rowIndex),gridpanel=grid.ownerCt;
  254. gridpanel.removeDetail(gridpanel,record.get('dlc_id'),record);
  255. }
  256. }
  257. );
  258. }
  259. }]
  260. }];
  261. },
  262. DetailUpdateSuccess:function(btn,type){
  263. var tabP=Ext.getCmp('saletabpanel'),_activeTab=tabP.activeTab;
  264. _activeTab.loadNewStore(_activeTab,_activeTab.params);
  265. var win=btn.up('window');
  266. if(win) win.close();
  267. },
  268. addRecord:function(btn){
  269. var combogrid=btn.up('combosetgrid'),
  270. edit = combogrid.cellEditingPlugin;
  271. edit.cancelEdit();
  272. combogrid.store.insert(0, {});
  273. edit.startEditByPosition({
  274. row: 0,
  275. column: 1
  276. });
  277. }
  278. })