jQuery copy cloned rows from one table to another with added td columns -
using jquery, need copy, rows, 1 @ at time, dragging table1 table2, leaving row unchanged in table1. on dropping in table2, need add 2 new columns row dropped in table2. rows need drag/dropped in table2 rearrange them. ok adds new data column table2 sending server. following code works. problem if drop occurs before mouse on table2, columns added table1. code follows:
<!doctype html> <html lang="en"> <head> <meta charset='utf-8'> <!-- <link rel='stylesheet' href='style/jquery-ui.css'> --> <script src='js/jquery-1.11.0.min.js'></script> <script src='js/jquery-ui.min.js'></script> <style type="text/css"> .tables_ui { display:inline-block; margin:2px 2%; border:2px solid #3333fe; border-spacing:0; } .tables_ui ul li { min-width: 200px; } .dragging li.ui-state-hover { min-width: 240px; } .dragging .ui-state-hover { color:green !important; font-weight: bold; } .tables_ui th,td { text-align: left; padding: 2px 4px; border: 1px solid; } .connectedsortable tr, .ui-sortable-helper { cursor: move; } .connectedsortable tr:first-child { cursor: default; } .ui-sortable-placeholder { background: yellow; } .ui-layout-center { width: 400px; height: 300px; border: 1px solid black; } .ui-state-hover { background-color: #f9ffff; } </style> <script> function dorows() { $('#sortable2').find('tr').each(function(index){ if (index > 0) { $('#sortable2 tr').eq(index).find('td').eq(5).remove(); //remove data column if exists var valid = $('#sortable2 tr').eq(index).find('td').eq(2).html(); var valnext = $('#sortable2 tr').eq(index).find('input').val(); $(this).append("<td><input style='cursor: default;' name='namedata' value='" + index + '~' + valid + '~' + valnext + "' /></td>"); } }); } $(document).ready(function() { $("#sortable1").sortable({ connectwith: ".connectedsortable", helper: function (e, tr) { this.copyhelper = tr.clone().insertafter(tr); $(this).data('copied', false); $(this).find('tr').each(function(index){ }); tr.append("<td><input style='cursor: default;' value='0' /></td>"); tr.append("<td><a style='cursor: default;' onclick='deleterow($(this));'>delete</a></td>"); return tr.clone(); }, stop: function () { var copied = $(this).data('copied'); if (copied) { $('#initial').remove(); //remove initial row } if (!copied) { this.copyhelper.remove(); } this.copyhelper = null; } }); $("#sortable2").sortable({ receive: function (e, ui) { ui.sender.data('copied', true); } }); //to add order number of rows text $( "#sortable2" ).sortable({ //only done when table2 reearranged stop: function( event, ui ){ $(this).find('tr').each(function(index){ }); } }); $('form').on('submit', function (e) { e.preventdefault(); }); }); function deleterow(row) { row.closest('tr').remove(); } </script> </head> <body> <table class="tables_ui"> <caption><h4>existing names</h4></caption> <tbody id="sortable1" class="connectedsortable"> <tr> <th>number</th> <th>name</th> <th>id</th> </tr> <tr> <td>s03</td> <td>name3</td> <td>id4</td> </tr> <tr> <td>s01</td> <td>name1</td> <td>id2</td> </tr> <tr> <td>s02</td> <td>name2</td> <td>id6</td> </tr> </tbody></table> <table class="tables_ui"> <caption><h4>when names arranged in order create sequence, enter time previous name in minutes. click ok.</h4></caption> <tbody id="sortable2" class="connectedsortable"> <tr> <th>number</th> <th>name</th> <th>id</th> <th>time in minutes previous name</th> <th>delete</th> </tr> <tr id='initial'> <td colspan='5' style='width: 300px;'>drop , rearrange names here</td> </tr> </tbody> </table> <form> <input type='submit' value='ok' onclick='dorows();' /> </form> </body> </html>
Comments
Post a Comment