Commits

migajek committed 4662d23 Draft

added handling of custom buttons for add,edit,delete,refresh. undocumented yet.

Comments (0)

Files changed (1)

tastypie_jqgrid/static/mgrid/mgrid.js

                         {                        	
                         	$elem.jqGrid('filterToolbar',options.mGrid.filter_toolbar);
                     	}                    	
-						                         	
-                        if (options.pager && options.mGrid.pager_options)
-                    		$elem.mGrid2('_init_form_editing', options);
-                    	
-                    	$elem.mGrid2('_init_editing');	
-                        //$elem.jqGrid('setGridHeight', $("#grid_container").height() - ($("#gbox_grid").height() - $('#gbox_grid .ui-jqgrid-bdiv').height()));
-                        //$elem.jqGrid('setFrozenColumns');
-                        //$elem.mGrid2('_init_grid_tabs');
+
+
+						$elem.mGrid2('_init_editing');
+												                         	
+                        //if (options.pager && options.mGrid.pager_options)
+                        // init it anyway, as it might be triggered manually
+                		$elem.mGrid2('_init_form_editing', options);
+                		
+                		$elem.mGrid2('_init_custom_pager_buttons')
+
         },
         
         // initialize common editing options
         // initialize form editing with appropriate communication settings 
         _init_form_editing: function(options)
         {
-    		var clicksubmit_func = function(params, postdata) {
-		    		params.url = $(this).jqGrid('getRowData', postdata).resource_uri;
-	  		};
-        	var delOptions = {	    			    	
-		    	onclickSubmit: clicksubmit_func 
-			};
-			var addOptions = {
-				mtype: "POST", 
-				
-			};
-			var editOptions = {
-				mtype: "PATCH",
-				onclickSubmit: function(params, postdata) {
-					var row_id = postdata[$(this).attr('id')+'_id'];
-					params.url = $(this).jqGrid('getRowData', row_id).resource_uri;
-				},
-				beforeShowForm: function(form){
-					$('[data-mgrid-clear-onshow]', form).removeAttr('value').removeClass('FormElement');
-					setTimeout(function(){$('[data-mgrid-formelement]', form).removeClass('FormElement');}, 200);
-				},
-				viewPagerButtons: false
-			}
-			$(this).jqGrid('navGrid', options.pager, options.mGrid.pager_options, editOptions, addOptions, delOptions);
+        	// these edit options are initialized once only, as mGrid2 module is loaded.
+			$(this).jqGrid('navGrid', options.pager, options.mGrid.pager_options, $.mgrid._editOptions, $.mgrid._addOptions, $.mgrid._delOptions);
         },
         
+        // initialize custom buttons with data-mgrid* attributes
+        _init_custom_pager_buttons: function()
+        {
+        	var $this = $(this),
+        		$buttons = $('a[data-mgrid-selector=#'+$this.attr('id')+']');
+        		
+        		$buttons.click(function(e){
+        			e.preventDefault();
+        			var $this = $(this),
+        				oper = $this.attr('data-mgrid-oper'),
+        				$grid = $($this.attr('data-mgrid-selector'));
+        				
+        				if (oper === 'add')
+        					$grid.jqGrid("editGridRow","new", $.mgrid._addOptions);
+    					else if (oper === 'edit' || oper === 'delete')
+    					{
+    						var selrow = $grid.mGrid2('get_selected_row_id');
+    						if (!selrow)
+    						{
+    							$.jgrid.viewModal("#alertmod",{gbox:"#gbox_"+$.jgrid.jqID($grid.attr('id')),jqm:true});
+								$("#jqg_alrt").focus();
+    						} else {
+    							if (oper === 'edit')
+    								$grid.jqGrid("editGridRow", selrow, $.mgrid._editOptions);
+								else if (oper === 'delete')
+									$grid.jqGrid("delGridRow", selrow, $.mgrid._delOptions);
+							}
+    					} else if (oper === 'refresh')
+    					{
+    						$grid.trigger("reloadGrid")
+    					}    					
+        			return false;
+        		})
+        	
+        },        
         _init_grid_tabs: function()
         {
                 // handle "All" tab click
         }    
     };
 
+	$.mgrid = {};
+	$.mgrid._delOptions = {
+		    	onclickSubmit: function(params, postdata) {
+		    		params.url = $(this).jqGrid('getRowData', postdata).resource_uri;
+	  			} 
+	};
+	$.mgrid._addOptions = {
+		mtype: "POST", 
+		
+	};
+	$.mgrid._editOptions = {
+		mtype: "PATCH",
+		onclickSubmit: function(params, postdata) {
+			var row_id = postdata[$(this).attr('id')+'_id'];
+			params.url = $(this).jqGrid('getRowData', row_id).resource_uri;
+		},
+		beforeShowForm: function(form){
+			$('[data-mgrid-clear-onshow]', form).removeAttr('value').removeClass('FormElement');
+			setTimeout(function(){$('[data-mgrid-formelement]', form).removeClass('FormElement');}, 200);
+		},
+		viewPagerButtons: false
+	}