Source: widgets/SpwDataTable.js

Retour à la documentation
/**
 * @class spw.widgets.SpwDataTable
 */
define(["dojo/_base/declare", "spw/api/SpwBaseWidget", "dojo/dom-construct", "dojo/_base/lang", "dojo/on",
        "dgrid/OnDemandGrid", "dgrid/util/mouse", "dgrid/Selection", "dgrid/extensions/ColumnResizer", "dgrid/extensions/ColumnHider",
        "dojo/_base/array", "dojo/store/Memory"],
        function(declare, SpwBaseWidget, domConstruct, lang, on, OnDemandGrid, mouseUtils, Selection, ColumnResizer, ColumnHider, 
        		array, Memory) {

    return declare("spw.widgets.SpwDataTable", [SpwBaseWidget], /** @lends spw.widgets.SpwDataTable.prototype */{
    	
    	columns: null,
    	data: null,
    	
    	_resultGrid: null,
    	
    	checkable: true,
    	deletable: true,
    	exportable: true,
    	
    	
        /**
         * @constructs
         * @param config
         */
    	constructor: function(){
    		this.listenWidgets = [];
    	},
    	
    	postCreate: function() {
    		this.inherited(arguments);
    	},
    	
    	gridRowEntered: function(){
    		
    	},
    	
    	gridRowLeaved: function(){
    		
    	},
    	
    	gridRowSelected: function(){
    		
    	},
    	
    	output: function(data, options){
    		this.clearOutput();
    		this.addOutput(data, options);
    	},
    	
    	addOutput: function(data, options) {
    		if(!this.activated){
    			this.onActivate();
    		}

			if(data && data.length > 0){
				this.data = data;
	    		if(options && !options.columns) {
	    			this.columns = {};
	    			if(this.checkable){
	    	    		this.columns._dataTableChecked = {
		    				label:"", 
		    				sortable: false,
		    				width: 40,
		    				renderCell: lang.hitch(this, function(object, value, node, options){
								var lnk = domConstruct.create("img", {src:this.imagesPath + "icon-delete.png", alt:"Supprimer", title:"Supprimer", style:"cursor:pointer;"}, domConstruct.create("div", {style:"text-align:center;"}, node));
								on(lnk, "click", lang.hitch(this, function(){
									this._resultGrid.get('store').remove(object.OBJECTID);
									this._resultGrid.refresh();
								}));
							}),
							renderHeaderCell: lang.hitch(this, function(node){
								var lnk = domConstruct.create("img", {src:this.imagesPath + "icon-delete.png", alt:"Supprimer", title:"Tout supprimer", style:"cursor:pointer;"}, domConstruct.create("div", {style:"text-align:center;"}, node));
								on(lnk, "click", lang.hitch(this, function(){
									this._resultGrid.get('store').setData([]);
									this._resultGrid.refresh();
								}));
							})
	    				};
	    			}
	    			if(this.deletable) {
	    	    		this.columns._dataTableDeleted = {
	    	    				label:"", 
			    				sortable: false,
	    	    				width: 40,
	    	    				renderCell: lang.hitch(this, function(object, value, node, options){
	    							var lnk = domConstruct.create("input", { type:"checkbox", title:"Sélectionner"  }, domConstruct.create("div", {style:"text-align:center;"}, node));
	    						}),
	    						renderHeaderCell: lang.hitch(this, function(node) {
	    							var lnk = domConstruct.create("input", { type:"checkbox", title:"Tout sélectionner" }, domConstruct.create("div", {style:"text-align:center;"}, node));
	    						})
	        				};
	    			}

		        	for(var k in data[0]){
		        		this.columns[k] = {label:k};
		        	}
	    		} else if (options){
	    			lang.mixin(this.columns, options.columns);
	    		}
	    		this.applyOptions(data);

		        this._resultGrid = new (declare([OnDemandGrid, Selection, ColumnHider, ColumnResizer]))({
					className: "dgrid-autoheight",
					addUiClasses: false,
		        	selectionMode: "single",
		        	columns: this.columns
	        	}, domConstruct.create("div", {}, this.domNode));
		        
		        this._resultGrid.set('store', new Memory({idProperty:"OBJECTID", data:this.data}));
		        
		        this.evtHandlers.push(this._resultGrid.on(mouseUtils.enterRow, options.gridRowEntered ? options.gridRowEntered : lang.hitch(this, this.gridRowEntered)));
		        this.evtHandlers.push(this._resultGrid.on(mouseUtils.leaveRow, options.gridRowLeaved ? options.gridRowLeaved : lang.hitch(this, this.gridRowLeaved)));
		        this.evtHandlers.push(this._resultGrid.on("dgrid-select", options.gridRowSelected ? options.gridRowSelected : lang.hitch(this, this.gridRowSelected)));
	        }
			
    	},
    	
    	applyOptions: function(data){
    		array.map(data, lang.hitch(this, function(item){
    			if(this.checkable){
    				item._dataTableChecked = false;
    			}
    			if(this.deletable) {
    				item._dataTableDeleted = false;
    			}
    		}));
    	},
    	
    	clearOutput: function() {
			if(this._resultGrid) {
//				this._resultGrid.set('store', new Memory({data:[]}));
				array.forEach(this.evtHandlers, function(handler){ handler.remove(); });
				this._resultGrid.destroy();
			}
    	}
		
    });
});