dnd_grid_to_formpanel.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Ext.require(['*']);
  2. Ext.onReady(function(){
  3. var myData = [
  4. { name : "Record 0", column1 : "0", column2 : "0" },
  5. { name : "Record 1", column1 : "1", column2 : "1" },
  6. { name : "Record 2", column1 : "2", column2 : "2" },
  7. { name : "Record 3", column1 : "3", column2 : "3" },
  8. { name : "Record 4", column1 : "4", column2 : "4" },
  9. { name : "Record 5", column1 : "5", column2 : "5" },
  10. { name : "Record 6", column1 : "6", column2 : "6" },
  11. { name : "Record 7", column1 : "7", column2 : "7" },
  12. { name : "Record 8", column1 : "8", column2 : "8" },
  13. { name : "Record 9", column1 : "9", column2 : "9" }
  14. ];
  15. // Generic fields array to use in both store defs.
  16. Ext.define('DataObject', {
  17. extend: 'Ext.data.Model',
  18. fields: [
  19. {name: 'name', mapping : 'name'},
  20. {name: 'column1', mapping : 'column1'},
  21. {name: 'column2', mapping : 'column2'}
  22. ]
  23. });
  24. // create the data store
  25. var gridStore = Ext.create('Ext.data.Store', {
  26. model : 'DataObject',
  27. data : myData
  28. });
  29. // Column Model shortcut array
  30. var columns = [
  31. { id : 'name', flex: 1, header: "Record Name", sortable: true, dataIndex: 'name'},
  32. {header: "column1", width: 50, sortable: true, dataIndex: 'column1'},
  33. {header: "column2", width: 50, sortable: true, dataIndex: 'column2'}
  34. ];
  35. // declare the source Grid
  36. var grid = Ext.create('Ext.grid.Panel', {
  37. viewConfig: {
  38. plugins: {
  39. ddGroup: 'GridExample',
  40. ptype: 'gridviewdragdrop',
  41. enableDrop: false
  42. }
  43. },
  44. store : gridStore,
  45. columns : columns,
  46. enableDragDrop : true,
  47. stripeRows : true,
  48. width : 325,
  49. margins : '0 2 0 0',
  50. region : 'west',
  51. title : 'Data Grid',
  52. selModel : Ext.create('Ext.selection.RowModel', {singleSelect : true})
  53. });
  54. // Declare the text fields. This could have been done inline, is easier to read
  55. // for folks learning :)
  56. var textField1 = Ext.create('Ext.form.field.Text', {
  57. fieldLabel : 'Record Name',
  58. name : 'name'
  59. });
  60. var textField2 = Ext.create('Ext.form.field.Text', {
  61. fieldLabel : 'Column 1',
  62. name : 'column1'
  63. });
  64. var textField3 = Ext.create('Ext.form.field.Text', {
  65. fieldLabel : 'Column 2',
  66. name : 'column2'
  67. });
  68. // Setup the form panel
  69. var formPanel = Ext.create('Ext.form.Panel', {
  70. region : 'center',
  71. title : 'Generic Form Panel',
  72. bodyStyle : 'padding: 10px; background-color: #DFE8F6',
  73. labelWidth : 100,
  74. margins : '0 0 0 3',
  75. items : [
  76. textField1,
  77. textField2,
  78. textField3
  79. ]
  80. });
  81. //Simple 'border layout' panel to house both grids
  82. var displayPanel = Ext.create('Ext.Panel', {
  83. width : 650,
  84. height : 300,
  85. layout : 'border',
  86. renderTo : 'panel',
  87. bodyPadding: 5,
  88. items : [
  89. grid,
  90. formPanel
  91. ],
  92. bbar : [
  93. '->', // Fill
  94. {
  95. text : 'Reset Example',
  96. handler : function() {
  97. //refresh source grid
  98. gridStore.loadData(myData);
  99. formPanel.getForm().reset();
  100. }
  101. }
  102. ]
  103. });
  104. /****
  105. * Setup Drop Targets
  106. ***/
  107. // This will make sure we only drop to the view container
  108. var formPanelDropTargetEl = formPanel.body.dom;
  109. var formPanelDropTarget = Ext.create('Ext.dd.DropTarget', formPanelDropTargetEl, {
  110. ddGroup: 'GridExample',
  111. notifyEnter: function(ddSource, e, data) {
  112. //Add some flare to invite drop.
  113. formPanel.body.stopAnimation();
  114. formPanel.body.highlight();
  115. },
  116. notifyDrop : function(ddSource, e, data){
  117. // Reference the record (single selection) for readability
  118. var selectedRecord = ddSource.dragData.records[0];
  119. // Load the record into the form
  120. formPanel.getForm().loadRecord(selectedRecord);
  121. // Delete record from the source store. not really required.
  122. ddSource.view.store.remove(selectedRecord);
  123. return true;
  124. }
  125. });
  126. });