提问者:小点点

如何使用JQuery或JavaScript在HTML中的多个表之间添加一个表?


如何在任意两个表之间添加表,目前我可以在追加模式下添加表,即在末尾添加表,即! 因此,Add table here中的基表中有按钮,如果用户单击该按钮,则应将表添加到单击该表按钮的表之后。

例如:

我已经创建了5个表,我想在第三个表之后再添加一个表,所以如果我点击add table here按钮,那么表应该被添加到第四个位置,而不是最后。

有什么建议吗?有什么解决办法吗

null

var filterTableNum = 0;
$('.add-table').click(add_empty_table);

function add_empty_table() {
    const originTable = document.getElementById('source-table');
    let newTable = originTable.cloneNode(true);
    newTable.id = 'source' + ++filterTableNum;
    newTable.querySelectorAll('input').forEach((element) => {
        element.value = '';
    });
    $(newTable).find('.component-id').val(newTable.id);
    $('#main-div').append(newTable);
    
}

function add_table_between(){

}
#container>table{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="add-table">
        Add Table
      </button>
<div id="container">     
<table
          id="source-table"
          data-component-type="Source"
          class="component-base table table-responsive table-striped rounded"
        >
          <thead class="thead-dark">
            <tr>
              <th colspan="6" class="text-center">
                <span>Source : </span
                ><input
                  id="id"
                  type="text"
                  name="id"
                  placeholder="id"
                  class="component-id form-control-head"
                />
                <button
                  class="delete-component btn btn-danger"
                  style="margin: 1%;"
                >
                  Delete Table
                </button>
              </th>
            </tr>

            <tr>
              <th scope="row">Type</th>
              <th scope="row">Name</th>
             
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                <input
                  id="type"
                  type="text"
                  name="type"
                  class="form-control"
                  placeholder="Type"
                />
              </td>
              <td>
                <input
                  id="Name"
                  type="text"
                  name="name"
                  class="form-control"
                  placeholder="Name"
                />
              </td>
            </tr>
          </tbody>
          <tfoot>    
            <tr>
            <td>
            <button class="btwn-table">Add Table Here</button>
           
            </td>
          </tr>
        </table>
        
 <div class="main-div" id="main-div"></div>

null

JS Fiddle:https://jsfiddle.net/shreekantbatale2/krnpfw36/1/


共1个答案

匿名用户

null

var filterTableNum = 0;
$('.add-table').click(add_empty_table);
$(document).on('click', '.btwn-table', add_table_between);

function create_table() {
    const originTable = document.getElementById('source-table');
    let newTable = originTable.cloneNode(true);
    newTable.id = 'source' + ++filterTableNum;
    newTable.querySelectorAll('input').forEach((element) => {
        element.value = '';
    });
    $(newTable).find('.component-id').val(newTable.id);
    return $(newTable);
}

function add_empty_table() {
    let newTable = create_table();
    $('#main-div').append(newTable);
}

function add_table_between(e){
    let newTable = create_table();
    let originTable = $(e.target.closest('table'));
    newTable.insertAfter(originTable);
}
#container>table{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="add-table">
        Add Table
      </button>
<div id="container">     
<table
          id="source-table"
          data-component-type="Source"
          class="component-base table table-responsive table-striped rounded"
        >
          <thead class="thead-dark">
            <tr>
              <th colspan="6" class="text-center">
                <span>Source : </span
                ><input
                  id="id"
                  type="text"
                  name="id"
                  placeholder="id"
                  class="component-id form-control-head"
                />
                <button
                  class="delete-component btn btn-danger"
                  style="margin: 1%;"
                >
                  Delete Table
                </button>
              </th>
            </tr>

            <tr>
              <th scope="row">Type</th>
              <th scope="row">Name</th>
             
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>
                <input
                  id="type"
                  type="text"
                  name="type"
                  class="form-control"
                  placeholder="Type"
                />
              </td>
              <td>
                <input
                  id="Name"
                  type="text"
                  name="name"
                  class="form-control"
                  placeholder="Name"
                />
              </td>
            </tr>
          </tbody>
          <tfoot>    
            <tr>
            <td>
            <button class="btwn-table">Add Table Here</button>
           
            </td>
          </tr>
        </table>
        
 <div class="main-div" id="main-div"></div>