JQuery Adding new Row in Table based on Selection

ZATRANO

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

screenshot

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...

Swati

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery Adding new Row in Table based on Selection, but Latest selection override the whole table everytime

From Dev

Adding new row to the table using javascript / jquery

From Dev

Adding a new row to a table in jQuery but apply unique ID to input names

From Dev

Compare two table and adding row based on condition

From Dev

Adding rows to a table based on number in another row

From Dev

Compare two table and adding row based on condition

From Dev

Error while adding a new row to DataTable in jQuery

From Dev

Adding table cells to end of row using jquery based on radio button selections

From Dev

Adding the numbers in table row using jquery

From Dev

Adding the numbers in table row using jquery

From Dev

Adding specific td to a specific row in a table with JQuery

From Dev

Adding a row in a table after pressing a key in jQuery

From Dev

How to retain the dropdown selection after adding new rows or columns to a table

From Dev

Copy Row Table into 'New Table'. | jQuery

From Dev

insert a row in 3 tables when adding another row for a new table

From Dev

Adding new rows to table on clicking button in JQuery

From Dev

Dynamically Adding a Row with jQuery adds row to bottom of table

From Dev

Append a new row to a table using jQuery

From Dev

Drag a specific row into a new table with jQuery Sortable

From Dev

jQuery - issue inserting new row in html table

From Dev

Add new table row to jQuery dynamically

From Dev

Update table row based on cell value jquery

From Dev

Adding a new row in a table after every 3 images from database

From Dev

Excel stops working when adding new row to the Table

From Dev

C# wpf adding new row to another data table

From Dev

HTML Row table selection

From Dev

HTML Row table selection

From Dev

Bootstrap Table Row Selection

From Dev

SQL - Insert new row into an existing table based on another table entry

Related Related

  1. 1

    JQuery Adding new Row in Table based on Selection, but Latest selection override the whole table everytime

  2. 2

    Adding new row to the table using javascript / jquery

  3. 3

    Adding a new row to a table in jQuery but apply unique ID to input names

  4. 4

    Compare two table and adding row based on condition

  5. 5

    Adding rows to a table based on number in another row

  6. 6

    Compare two table and adding row based on condition

  7. 7

    Error while adding a new row to DataTable in jQuery

  8. 8

    Adding table cells to end of row using jquery based on radio button selections

  9. 9

    Adding the numbers in table row using jquery

  10. 10

    Adding the numbers in table row using jquery

  11. 11

    Adding specific td to a specific row in a table with JQuery

  12. 12

    Adding a row in a table after pressing a key in jQuery

  13. 13

    How to retain the dropdown selection after adding new rows or columns to a table

  14. 14

    Copy Row Table into 'New Table'. | jQuery

  15. 15

    insert a row in 3 tables when adding another row for a new table

  16. 16

    Adding new rows to table on clicking button in JQuery

  17. 17

    Dynamically Adding a Row with jQuery adds row to bottom of table

  18. 18

    Append a new row to a table using jQuery

  19. 19

    Drag a specific row into a new table with jQuery Sortable

  20. 20

    jQuery - issue inserting new row in html table

  21. 21

    Add new table row to jQuery dynamically

  22. 22

    Update table row based on cell value jquery

  23. 23

    Adding a new row in a table after every 3 images from database

  24. 24

    Excel stops working when adding new row to the Table

  25. 25

    C# wpf adding new row to another data table

  26. 26

    HTML Row table selection

  27. 27

    HTML Row table selection

  28. 28

    Bootstrap Table Row Selection

  29. 29

    SQL - Insert new row into an existing table based on another table entry

HotTag

Archive