当前位置 : 主页 > 网页制作 > Dojo >

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

来源:互联网 收集:自由互联 发布时间:2021-06-15
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
<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', 'width': '100px'},
      {'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,'click',
	    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.voidcn.com/article/p-pvahskxh-we.html dojo EnhancedGrid的两种实现方式对比

网友评论