When the user selects a value from drop down which is the product a row is dynamically added which work perfectly fine in my laravel project.
Then adding a new product to the row which also adds a row to the Table but the previous product record is also replaced by this product value.
<div class="card-body">
<div class="row">
<div class="col-12 col-md-12 col-sm-12 form-group table-responsive m-b-30">
<table class="table table-bordered" id="trainingsTable">
<thead>
<tr>
<th>Ekle</th>
<th>Eğitim</th>
<th>Satış</th>
<th>Liste</th>
<th>İndirimli</th>
<th>Sınav</th>
</tr>
</thead>
<tbody>
<tr>
<td><button type="button" name="add" id="add" class="btn btn-success"><i class="fa fa-plus-circle"></i></button></td>
<td>
<select class="form-control selectTraining" name="training_id[]" id="training_id[]">
<option>Seçiniz</option>
@foreach($trainings as $training)
<option data-price="{{ $training->price }}" data-listprice="{{ $training->list_price }}" data-examprice="{{ $training->exam_price }}" value="{{ $training->id }}">{{ $training->name }}</option>
@endforeach
</select>
</td>
<td>{!! Form::text('sale_amount[]', old('sale_amount'), ['id' => 'sale_amount[]', 'class' => 'form-control sale_amount']) !!}</td>
<td><input type="text" name="list_price" id="list_price" value="" class="form-control list_price" disabled /></td>
<td><input type="text" name="price" id="price" value="" class="form-control price" disabled /></td>
<td><input type="text" name="exam_price" id="exam_price" value="" class="form-control exam_price" disabled /></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
// Script Start
$('.selectTraining').on('change', function() {
$('.list_price').val($(this).find(':selected').data('listprice'));
$('.price').val($(this).find(':selected').data('price'));
$('.exam_price').val($(this).find(':selected').data('examprice'));
});
var i = 0;
$("#add").click(function(){
++i;
$("#trainingsTable").append('<tr>' +
'<td><button type="button" class="btn btn-danger remove-tr"><i class="fa fa-minus-circle"></i></button></td>' +
'<td>' +
'<select class="form-control selectTraining" name="training_id[]" id="training_id[]">' +
'<option>Seçiniz</option>' +
'@foreach($trainings as $training)' +
'<option data-price="{{ $training->price }}" data-listprice="{{ $training->list_price }}" data-examprice="{{ $training->exam_price }}" value="{{ $training->id }}" >{{ $training->name }}</option>' +
'@endforeach' +
'</select>' +
'</td>' +
'<td>{!! Form::text('sale_amount[]', old('sale_amount'), ['id' => 'sale_amount[]', 'class' => 'form-control sale_amount']) !!}</td>' +
'<td><input type="text" name="list_price" id="list_price" value="" class="form-control list_price" disabled /></td>' +
'<td><input type="text" name="price" id="price" value="" class="form-control price" disabled /></td>' +
'<td><input type="text" name="exam_price" id="exam_price" value="" class="form-control exam_price" disabled /></td></tr>');
$('.selectTraining').on('change', function() {
$('.list_price').val($(this).find(':selected').data('listprice'));
$('.price').val($(this).find(':selected').data('price'));
$('.exam_price').val($(this).find(':selected').data('examprice'));
});
});
$(document).on('click', '.remove-tr', function(){
$(this).parents('tr').remove();
});
// Script End
A product can be selected in the sold row and the offer Price, Discounted Price and Exam Fee are automatically filled in the relevant fields. But when two or more lines are selected, all fields are filled with the latest contact information, crushing other selections.
Thanks for helps...
When your select-box changes you are targetting all inputs with class list_price , price..etc
.Instead you can use $(this).closest('tr')
this will get the closest tr where select-box has been change then use .find()
to find inputs and add required values there .
Demo Code :
$(document).on('change', '.selectTraining', function() {
var selector = $(this).closest('tr') //get closest tr
//find inputs inside required tr and then find input
selector.find('.list_price').val($(this).find(':selected').data('listprice'));
selector.find('.price').val($(this).find(':selected').data('price'));
selector.find('.exam_price').val($(this).find(':selected').data('examprice'));
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
<div class="row">
<div class="col-12 col-md-12 col-sm-12 form-group table-responsive m-b-30">
<table class="table table-bordered" id="trainingsTable">
<thead>
<tr>
<th>Ekle</th>
<th>Eğitim</th>
<th>Satış</th>
<th>Liste</th>
<th>İndirimli</th>
<th>Sınav</th>
</tr>
</thead>
<tbody>
<tr>
<td><button type="button" name="add" id="add" class="btn btn-success"><i class="fa fa-plus-circle"></i></button></td>
<td>
<select class="form-control selectTraining" name="training_id[]" id="training_id[]">
<option>Seçiniz</option>
<option data-price="12" data-listprice="23" data-examprice="32" value="1">A</option>
<option data-price="13" data-listprice="33" data-examprice="33" value="2">B</option>
</select>
</td>
<td><input type="text" class="form-control sale_amount" name="sale_amount[]">
<td><input type="text" name="list_price" value="" class="form-control list_price" disabled /></td>
<td><input type="text" name="price" value="" class="form-control price" disabled /></td>
<td><input type="text" name="exam_price" value="" class="form-control exam_price" disabled /></td>
</tr>
<tr>
<td><button type="button" class="btn btn-danger remove-tr"><i class="fa fa-minus-circle"></i></button></td>
<td>
<select class="form-control selectTraining" name="training_id[]" id="training_id[]">
<option>Seçiniz</option>
<option data-price="12" data-listprice="23" data-examprice="32" value="1">A</option>
<option data-price="13" data-listprice="33" data-examprice="33" value="2">B</option>
</select>
</td>
<td><input type="text" class="form-control sale_amount" name="sale_amount[]">
<td><input type="text" name="list_price" value="" class="form-control list_price" disabled /></td>
<td><input type="text" name="price" value="" class="form-control price" disabled /></td>
<td><input type="text" name="exam_price" value="" class="form-control exam_price" disabled /></td>
</tr>
<tr>
<td><button type="button" class="btn btn-danger remove-tr"><i class="fa fa-minus-circle"></i></button></td>
<td>
<select class="form-control selectTraining" name="training_id[]" id="training_id[]">
<option>Seçiniz</option>
<option data-price="12" data-listprice="23" data-examprice="32" value="1">A</option>
<option data-price="13" data-listprice="33" data-examprice="33" value="2">B</option>
</select>
</td>
<td><input type="text" class="form-control sale_amount" name="sale_amount[]">
<td><input type="text" name="list_price" value="" class="form-control list_price" disabled /></td>
<td><input type="text" name="price" value="" class="form-control price" disabled /></td>
<td><input type="text" name="exam_price" value="" class="form-control exam_price" disabled /></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments