tonytomov/jqGrid — Issue #206: Use bdiv of jqgrid as drop target for gridDnD to work around hack for empty tables
Author: exell-christopher
Created: 2011-12-02 17:23:58+00:00
State: open

I'm proposing a change to the gridDnD functionality to use the parent ".ui-jqgrid-bdiv" as the drop target so that the entire table body is droppable instead of just the set of rows in the table.

The patch from the main 4.1.2 src file is below:

*** 10753,10766 ****
$($t).jqGrid('collapseSubGridRow',subgid);
} catch (e) {}
}
-	// hack
-	// drag and drop does not insert tr in table, when the table has no rows
-	// we try to insert new empty row on the target(s)
-	for (var i=0;i<$.data($t,"dnd").connectWith.length;i++){
-	if($($.data($t,"dnd").connectWith[i]).jqGrid('getGridParam','reccount') == "0" ){
-	$($.data($t,"dnd").connectWith[i]).jqGrid('addRowData','jqg_empty_row',{});
-	}
-	}
ui.helper.addClass("ui-state-highlight");
$("td",ui.helper).each(function(i) {
this.style.width = $t.grid.headers[i].width+"px";
--- 10753,10758 ----
***************
*** 10772,10781 ****
var ids = $(ui.helper).attr("id");
$($t).jqGrid('delRowData',ids );
}
-	// if we have a empty row inserted from start event try to delete it
-	for (var i=0;i<$.data($t,"dnd").connectWith.length;i++){
-	$($.data($t,"dnd").connectWith[i]).jqGrid('delRowData','jqg_empty_row');
-	}
if(opts.onstop && $.isFunction(opts.onstop) ) { opts.onstop.call($($t),ev,ui); }
}
},opts.drag_opts || {});
--- 10764,10769 ----
***************
*** 10787,10793 ****
var tid = $(d).closest("table.ui-jqgrid-btable");
if(tid.length > 0 && $.data(tid[0],"dnd") !== undefined) {
var cn = $.data(tid[0],"dnd").connectWith;
!	return $.inArray('#'+this.id,cn) != -1 ? true : false;
}
return d;
},
--- 10775,10783 ----
var tid = $(d).closest("table.ui-jqgrid-btable");
if(tid.length > 0 && $.data(tid[0],"dnd") !== undefined) {
var cn = $.data(tid[0],"dnd").connectWith;
! var drop_table = $(this).find('table');
! var drop_table_id = drop_table.attr('id')
!	return $.inArray('#'+drop_table_id,cn) != -1 ? true : false;
}
return d;
},
***************
*** 10795,10803 ****
if (!$(ui.draggable).hasClass('jqgrow')) { return; }
var accept = $(ui.draggable).attr("id");
var getdata = ui.draggable.parent().parent().jqGrid('getRowData',accept);
if(!opts.dropbyname) {
var j =0, tmpdata = {}, dropname;
!	var dropmodel = $("#"+this.id).jqGrid('getGridParam','colModel');
try {
for (var key in getdata) {
if(getdata.hasOwnProperty(key) && dropmodel[j]) {
--- 10785,10795 ----
if (!$(ui.draggable).hasClass('jqgrow')) { return; }
var accept = $(ui.draggable).attr("id");
var getdata = ui.draggable.parent().parent().jqGrid('getRowData',accept);
+ var drop_table = $(this).find('table');
+ var drop_table_id = drop_table.attr('id')
if(!opts.dropbyname) {
var j =0, tmpdata = {}, dropname;
!	var dropmodel = $("#"+drop_table_id).jqGrid('getGridParam','colModel');
try {
for (var key in getdata) {
if(getdata.hasOwnProperty(key) && dropmodel[j]) {
***************
*** 10813,10835 ****
if(opts.beforedrop && $.isFunction(opts.beforedrop) ) {
//parameters to this callback - event, element, data to be inserted, sender, reciever
// should return object which will be inserted into the reciever
!	var datatoinsert = opts.beforedrop.call(this,ev,ui,getdata,$('#'+$t.id),$(this));
if (typeof datatoinsert != "undefined" && datatoinsert !== null && typeof datatoinsert == "object") { getdata = datatoinsert; }
}
if(ui.helper.dropped) {
var grid;
if(opts.autoid) {
if($.isFunction(opts.autoid)) {
!	grid = opts.autoid.call(this,getdata);
} else {
grid = Math.ceil(Math.random()*1000);
grid = opts.autoidprefix+grid;
}
}
// NULL is interpreted as undefined while null as object
!	$("#"+this.id).jqGrid('addRowData',grid,getdata,opts.droppos);
}
!	if(opts.ondrop && $.isFunction(opts.ondrop) ) { opts.ondrop.call(this,ev,ui, getdata); }
}}, opts.drop_opts || {});
},
"onstart" : null,
--- 10805,10827 ----
if(opts.beforedrop && $.isFunction(opts.beforedrop) ) {
//parameters to this callback - event, element, data to be inserted, sender, reciever
// should return object which will be inserted into the reciever
!	var datatoinsert = opts.beforedrop.call(drop_table.get(),ev,ui,getdata,$('#'+$t.id),drop_table);
if (typeof datatoinsert != "undefined" && datatoinsert !== null && typeof datatoinsert == "object") { getdata = datatoinsert; }
}
if(ui.helper.dropped) {
var grid;
if(opts.autoid) {
if($.isFunction(opts.autoid)) {
!	grid = opts.autoid.call(drop_table.get(),getdata);
} else {
grid = Math.ceil(Math.random()*1000);
grid = opts.autoidprefix+grid;
}
}
// NULL is interpreted as undefined while null as object
!	$("#"+drop_table_id).jqGrid('addRowData',grid,getdata,opts.droppos);
}
!	if(opts.ondrop && $.isFunction(opts.ondrop) ) { opts.ondrop.call(drop_table.get(),ev,ui, getdata); }
}}, opts.drop_opts || {});
},
"onstart" : null,
***************
*** 10864,10870 ****
$t.p.jqgdnd = true;
for (var i=0;i