I know there are no. of questions related to this on stackoverflow one of them as How to make dynamically added list elements draggable in jQuery?
But I am trying something different. I am adding my list elements at the time of page loads. As like below:
<ul id="CreateCustomerListUL">
<li class="custList" draggable="true" data-id="200001" data-pnumber="1234567890" data-accountname="Nidhi Sales"><strong>Nidhi Sales</strong> <p>1234567890</p></li>
<li class="custList" draggable="true" data-id="200002" data-pnumber="1234567890" data-accountname="Dhanashree Traders"><strong>Dhanashree Traders</strong> <p>1234567890</p></li>
<li class="custList" draggable="true" data-id="200003" data-pnumber="1234567890" data-accountname="Amrut Traders" style="display: list-item;"><strong>Amrut Traders</strong> <p>1234567890</p></li>
</ul>
Once list page loaded. This each item is dragged and dropped into a dataTable. And list increases as the drag and dropped items from customer List. The following code is automatically generated after drag and drop.
<tbody id="CustListTableBody">
<tr role="row" class="odd"><td class="sorting_1">1</td><td class="">Dhanashree Traders</td><td>1234567890</td><td><span class="glyphicon glyphicon-remove deleteRow" data-name="Dhanashree Traders" data-number="1234567890" data-id="200002"></span></td></tr>
<tr role="row" class="even"><td class="sorting_1">2</td><td class="">Nidhi Sales</td><td>1234567890</td><td><span class="glyphicon glyphicon-remove deleteRow" data-name="Nidhi Sales" data-number="1234567890" data-id="200001" ></span></td></tr>
<tr role="row" class="odd"><td class="sorting_1">3</td><td class="">Amrut Traders</td><td>1234567890</td><td><span class="glyphicon glyphicon-remove deleteRow" data-name="Amrut Traders" data-number="1234567890" data-id="200003" ></span></td></tr>
</tbody>
The datatable will be look like this:
The droppable code is like this:
$('#CustListTable').droppable({
onDrop:function(e,source){
var t = $('#CustListTable').DataTable();
var rowCount = t.rows().data().length +1;
var custName = $(source).attr('data-accountname');
var custNumber = $(source).attr('data-pnumber');
var custId = $(source).attr('data-id');
var deleteRow = '<span class="glyphicon glyphicon-remove deleteRow" data-name="'+custName+'" data-number="'+custNumber+'" data-id="'+custId+'"></span>';
t.row.add( [ rowCount, custName, custNumber, deleteRow]).draw();
$(source).remove();
}
});
` I am deleting the row after clicking on cross symbol in third column. Then deleting the row of datatable is :
$('#CustListTable').on('click','.deleteRow',function() {
var t = $('#CustListTable').dataTable();
var name=$(this).attr('data-name');
var pNumber = $(this).attr('data-number');
var id = $(this).attr('data-id');
var item = '<li class="custList" draggable="true" data-id="'+id+'"data-pnumber="'+pNumber+'" data-accountname="'+name+'"><strong>'+name+'</strong> <p>'+pNumber+'</p></li>';
var $element = $('<li class="customerList" draggable="true" data-id="'+id+'"data-pnumber="+pNumber+" data-accountname="'+name+'"><strong>'+name+'</strong> <p>'+pNumber+'</p></li>');
$element.draggable().resizable();
$('#CreateCustomerListUL').append($element);
var row = $(this).closest("tr").get(0);
t.fnDeleteRow(t.fnGetPosition(row));
});
Up to this all is working fine and if I am dropping the element that was last time deleted and appended to list is also dropping in the datatable successfully.But problem is that, when I am dragging the same element and if dropping failed then it is not reverting (means not taking its original position). It is staying where it was dragged. Automatically inline style tag is generated when the dragging started and that css affecting it from reveting. I am also unable to delete that inline generated code. I also tried this link and other stackoverflow links. But I am not getting the output. Specially affecting is style="position:absolute;"
if I removes this position value blank by inspect element on browser then it is reverting to its original position.
I tried the following code for reverting the drag.
$('#CreateCustomerListUL li').draggable(
{revert : function(event, ui) {
alert('Hello');
$(this).data("uiDraggable").originalPosition = { top : 0, left : 0 };
}});