Bootstrap исчезновение строки таблицы - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь добавлять и удалять строки при нажатии кнопок. Все работает правильно, но всякий раз, когда я масштабирую окно до небольшого размера, чтобы проверить отзывчивость мобильного устройства, строки исчезают.

Я новичок в Bootstrap Я не уверен, сделал ли я что-нибудь ошибки.

$(document).ready(function() {
  //Try to get tbody first with jquery children. works faster!

  var tbody = $('#myTable').children('tbody');

  //Then if no tbody just select your table 
  var table = tbody.length ? tbody : $('#myTable');

  $('#addrow').click(function() {
    //Add row

    table.append('<tr scope="row"><td ><select  id="ex1"  required ><option value="">Iphone</option><option value="">IPod</option><option value="">Galaxy Tab</option> <option value="">PocoPhone</option> </select>  </td>  <td >  <input  id="ex2" type="number">  </td> <td ><input  id="ex3" type="number"> </td> <td > <input  id="ex4" type="number"> </td> <td >  <button   class="btnDelete btn-outline-success"   id="delrow"  >Delete</button>  </td>  </tr>  ');
  })

  $("#myTable").on('click', '.btnDelete', function() {
    $(this).closest('tr').remove();
  });

});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.16.0/dist/bootstrap-table.min.css">


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

<script src="https://unpkg.com/bootstrap-table@1.16.0/dist/bootstrap-table.min.js"></script>

<script src="https://unpkg.com/bootstrap-table@1.16.0/dist/extensions/mobile/bootstrap-table-mobile.min.js"></script>

<table class="table table-striped" data-toggle="table" data-pagination="true" data-mobile-responsive="true" id="myTable">

  <thead>
    <tr>
      <th scope="col">Product</th>
      <th scope="col">QTY</th>
      <th scope="col">Price</th>
      <th scope="col">Total</th>
      <th scope="col">Option</th>
    </tr>
  </thead>
  <tbody>
    <tr scope="row">
      <td>
        <select id="ex1" required>
          <option value="">Iphone</option>
          <option value="">IPod</option>
          <option value="">Galaxy Tab</option>
          <option value="">PocoPhone</option>
        </select>
      </td>
      <td>
        <input id="ex2" type="number">
      </td>
      <td>
        <input id="ex3" type="number">
      </td>
      <td>
        <input id="ex4" type="number">
      </td>
      <td>
        <button class="btn btn-outline-success" id="addrow" class="addnew"> Add </button>
      </td>
    </tr>
  </tbody>
</table>

1 Ответ

1 голос
/ 14 июля 2020

Этот код работает отлично

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    
    
    <form class="form-horizontal">
        <div class="table-responsive">
            <table class="table ">
                <thead>
                    <th>Product</th>
                    <th>QTY</th>
                    <th>Cost</th>
                    <th>Price</th>
                    <th>Total</th>
                    <th>Option</th>
                   
                </thead>
                <tbody>
                    <tr>
                         <td>
            <select id="ex1" required>
              <option value="">Iphone</option>
              <option value="">IPod</option>
              <option value="">Galaxy Tab</option>
              <option value="">PocoPhone</option>
            </select>
          </td>
                        <td><input  class="form-control" id="ex2" type="number" ></td>
                        <td><input class="form-control" id="ex3" type="number"></td>
                        <td>
                            <select class="form-control">
                                <option>Monthly</option>
                                <option>Yearly</option>
                            </select>
                        </td>
                        <td><input class="form-control" id="ex2" type="number" ></td>
                        <td><button class="btn btn-outline-success" id="addrow" class="addnew"> Add </button></td>
                        
                    </tr>
                </tbody>
            </table>
        </div>
    </form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...