dojo小例子(5)EnhancedGrid+JsonRest,实现分页、添加、删除
发布时间:2020-12-16 21:29:22 所属栏目:百科 来源:网络整理
导读:script require(['dojo/_base/array','dojo/_base/event','dojo/on','dojox/grid/EnhancedGrid','dojox/grid/enhanced/plugins/IndirectSelection','dojox/grid/enhanced/plugins/Pagination','dojo/store/JsonRest','dojo/store/Memory','dojo/store/Cache'
<script> require(['dojo/_base/array','dojo/_base/event','dojo/on','dojox/grid/EnhancedGrid','dojox/grid/enhanced/plugins/IndirectSelection','dojox/grid/enhanced/plugins/Pagination','dojo/store/JsonRest','dojo/store/Memory','dojo/store/Cache','dojo/data/ObjectStore','dijit/form/Button','dojo/parser','dojo/domReady!'],function(array,event,on,EnhancedGrid,IndirectSelection,Pagination,JsonRest,Memory,Cache,ObjectStore,Button,parser){ parser.parse(); var url = "http://"+document.location.host; json = //new Cache( // 如果不注释,会报错,后面详说 new JsonRest({ target: "/dojo/rest/getUsers?idx=" })/*,new Memory() // 如果不注释,会报错,后面详说 ) */; store = new ObjectStore({objectStore: json}); /*set up layout*/ var layout = [[ {'name': 'Column 1','field': 'id','width': '100px'},{'name': 'Column 2','field': 'name',{'name': 'Column 3','field': 'desc','width': '200px'} ]]; /*create a new grid*/ grid = new EnhancedGrid({ id: 'grid',store: store,structure: layout,//autoWidth:true,autoHeight:true,rowSelector: '20px',plugins:{ indirectSelection: {headerSelector:true,width:'40px',styles:'text-align: center;'},pagination: true },noDataMessage : "本单位当前无用户信息!" }); /*append the new grid to the div*/ grid.placeAt("gridDiv"); /* attach an event handler */ var i = 0; on(button2,'click',function(e){ if( i == 0 ){ i = grid.getTotalRowCount(); } /* set the properties for the new item: */ var myNewItem = {id: ++i,name: "Mediate",desc: 'Newly added values'}; /* Insert the new item into the store:*/ store.newItem(myNewItem); store.save({onComplete: saveDone,onError: saveFailed}); // } ); /* attach an event handler */ on(button1,function(e){ /* Get all selected items from the Grid: */ var items = grid.selection.getSelected(); if(items.length){ array.forEach(items,function(selectedItem){ if(selectedItem !== null){ /* Delete the item from the data store: */ store.deleteItem(selectedItem); } }); store.save({onComplete: saveDone,onError: saveFailed }); // } event.stop(e); } ); /*Call startup() to render the grid*/ grid.startup(); function saveDone(){ console.log("Done saving."); } function saveFailed(err){ console.log("Save failed."); console.log(err); } 上面为什么注释掉cache、memory呢? 因为JsonRest结合Cache、Memory为EnhancedGrid提供数据,增加数据时, 后台分页处理请看这里:http://www.52php.cn/article/p-pvahskxh-we.htmldojo EnhancedGrid的两种实现方式对比 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |