2017-02-28 52 views
1

我使用:從jquery.dataTables.js:https://datatables.netjQuery的數據表拖放從一個表中刪除列到另一個

我試圖將它從一個表中刪除列到另一個。

編輯: 所以基本上我想要做的是:

  • 能夠將它從表2滴名字到表格叫列上方
  • 阻力後拖放同樣應該從表2

情況下2消失的名稱:如果我添加一個新行使用按鈕添加新行

  • 我需要能夠拖動下降,從表2中的名稱轉換爲列了。

所以基本上我想做一個拖放只是在不在行中的列。

我不想創建一個新行,只需將名稱從一個表移動到另一個表。

編輯2:

1 - 是否可以從表#2拖/放倍數值表#1? 不可以,只能拖放1個。 用戶點擊編輯或添加新行後,只需拖放即可。 所以我可以將名稱drom表2替換爲列名稱表1

2-如果否,則拖動的值應該替換掉它的值嗎?

3如果是,它應該如何工作?添加其他值爲空的新行? 不需要添加任何行,我們只需要替換列名。

如何將作品: 所以在點擊後編輯或添加新行,我將能夠從表2中 表拖到一個名稱列1.

幾個resquests: 如果選擇表2中的行,該行應該改變顏色,顯示被選中。並在表1列名稱中需要刪除的位置需要更改顏色以顯示用戶可以放棄的位置,用戶放棄顏色後應該恢復正常。

樣品在這裏工作: http://plnkr.co/edit/6sbmBzbXDzm4p6CjaVK0?p=preview

$(document).ready(function() { 
 
var dataUrl = 'http://www.json-generator.com/api/json/get/ccTtqmPbkO?indent=2'; 
 
var options = [ 
 
    { key : 'option 1', value : 1 }, 
 
    { key : 'option 2', value : 2 }, 
 
    { key : 'option 3', value : 3 } 
 
]; 
 

 
$(document).ready(function() { 
 
    var $table = $('#example'); 
 
    var dataTable = null; 
 

 
    $table.on('mousedown', 'td .fa.fa-minus-square', function(e) { 
 
    dataTable.row($(this).closest("tr")).remove().draw(); 
 
    }); 
 

 
    $table.on('mousedown.edit', 'i.fa.fa-pencil-square', function(e) { 
 
    enableRowEdit($(this)); 
 
    }); 
 

 
    $table.on('mousedown', 'input', function(e) { 
 
    e.stopPropagation(); 
 
    }); 
 

 
    $table.on('mousedown.save', 'i.fa.fa-envelope-o', function(e) { 
 
    updateRow($(this), true); // Pass save button to function. 
 
    }); 
 

 
    $table.on('mousedown', '.select-basic', function(e) { 
 
    e.stopPropagation(); 
 
    }); 
 

 
    dataTable = $table.DataTable({ 
 
    ajax: dataUrl, 
 
    rowReorder: { 
 
     dataSrc: 'order', 
 
     selector: 'tr' 
 
    }, 
 
    columns: [{ 
 
     data: 'order' 
 
    }, { 
 
     data: 'name' 
 
    }, { 
 
     data: 'place' 
 
    }, { 
 
     data: 'delete' 
 
    }] 
 
    }); 
 

 
    $table.css('border-bottom', 'none') 
 
     .after($('<div>').addClass('addRow') 
 
      .append($('<button>').attr('id', 'addRow').text('Add New Row'))); 
 

 
    // Add row 
 
    $('#addRow').click(function() { 
 
    var $row = $("#new-row-template").find('tr').clone(); 
 
    dataTable.row.add($row).draw(); 
 
    // Toggle edit mode upon creation. 
 
    enableRowEdit($table.find('tbody tr:last-child td i.fa.fa-pencil-square')); 
 
    }); 
 

 
    $('#btn-save').on('click', function() { 
 
    updateRows(true); // Update all edited rows 
 
    }); 
 

 
    $('#btn-cancel').on('click', function() { 
 
    updateRows(false); // Revert all edited rows 
 
    }); 
 

 
    function enableRowEdit($editButton) { 
 
    $editButton.removeClass().addClass("fa fa-envelope-o"); 
 
    var $row = $editButton.closest("tr").off("mousedown"); 
 

 
    $row.find("td").not(':first').not(':last').each(function(i, el) { 
 
     enableEditText($(this)) 
 
    }); 
 

 
    $row.find('td:first').each(function(i, el) { 
 
     enableEditSelect($(this)) 
 
    }); 
 
    } 
 
    
 
    function enableEditText($cell) { 
 
    var txt = $cell.text(); 
 
    $cell.empty().append($('<input>', { 
 
     type : 'text', 
 
     value : txt 
 
    }).data('original-text', txt)); 
 
    } 
 

 
    function enableEditSelect($cell) { 
 
    var txt = $cell.text(); 
 
    $cell.empty().append($('<select>', { 
 
     class : 'select-basic' 
 
    }).append(options.map(function(option) { 
 
     return $('<option>', { 
 
     text : option.key, 
 
     value : option.value 
 
     }) 
 
    })).data('original-value', txt)); 
 
} 
 

 
    function updateRows(commit) { 
 
    $table.find('tbody tr td i.fa.fa-envelope-o').each(function(index, button) { 
 
     updateRow($(button), commit); 
 
    }); 
 
    } 
 

 
    function updateRow($saveButton, commit) { 
 
    $saveButton.removeClass().addClass('fa fa-pencil-square'); 
 
    var $row = $saveButton.closest("tr"); 
 

 
    $row.find('td').not(':first').not(':last').each(function(i, el) { 
 
     var $input = $(this).find('input'); 
 
     $(this).text(commit ? $input.val() : $input.data('original-text')); 
 
    }); 
 

 
    $row.find('td:first').each(function(i, el) { 
 
     var $input = $(this).find('select'); 
 
     $(this).text(commit ? $input.val() : $input.data('original-value')); 
 
    }); 
 
    } 
 
}); 
 

 
$(document).ready(function() { 
 
    \t var url = 'http://www.json-generator.com/api/json/get/bXcKDeAbyq?indent=2'; 
 
     table = $('#example2').DataTable({ 
 
     ajax: url, 
 
     order: [[ 0, "desc" ]], 
 
     rowReorder: { 
 
      dataSrc: 'place', 
 
      selector: 'tr' 
 
     }, 
 
     columns: [ { 
 
      data: 'name' 
 
     }] 
 
     }); 
 

 
    }); 
 
});
div.addRow { 
 
     line-height: 45px; 
 
     background-color: #fff; 
 
     padding-left: 10px; 
 
     border-bottom: 1px solid; 
 
     border-top: 1px solid #e5e5e5; 
 
    }
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script> 
 
<script src="//cdn.rawgit.com/DataTables/RowReorder/ce6d240e/js/dataTables.rowReorder.js"></script> 
 
<link href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet" /> 
 
<link href="//cdn.datatables.net/rowreorder/1.2.0/css/rowReorder.dataTables.min.css" rel="stylesheet"/> 
 
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> 
 

 
<table id="example" class="display" width="100%" cellspacing="0"> 
 
    <thead> 
 
    <tr> 
 
     <th>order</th> 
 
     <th>name</th> 
 
     <th>country</th> 
 
     <th>action</th> 
 
    </tr> 
 
    </thead> 
 
</table> 
 

 
<table id="new-row-template" style="display:none"> 
 
    <tbody> 
 
    <tr> 
 
     <td>999</td> <!-- Use a large number or row might be inserted in the middle --> 
 
     <td>__NAME__</td> 
 
     <td>__COUNTRY__</td> 
 
     <td> 
 
     <i class="fa fa-pencil-square" aria-hidden="true"></i> 
 
     <i class="fa fa-minus-square" aria-hidden="true"></i> 
 
     </td> 
 
    </tr> 
 
    </tbody> 
 
</table> 
 
<br> 
 
<div class="pull-right"> 
 
    <button type="button" id="btn-cancel" class="btn btn-default" data-dismiss="modal">Cancel</button> 
 
    <button type="button" id="btn-save" class="btn btn-primary" data-dismiss="modal">Save</button> 
 
</div> 
 

 
<br> 
 
<br> 
 
<h1> 
 
table 2 
 
</h1><br> 
 
<br> 
 
<table id="example2" class="display" width="100%" cellspacing="0"> 
 
    <thead> 
 
    <tr> 
 
     <th> name</th> 
 
    </tr> 
 
    </thead> 
 
</table> 
 

 

 
<br> 
 
<br> 
 
<h1> 
 
table 2 
 
</h1><br> 
 
<br> 
 
<table id="example2" class="display" width="100%" cellspacing="0"> 
 
    <thead> 
 
    <tr> 
 
     <th> name</th> 
 
    </tr> 
 
    </thead> 
 
</table>

回答

3

我已經回答了這個問題在這裏:How to drag and drop a column into another

到您的代碼一些變化(全球MouseUp事件和第二表MouseDown事件):

var rowChache = []; 

    function mouseUp(event) { 
    var ctrl = $(document.elementsFromPoint(event.clientX, event.clientY)).filter('input.border-highlight'); 

    if (ctrl.length > 0 && rowCache.length > 0) { 
     var el = rowCache[0]; 
     var data = el.row.data(); 

     if (data.length > 0) { 
     ctrl.val(data[0].name); 
     el.row.remove().draw(); 
     } 
    } 

    rowCache = []; 
    $('#example tr td:nth-child(2) input').removeClass('border-highlight'); 
    } 

table.on('mousedown', 'tbody tr', function() { 
var $row = $(this); 

var r = table.rows(function(i, data) { 
    return data.name == $row.children().first().text(); 
}); 

if (r[0].length > 0) { 
    $row.parents('table').find('tr').removeClass('highlight'); 
    $row.addClass('highlight'); 
    $('#example tr td:nth-child(2) input').addClass('border-highlight'); 
} 

    rowCache.push({ 
    row: r 
    }); 
}); 

檢查還鏈接:http://jsfiddle.net/f7debwj2/47/

+0

你好,非常感謝你的工作完美,我可以請求2更多事情?1當我選擇列翅它的綠色完美,但是我們可以讓那些也能夠接收數據的專欄成爲現實?另一件事是你可以做一個樣本沒有presse編輯工作?但請不要刪除這一個我也會使用,在幾個小時內,我可以給你賞金謝謝,ispaciba :)。 – Raduken

+1

好的。下面是代碼的另一個版本,儘管它們的編輯狀態和目標列的不斷突出顯示,但仍有機會替換值:http://jsfiddle.net/jahn08/L3Lhw7jk/1/ –

+0

非常感謝您,能否幫助我在這一個? http://stackoverflow.com/questions/42653929/jquery-datatable-drag-and-drop-changing-bg-after-drop-a-new-name – Raduken

1

你甚至嘗試搜索?

https://datatables.net/forums/discussion/30197/add-remove-table-rows-on-drag-and-drop-between-two-datatables

move rows between two datatables

https://gist.github.com/davemo/706167

drag n drop between two tables

Drag/drop between two datatables

最重要的珍聞來自數據表的創建者:

這不是DataTables的一個特性,但是,它應該很有可能使用API​​。具體來說,我會建議使用row()。remove()和row.add()來根據需要刪除和添加行。然而,拖放代碼將在DataTables外部。

你要麼使用https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API或做一些JS,和/或寫缺少的功能集成到API,但鑑於上述鏈接,你會看到人們是如何解決你所描述的同樣的問題。而不是行,你會做專欄,我相信它可以被修改完成你想要的。

+0

對不起,這對我來說太難了,我是新手:(如果有人可以幫忙,我會給500分,謝謝 – Raduken

相關問題