"Dynamic Sortable Tables"
Bootstrap 3.2.0 Snippet by matiaschap

<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> <div class="container"> <div class="row clearfix"> <div class="col-md-12 table-responsive"> <table class="table table-bordered table-hover table-sortable" id="tab_logic"> <thead> <tr > <th class="text-center"> Notes </th> <th class="text-center" style="border-top: 1px solid #ffffff; border-right: 1px solid #ffffff;"> </th> </tr> </thead> <tbody> <tr id='addr0' data-id="0" class="hidden"> <td data-name="desc"> <select name="sel0"> <option value"">Select Option</option> <option value"1">Option 1</option> <option value"2">Option 2</option> <option value"3">Option 3</option> </select> <a id="add_row" class="btn btn-default">Add Row</a> </td> <td data-name="del"> <button nam"del0" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button> </td> </tr> </tbody> </table> </div> </div> <a id="add_row" class="btn btn-default pull-right">Add Row</a> </div>
.table-sortable tbody tr { cursor: move; } /* Text fields */ div.input-group-option:last-child span.input-group-addon-remove{ display: none; } div.input-group-option:last-child input.form-control{ border-bottom-right-radius: 3px; border-top-right-radius: 3px; } div.input-group-option span.input-group-addon-remove{ cursor: pointer; } div.input-group-option{ margin-bottom: 3px; } /* Selects */ div.input-group-multiple-select:last-child span.input-group-addon-remove{ display: none; } div.input-group-multiple-select:last-child input.form-control{ border-bottom-right-radius: 3px; border-top-right-radius: 3px; } div.input-group-multiple-select span.input-group-addon-remove{ cursor: pointer; background: none; border: none; } div.input-group-multiple-select{ margin-bottom: 5px; }
$(document).ready(function() { $("#add_row").on("click", function() { // Dynamic Rows Code // Get max row id and set new id var newid = 0; $.each($("#tab_logic tr"), function() { if (parseInt($(this).data("id")) > newid) { newid = parseInt($(this).data("id")); } }); newid++; var tr = $("<tr></tr>", { id: "addr"+newid, "data-id": newid }); // loop through each td and create new elements with name of newid $.each($("#tab_logic tbody tr:nth(0) td"), function() { var cur_td = $(this); var children = cur_td.children(); // add new td and element if it has a nane if ($(this).data("name") != undefined) { var td = $("<td></td>", { "data-name": $(cur_td).data("name") }); var c = $(cur_td).find($(children[0]).prop('tagName')).clone().val(""); c.attr("name", $(cur_td).data("name") + newid); c.appendTo($(td)); td.appendTo($(tr)); } else { var td = $("<td></td>", { 'text': $('#tab_logic tr').length }).appendTo($(tr)); } }); // add delete button and td /* $("<td></td>").append( $("<button class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>") .click(function() { $(this).closest("tr").remove(); }) ).appendTo($(tr)); */ // add the new row $(tr).appendTo($('#tab_logic')); $(tr).find("td button.row-remove").on("click", function() { $(this).closest("tr").remove(); }); }); // Sortable Code var fixHelperModified = function(e, tr) { var $originals = tr.children(); var $helper = tr.clone(); $helper.children().each(function(index) { $(this).width($originals.eq(index).width()) }); return $helper; }; $(".table-sortable tbody").sortable({ helper: fixHelperModified }).disableSelection(); $(".table-sortable thead").disableSelection(); $("#add_row").trigger("click"); }); /* Selects */ $(function(){ var values = new Array(); $(document).on('change', '.form-group-multiple-selects .input-group-multiple-select:last-child select', function(){ var selectsLength = $('.form-group-multiple-selects .input-group-multiple-select select').length; var optionsLength = ($(this).find('option').length)-1; if(selectsLength < optionsLength){ var sInputGroupHtml = $(this).parent().html(); var sInputGroupClasses = $(this).parent().attr('class'); $(this).parent().parent().append('<div class="'+sInputGroupClasses+'">'+sInputGroupHtml+'</div>'); } updateValues(); }); $(document).on('change', '.form-group-multiple-selects .input-group-multiple-select:not(:last-child) select', function(){ updateValues(); }); $(document).on('click', '.input-group-addon-remove', function(){ $(this).parent().remove(); updateValues(); }); function updateValues() { values = new Array(); $('.form-group-multiple-selects .input-group-multiple-select select').each(function(){ var value = $(this).val(); if(value != 0 && value != ""){ values.push(value); } }); $('.form-group-multiple-selects .input-group-multiple-select select').find('option').each(function(){ var optionValue = $(this).val(); var selectValue = $(this).parent().val(); if(in_array(optionValue,values)!= -1 && selectValue != optionValue) { $(this).attr('disabled', 'disabled'); } else { $(this).removeAttr('disabled'); } }); } function in_array(needle, haystack){ var found = 0; for (var i=0, length=haystack.length;i<length;i++) { if (haystack[i] == needle) return i; found++; } return -1; } });

Related: See More


Questions / Comments: