2016-05-17 6 views
0

解決には苦労しています。表は、追加ボタンをいずれかのテーブルにプッシュされると、他の2を有する3つの入力を有し、私は2つのテーブルクローンで表の行を複製する

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

一つは、私は、日付ピッカーをクローニング含むテーブルの行を、クローニングしていました。

状況は良くなっていますが、今は問題があります。 2番目のテーブルはすべて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 を設定しています。最初のテーブルにいくつかの行を追加すると、これはすべて正常に動作します。この後、2番目のテーブルにいくつかの行を追加します。これはうまくいくようです。ただし、2番目のテーブルの行の削除を開始します。何らかの理由で、最初のテーブルの行も削除されているようです。

私の主な質問は、なぜこれが起こるのでしょうか?さらに、私はコードを複製せずにこれを行うことができる方法はありますか? 2番目のテーブルはselect2を使用しません。あなたはこれを削除している

おかげ

+0

を... $( "#" +(curRowIdx - 1))。remove(); 具体的なセレクタが必要です。このセレクタは両方のテーブルに有効です... –

+0

私はそれを理解しようとしています。私はtable4を渡していませんか? –

+1

変更$( "#" +(curRowIdx - 1))。remove(); $ table4.find( 'tr')。last()。remove(); table1と同じです...実際にあなたのTR-Elementsはあなたの2つのテーブルの間に一意の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

完璧、ありがとう –

関連する問題