Drag and Drop from Grid to Grid Example,增加上下、左右移动的功能。

版本:extjs 2.3.0

需求:自定义可排序列选择。

例子:http://localhost/ext230/examples/dd/dnd_grid_to_grid.html

封装:暂无,给大家参考下。

效果如图1所示。

图1


c.html:

[html] view plain copy
  1. <html>
  2. <head>
  3. <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
  4. <title>test</title>
  5. <linkrel="stylesheet"type="text/css"href="./js/extjs/resources/css/ext-all.css"/>
  6. <scripttype="text/javascript"src="./js/extjs/ext-base.js"></script>
  7. <scripttype="text/javascript"src="./js/extjs/ext-all.js"></script>
  8. <scripttype="text/javascript"src="c.js"></script>
  9. </head>
  10. <body>
  11. <h1>DragandDropfromGridtoGridExample</h1>
  12. <p>ThisexampleshowshowtosetuptwowaydraganddropfromoneGridPaneltoanother.</p>
  13. <p>Notethatthejsisnotminifiedsoitisreadable.See<ahref="dnd_grid_to_grid.js">dnd_grid_to_grid.js</a>.</p>
  14. <divid="panel"></div>
  15. </body>
  16. </html>

c.js

[javascript] view plain copy
  1. /*
  2. *ExtJSLibrary2.3.0
  3. *Copyright(c)2006-2009,ExtJS,LLC.
  4. *licensing@extjs.com
  5. *
  6. *http://extjs.com/license
  7. */
  8. Ext.onReady(function(){
  9. Ext.QuickTips.init();
  10. varmyData={
  11. records:[
  12. {name:"名称0",column1:"0",column2:"0"},
  13. {name:"名称1",column1:"1",column2:"1"},
  14. {name:"名称2",column1:"2",column2:"2"},
  15. {name:"名称3",column1:"3",column2:"3"},
  16. {name:"名称4",column1:"4",column2:"4"},
  17. {name:"名称5",column1:"5",column2:"5"},
  18. {name:"名称6",column1:"6",column2:"6"},
  19. {name:"名称7",column1:"7",column2:"7"},
  20. {name:"名称8",column1:"8",column2:"8"},
  21. {name:"名称9",column1:"9",column2:"9"},
  22. {name:"名称10",column1:"9",column2:"9"},
  23. {name:"名称11",column1:"9",column2:"9"},
  24. {name:"名称12",column1:"9",column2:"9"},
  25. {name:"名称13",column1:"9",column2:"9"},
  26. {name:"名称14",column1:"9",column2:"9"},
  27. {name:"名称15",column1:"9",column2:"9"},
  28. {name:"名称16",column1:"9",column2:"9"},
  29. {name:"名称17",column1:"9",column2:"9"},
  30. {name:"名称18",column1:"9",column2:"9"},
  31. {name:"名称19",column1:"9",column2:"9"}
  32. ]
  33. };
  34. //Genericfieldsarraytouseinbothstoredefs.
  35. varfields=[
  36. {name:'name',mapping:'name'},
  37. {name:'column1',mapping:'column1'},
  38. {name:'column2',mapping:'column2'}
  39. ];
  40. //createthedatastore
  41. varfirstGridStore=newExt.data.JsonStore({
  42. fields:fields,
  43. data:myData,
  44. root:'records'
  45. });
  46. //ColumnModelshortcutarray
  47. varcols=[
  48. {id:'name',header:"列名称",
  49. //width:160,
  50. sortable:true,
  51. dataIndex:'name'
  52. }//,
  53. //{header:"column1",width:50,sortable:true,dataIndex:'column1'},
  54. //{header:"column2",width:50,sortable:true,dataIndex:'column2'}
  55. ];
  56. //declarethesourceGrid
  57. varfirstGrid=newExt.grid.GridPanel({
  58. ddGroup:'secondGridDDGroup',
  59. store:firstGridStore,
  60. columns:cols,
  61. enableDragDrop:true,
  62. stripeRows:true,
  63. autoExpandColumn:'name',
  64. //width:325,
  65. height:272,
  66. columnWidth:0.4,
  67. //region:'west',
  68. title:'待选择列'
  69. });
  70. varsecondGridStore=newExt.data.JsonStore({
  71. fields:fields,
  72. root:'records'
  73. });
  74. //createthedestinationGrid
  75. varsecondGrid=newExt.grid.GridPanel({
  76. ddGroup:'firstGridDDGroup',
  77. store:secondGridStore,
  78. columns:cols,
  79. enableDragDrop:true,
  80. stripeRows:true,
  81. autoExpandColumn:'name',
  82. height:272,
  83. columnWidth:0.4,
  84. title:'已选择列'
  85. });
  86. varlrPanel={
  87. columnWidth:0.1,
  88. height:300,
  89. items:[
  90. newExt.Button({
  91. text:">",
  92. style:'margin:70px0px0px15px',
  93. handler:function(){
  94. if(firstGrid.getSelectionModel().hasSelection())
  95. {
  96. //数据行数
  97. vartotal=secondGridStore.getCount();
  98. varrecords=firstGrid.getSelectionModel().getSelections();
  99. for(vari=0;i<records.length;i++){
  100. varrecord=records[i];
  101. firstGridStore.remove(record);
  102. secondGridStore.insert(total+i,record);
  103. }
  104. }
  105. }
  106. }),
  107. newExt.Button({
  108. text:">>",
  109. style:'margin:15px0px0px15px',
  110. handler:function(){
  111. //数据行数
  112. vartotal=secondGridStore.getCount();
  113. vari=0;
  114. while(firstGridStore.getCount()>0)
  115. {
  116. varrecord=firstGridStore.getAt(0);
  117. firstGridStore.remove(record);
  118. secondGridStore.insert(total+i,record);
  119. i++;
  120. }
  121. }
  122. }),
  123. newExt.Button({
  124. text:"<",
  125. style:'margin:15px0px0px15px',
  126. handler:function(){
  127. if(secondGrid.getSelectionModel().hasSelection())
  128. {
  129. //数据行数
  130. vartotal=firstGridStore.getCount();
  131. varrecords=secondGrid.getSelectionModel().getSelections();
  132. for(vari=0;i<records.length;i++){
  133. varrecord=records[i];
  134. secondGridStore.remove(record);
  135. firstGridStore.insert(total+i,record);
  136. }
  137. }
  138. }
  139. }),
  140. newExt.Button({
  141. text:"<<",
  142. style:'margin:15px0px0px15px',
  143. handler:function(){
  144. //数据行数
  145. vartotal=firstGridStore.getCount();
  146. vari=0;
  147. while(secondGridStore.getCount()>0)
  148. {
  149. varrecord=secondGridStore.getAt(0);
  150. secondGridStore.remove(record);
  151. firstGridStore.insert(total+i,record);
  152. i++;
  153. }
  154. }
  155. })
  156. ]
  157. };
  158. varudPanel={
  159. columnWidth:0.1,
  160. height:300,
  161. items:[
  162. newExt.Button({
  163. text:"向上",
  164. style:'margin:90px0px0px10px',
  165. handler:function(){
  166. if(secondGrid.getSelectionModel().hasSelection())
  167. {
  168. varrecord=secondGrid.getSelectionModel().getSelected();
  169. //数据行数
  170. vartotal=secondGridStore.getCount();
  171. for(vari=0;i<total-1;i++){
  172. vartemp=secondGridStore.getAt(i);
  173. if(temp.get('name')==record.get('name'))
  174. {
  175. break;
  176. }
  177. varnext=secondGridStore.getAt(i+1);
  178. if(next.get('name')==record.get('name'))
  179. {
  180. secondGridStore.remove(next);
  181. secondGridStore.insert(i,next);
  182. }
  183. }
  184. }
  185. }
  186. }),
  187. newExt.Button({
  188. text:"向下",
  189. style:'margin:15px0px0px10px',
  190. handler:function(){
  191. if(secondGrid.getSelectionModel().hasSelection())
  192. {
  193. varrecord=secondGrid.getSelectionModel().getSelected();
  194. //数据行数
  195. vartotal=secondGridStore.getCount();
  196. for(vari=0;i<total-1;i++){
  197. varnext=secondGridStore.getAt(i);
  198. if(next.get('name')==record.get('name'))
  199. {
  200. secondGridStore.remove(next);
  201. secondGridStore.insert(i+1,next);
  202. }
  203. }
  204. }
  205. }
  206. })
  207. ]
  208. };
  209. //Simple'borderlayout'paneltohousebothgrids
  210. vardisplayPanel=newExt.Panel({
  211. width:650,
  212. height:300,
  213. //layout:'border',
  214. layout:'column',
  215. renderTo:'panel',
  216. items:[
  217. firstGrid,
  218. lrPanel,
  219. secondGrid,
  220. udPanel
  221. ],
  222. bbar:[
  223. '->',//Fill
  224. {
  225. text:'确定',
  226. handler:function(){
  227. //数据行数
  228. vartotal=secondGridStore.getCount();
  229. varresults="";
  230. if(total>0)
  231. {
  232. for(vari=0;i<total;i++)
  233. {
  234. varrecord=secondGridStore.getAt(i);
  235. results+=record.get("name")+",";
  236. }
  237. results=results.substring(0,results.length-1);
  238. }
  239. alert("选择的列名称:"+results);
  240. }
  241. },
  242. {
  243. text:'重置',
  244. handler:function(){
  245. //refreshsourcegrid
  246. firstGridStore.loadData(myData);
  247. //purgedestinationgrid
  248. secondGridStore.removeAll();
  249. }
  250. }
  251. ]
  252. });
  253. //usedtoaddrecordstothedestinationstores
  254. varblankRecord=Ext.data.Record.create(fields);
  255. /****
  256. *SetupDropTargets
  257. ***/
  258. //Thiswillmakesureweonlydroptotheviewcontainer
  259. varfirstGridDropTargetEl=firstGrid.getView().el.dom.childNodes[0].childNodes[1];
  260. varfirstGridDropTarget=newExt.dd.DropTarget(firstGridDropTargetEl,{
  261. ddGroup:'firstGridDDGroup',
  262. copy:true,
  263. notifyDrop:function(ddSource,e,data){
  264. //Genericfunctiontoaddrecords.
  265. functionaddRow(record,index,allItems){
  266. //Searchforduplicates
  267. varfoundItem=firstGridStore.find('name',record.data.name);
  268. //ifnotfound
  269. if(foundItem==-1){
  270. firstGridStore.add(record);
  271. //Callasortdynamically
  272. //firstGridStore.sort('name','ASC');
  273. //RemoveRecordfromthesource
  274. ddSource.grid.store.remove(record);
  275. }
  276. }
  277. //Loopthroughtheselections
  278. Ext.each(ddSource.dragData.selections,addRow);
  279. return(true);
  280. }
  281. });
  282. //Thiswillmakesureweonlydroptotheviewcontainer
  283. varsecondGridDropTargetEl=secondGrid.getView().el.dom.childNodes[0].childNodes[1]
  284. vardestGridDropTarget=newExt.dd.DropTarget(secondGridDropTargetEl,{
  285. ddGroup:'secondGridDDGroup',
  286. copy:false,
  287. notifyDrop:function(ddSource,e,data){
  288. //Genericfunctiontoaddrecords.
  289. functionaddRow(record,index,allItems){
  290. //Searchforduplicates
  291. varfoundItem=secondGridStore.find('name',record.data.name);
  292. //ifnotfound
  293. if(foundItem==-1){
  294. secondGridStore.add(record);
  295. //Callasortdynamically
  296. //secondGridStore.sort('name','ASC');
  297. //RemoveRecordfromthesource
  298. ddSource.grid.store.remove(record);
  299. }
  300. }
  301. //Loopthroughtheselections
  302. Ext.each(ddSource.dragData.selections,addRow);
  303. return(true);
  304. }
  305. });
  306. });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值