In my scenario just select the list box options, that values and text going to append to the table rows, but i am facing small issues, user not allow to select same options (already selected options) and user not allow to append same values in the table.. if user select same option show alert messages like, 'you are already selected, select another option' like that..
but here now if i delete any rows and add back that same value, it showing alert 'its matching', i don't need that, if i delete and add back the same value into the table..
Full Fiddle here..
Sample code here..
// Small table
// Exiscisting supplier dropdown change function - for new method
$("#excist_supp").on("change", function() {
if (!check()) {
$(".indexDrop").hide();
var newText = $("#excist_supp option:selected").text();
var newValue = $("#excist_supp option:selected").val();
console.log(newText, 'not yet to delete');
console.log(newValue, 'not yet to delete');
// delete button
$("table.order-list").on("click", ".ibtnDel", function(_event) {
var $row = $(this).closest("tr"); // Find the row
var $text = $row.find(".newStyle1").val(''); // Find the text
var $secTd = $row.find(".newStyle2").val('');
$(this).closest("tr").remove();
});
$("#letterTable tbody").append('<tr><td><input type="text" id="sm_supp" value="' + newText + '" class="form-control newStyle1" name="sm_supp"></td><td><input type="text" id="sm_code" value="' + newValue + '" class="form-control newStyle2" name="sm_code"></td><td><button type="button" class="adRow ibtnDel newExcist" style="width:30%;position: relative;right: 25%; margin-bottom:0px">x</button></a></td></tr>');
$("#letterTable tr").each(function(i) {
var count = (i) - 1;
// alert(count);
if (i === 0) return;
var input1 = $(this).find('.newStyle1');
var input2 = $(this).find('.newStyle2');
// id
// Dropdowns increment
input1.eq(0).attr("id", "sm_supp" + count); // Text fields
input2.eq(0).attr("id", "sm_code" + count);
input1.eq(0).attr("name", "reqpartys[" + count + "].pname"); // Text fields
input2.eq(0).attr("name", "reqpartys[" + count + "].pacd");
});
}
});
/* compare two fields and not allow to selected options to select second time */
var temp = [];
function check() {
var listVal = $('#excist_supp').val();
var tableVal = $('.newStyle2').val();
var nawTab = JSON.stringify(tableVal);
if (temp.includes(listVal)) {
alert('matching!');
$(".notify").addClass("active");
$("#notifyType").addClass("failure");
setTimeout(function() {
$(".notify").removeClass("active");
$("#notifyType").removeClass("failure");
}, 3000);
return true;
} else {
// alert('Not matching!');
temp.push(listVal)
return false;
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<!-- List box -->
<div class="col-sm-6">
<div class="col-12">
<div class="row">
<div class="col-12">
<div class="selector">
<select class="col-12 listbox newBox" name="List_0" size="20" id="excist_supp" style="height: 125px !important;">
<option class="indexDrop">Choose Existing Suppliers</option>
<option value="0000">Komal </option>
<option value="0001">Ranjeet</option>
<option value="0002">Vishal </option>
<option value="0003">Gaurav</option>
<option value="0004">Dhanpat</option>
<option value="0005">Joe</option>
<option value="0006">Gowri</option>
<option value="0007">shankar</option>
<option value="0008">Dhanpat</option>
</select>
</div>
</div>
</div>
</div>
</div>
<!-- Small table -->
<div class="col-5">
<div class="container">
<div class="row clearfix">
<div class="table-wrapper">
<div class="table-scroll">
<table id="letterTable" class="table table-bordered table-striped order-scroll order-list">
<thead>
<tr style="background-color: #680f79;color: #fff;">
<th class="text-center">Supplier Name</th>
<th class="text-center">Supplier Code</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody id="mytbody" style="text-align: center;">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
temparray variable... you add the rows to it but never remove it from the array. In your delete function, check if row exist in temp array and if it does, delete it