dojo小例子(5)EnhancedGrid+JsonRest,实现分页、添加、删除

前端之家收集整理的这篇文章主要介绍了dojo小例子(5)EnhancedGrid+JsonRest,实现分页、添加、删除前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<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提供数据,增加数据时,
store.save({onComplete: saveDone,onError: saveFailed});
数据增加成功,却调用onError方法错误信息:TypeError: invalid 'in' operand object
而去掉Cache、Memory,就不会报错了

后台分页处理请看这里:http://www.jb51.cc/article/p-pvahskxh-we.htmldojo EnhancedGrid的两种实现方式对比

原文链接:https://www.f2er.com/dojo/291227.html

猜你在找的Dojo相关文章