Setup a one way drag and drop from a Grid to a Form.
A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.
autoheight property – Sets or gets the height of the grid to be equal to the summary height of the grid rows.width property – Sets or gets the Grid's width.columns property – Sets the Grid columns.pageable property – Enables or disables the Grid Paging feature.rendered property – Callback function which is called when the jqxGrid's render function is called either internally or not.sortable property – The sortable property enables or disables the sorting feature.source property – Sets and gets the source.rowclick event – This event is triggered when a row is clicked.focus method – Sets the keyboard Focus to the jqxGrid widget.getrowdata method – Gets the data of a row.getcellatposition method – Gets a cell at specific position.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxGrid shows the setup step by step.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxgrid.js, jqxgrid.selection.js, jqxgrid.sort.js, jqxgrid.filter.js, jqxgrid.grouping.js, jqxgrid.columnsresize.js, jqxgrid.edit.js, jqxgrid.columnsreorder.js, jqxgrid.pager.js, jqxexpander.js, jqxlistbox.js, jqxdropdownlist.js, jqxdragdrop.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), generatedata.js (generatedata(), random sample rows).
$(document).ready(function () {
var source =
{
localdata: generatedata(10),
datafields:
[
{ name: 'firstname', type: 'string' },
{ name: 'lastname', type: 'string' },
{ name: 'productname', type: 'string' }
],
datatype: "array"
};
var dataAdapter = new $.jqx.dataAdapter(source);
var columns = [
{ text: 'First Name', dataField: 'firstname', width: 100 },
{ text: 'Last Name', dataField: 'lastname', width: 100 },
{ text: 'Product', dataField: 'productname' }
];
$("#grid").on('rowclick', function () {
// put the focus back to the Grid. Otherwise, the focus goes to the drag feedback element.
$("#grid").jqxGrid('focus');
});
// create data grids.
$("#grid").jqxGrid(
{
width: 600,
source: dataAdapter,
autoheight: true,
pageable: true,
sortable: true,
columns: columns,
rendered: function () {
// select all grid cells.
var gridCells = $('#grid').find('.jqx-grid-cell');
if ($('#grid').jqxGrid('groups').length > 0) {
gridCells = $('#grid').find('.jqx-grid-group-cell');
}
// initialize the jqxDragDrop plug-in. Set its drop target to the second Grid.
gridCells.jqxDragDrop({
appendTo: 'body', dragZIndex: 99999,
dropAction: 'none',
initFeedback: function (feedback) {
feedback.height(70);
feedback.width(220);
}
});
// initialize the dragged object.
gridCells.off('dragStart');
gridCells.on('dragStart', function (event) {
var value = $(this).text();
var position = $.jqx.position(event.args);
var cell = $("#grid").jqxGrid('getcellatposition', position.left, position.top);
$(this).jqxDragDrop('data', $("#grid").jqxGrid('getrowdata', cell.row));
var groupslength = $('#grid').jqxGrid('groups').length;
// update feedback's display value.
var feedback = $(this).jqxDragDrop('feedback');
var feedbackContent = $(this).parent().clone();
var table = '<table>';
$.each(feedbackContent.children(), function (index) {
if (index < groupslength)
return true;
table += '<tr>';
table += '<td>';
table += columns[index - groupslength].text + ': ';
table += '</td>';
table += '<td>';
table += $(this).text();
table += '</td>';
table += '</tr>';
});
table += '</table>';
feedback.html(table);
});
gridCells.off('dragEnd');
gridCells.on('dragEnd', function (event) {
var value = $(this).jqxDragDrop('data');
var position = $.jqx.position(event.args);
var pageX = position.left;
var pageY = position.top;
var $form = $("#form");
var targetX = $form.offset().left;
var targetY = $form.offset().top;
var width = $form.width();
var height = $form.height();
// fill the form if the user dropped the dragged item over it.
if (pageX >= targetX && pageX <= targetX + width) {
if (pageY >= targetY && pageY <= targetY + height) {
$("#firstName").val(value.firstname);
$("#lastName").val(value.lastname);
$("#product").val(value.productname);
}
}
});
}
});
$("#form").jqxExpander({ width: 250, toggleMode: 'none', showArrow: false });
});
<div id="jqxWidget">
<div id="grid">
</div>
<div style="margin-top: 20px;" id="form">
<div>
Form Panel
</div>
<div>
<form>
<table>
<tbody><tr>
<td>
First Name:
</td>
<td>
<input id="firstName">
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input id="lastName">
</td>
</tr>
<tr>
<td>
Product:
</td>
<td>
<input id="product">
</td>
</tr>
</tbody></table>
</form>
</div>
</div>
</div>