2016-05-17 95 views
0

我遇到了一個難以解決的問題。我有兩個表用克隆複製表格行

<div class="form-group"> 
    <div class="row"> 
     <div class="col-md-12"> 
      <div class="col-md-12 noPadding"> 
       <table class="table table-bordered table-hover additionalMargin alignment" id="table1"> 
        <thead> 
        <tr> 
         <th>Campaign Type</th> 
         <th>Deployment Date</th> 
         <th>Additional Information</th> 
        </tr> 
        </thead> 
        <tbody> 
        <tr class='template'> 
         <td> 
          <select class="selectType" name='typeInput[0][campType]' id="campInput"> 
           <option value=""></option> 
           <option value="Main">Main</option> 
           <option value="Other">Standalone</option> 
          </select> 
         </td> 
         <td> 
          <input type="text" name='typeInput[0][deliveryDate]' id="dateInput" placeholder='Deployment Date' class="form-control dateControl"/> 
         </td> 
         <td> 
          <textarea name='typeInput[0][addInfo]' id="additionalInput" placeholder='Additional Information' class="form-control noresize"></textarea> 
         </td> 
        </tr> 
        </tbody> 
       </table> 
       <a id='add' class="pull-right btn btn-default">Add Row</a> 
       <a id='delete' class="pull-right btn btn-default">Delete Row</a> 
      </div> 
     </div> 
    </div> 
</div> 


<div class="form-group"> 
    <div class="row"> 
    <div class="col-md-12"> 
     <div class="col-md-12 noPadding"> 
     <table class="table table-bordered table-hover additionalMargin alignment" id="table4"> 
      <thead> 
      <tr> 
       <th>Additional Information</th> 
       <th>Deployment Date</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr class='template4'> 
       <td> 
       <textarea name='amendsInput[0][addInfo]' id="additionalInput" placeholder='Additional Information' class="form-control noresize"></textarea> 
       </td> 
       <td> 
       <input type="text" name='amendsInput[0][deliveryDate]' id="dateInput" placeholder='Deployment Date' class="form-control dateControl"/> 
       </td> 
      </tr> 
      </tbody> 
     </table> 
     <a id='add4' class="pull-right btn btn-default">Add Row</a> 
     <a id='delete4' class="pull-right btn btn-default">Delete Row</a> 
     </div> 
    </div> 
    </div> 
</div> 

的表具有3個輸入端,其它的具有2。當添加按鈕被在任表推,我克隆的表格行,該方法包括克隆一個datepicker。

事情一直很好,但現在我有一個問題。第二張桌子我用4個table4,template4,add4和delete4。然後,我從優質表中複製了Javascript,但將4添加到了所有內容中(我重複了它,因爲此表有不同的輸入)。這導致了下面的代碼。

$(function() { 
    initJQueryPlugins(); 

    $('#add').on('click', function() { 
     $last_row = $('#table1 > tbody > tr').last(); 
     if(!hasValues($last_row)){ 
      alert('You need to insert at least one value in last row before adding'); 
     } else { 
      add_row($('#table1')); 
     } 
    }); 

    $('#delete').on('click', function() { delete_row($('#table1')); }); 


    $('#add4').on('click', function() { 
     $last_row = $('#table4 > tbody > tr').last(); 
     if(!hasValues4($last_row)){ 
      alert('You need to insert at least one value in last row before adding'); 
     } else { 
      add_row4($('#table4')); 
     } 
    }); 

    $('#delete4').on('click', function() { delete_row4($('#table4')); }); 
}); 


function add_row($table) { 
    var tr_id = $table.find('tr').length - 1; 
    var $template = $table.find('tr.template'); 

    var $tr = $template.clone().removeClass('template').prop('id', tr_id); 

    $tr.find(':input').each(function() { 
     if($(this).hasClass('hasDatepicker')) { 
      $(this).removeClass('hasDatepicker').removeData('datepicker'); 
     } 

     var input_id = $(this).prop('id'); 
     input_id = input_id + tr_id; 
     $(this).prop('id', input_id); 

     var new_name = $(this).prop('name'); 
     new_name = new_name.replace('[0]', '['+ tr_id +']'); 
     $(this).prop('name', new_name); 

     $(this).prop('value', ''); 
    }); 
    $table.find('tbody').append($tr); 

    $(".dateControl", $tr).datepicker({ 
     dateFormat: "dd-mm-yy" 
    }); 

    $(".selectType", $tr).select2({ 
     tags: true 
    }); 
} 

function hasValues($row){ 
    $optVal = $row.find('td option:selected').text(); 
    $inputVal = $row.find('td input').val(); 
    $textVal = $row.find('td textarea').val(); 
    if($optVal != "" || $inputVal != "" || $textVal != ""){ 
      return true; 
    } else { 
      return false; 
    } 
} 

function delete_row($table) { 
    var curRowIdx = $table.find('tr').length - 1; 
    if (curRowIdx > 2) { 
     $("#" + (curRowIdx - 1)).remove(); 
     curRowIdx--; 
    } 
} 

function add_row4($table4) { 
    var tr_id = $table4.find('tr').length - 1; 
    var $template = $table4.find('tr.template4'); 

    var $tr = $template.clone().removeClass('template4').prop('id', tr_id); 

    $tr.find(':input').each(function() { 
     if($(this).hasClass('hasDatepicker')) { 
      $(this).removeClass('hasDatepicker').removeData('datepicker'); 
     } 

     var input_id = $(this).prop('id'); 
     input_id = input_id + tr_id; 
     $(this).prop('id', input_id); 

     var new_name = $(this).prop('name'); 
     new_name = new_name.replace('[0]', '['+ tr_id +']'); 
     $(this).prop('name', new_name); 

     $(this).prop('value', ''); 
    }); 
    $table4.find('tbody').append($tr); 

    $(".dateControl", $tr).datepicker({ 
     dateFormat: "dd-mm-yy" 
    }); 
} 

function hasValues4($row4){ 
    $inputVal = $row4.find('td input').val(); 
    $textVal = $row4.find('td textarea').val(); 
    if($inputVal != "" || $textVal != ""){ 
      return true; 
    } else { 
      return false; 
    } 
} 

function delete_row4($table4) { 
    var curRowIdx = $table4.find('tr').length - 1; 
    if (curRowIdx > 2) { 
     $("#" + (curRowIdx - 1)).remove(); 
     curRowIdx--; 
    } 
} 

function initJQueryPlugins() { 
    add_row($('#table1')); 
    add_row4($('#table4')); 
} 

我已成立了一個工作FIDDLE 問題是這樣的。如果你開始在第一個表中添加幾行,這一切都可以正常工作。在此之後,在第二個表中添加幾行。這似乎工作正常。但是,現在開始刪除第二個表中的行。出於某種原因,它似乎也刪除了第一個表中的行。

所以我的主要問題是爲什麼會發生這種情況?另外,有沒有什麼辦法可以做到這一點,而無需複製代碼?第二個表不使用select2。

感謝

+0

關於刪除... (「#」+(curRowIdx - 1))。remove(); 你需要一個特定的選擇器,這個選擇器對兩個表都有效...... –

+0

我想明白這一點。我沒有通過它table4? –

+1

更改$(「#」+(curRowIdx - 1))。remove();事情到$ table4.find('tr')。last()。remove();和table1一樣...實際上你的TR-Elements在你的兩個桌子之間沒有唯一的ID ...你必須改變你的選擇器從右表中挑選 –

回答

1

您要刪除此:

$("#" + (curRowIdx - 1)).remove(); 

這個id也是第一臺可用的,你要選擇一個更特定的選擇

,如:

$table4.find("#" + (curRowIdx - 1)).remove(); 

或更好:(上述K.Bastian的評論)

$table4.find('tr').last().remove() 

我編輯您的樣品在這裏:

https://jsfiddle.net/cLssk6bv/

在這裏,我也刪除了dublicated代碼,只有不同的插入方法仍然存在:

https://jsfiddle.net/cLssk6bv/1/

+0

完美,謝謝 –