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', '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://blog.csdn.net/earthhour/article/details/17203515 dojo EnhancedGrid的两种实现方式对比

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值