微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

dojo小例子4DataGrid对动态数据的增删

<script>
 require(['dojo/_base/array','dojo/_base/event','dojo/on','dojox/grid/DataGrid','dojo/store/JsonRest','dojo/store/Memory','dojo/store/Cache','dojo/data/ObjectStore','dijit/form/Button','dojo/parser','dojo/domready!'],function(array,event,on,DataGrid,JsonRest,Memory,Cache,ObjectStore,Button,parser){
    parser.parse();
    
    var url = "http://"+document.location.host;
		
    json = new Cache(
    	    new JsonRest({
		        target: url + "/dojo/rest/users?id=" // rest url
			}),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 DataGrid({
        id: 'grid',store: store,height: '200px',structure: layout,rowSelector: '20px'});

    /*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.rowCount;
		     }
	        /* 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);
	                }
	            });
	        }
	        event.stop(e);
	        store.save({onComplete: saveDone,onError: saveFailed}); // 触发向后台提交动作
	    }
    );

    /*Call startup() to render the grid*/
    grid.startup();
    
    function saveDone(){
    	console.log("Done saving.");
    }
    function saveFailed(err){
    	console.log("Save Failed.");
    	console.log(err);
    }
});
</script>
<body class="claro">
    <p>
    This example shows,how to add/remove rows
</p>
<div id='gridDiv'></div>

<p>
  <button data-dojo-id='button2' id='button2'>
      Add Row
  </button>

  <button data-dojo-id='button1' id='button1'>
      Remove Selected Rows
  </button>
</p>
</body>

原文地址:https://www.jb51.cc/dojo/291228.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐