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

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

来源:互联网 收集:自由互联 发布时间:2021-06-15
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!'], functi
<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', 'width': '100px'},
      {'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,'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);
	                }
	            });
	        }
	        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>
网友评论