2016-03-05 75 views
1

嗨,我是一個新手DataTables ....我有一個數據表,我想添加行(一次一個),並具有多行刪除。我設法得到正常的功能工作,但問題是,當我刪除一行/行,然後嘗試添加一個新行,刪除的行出現在相同的位置作爲選定的行....下面是代碼我現在用初始化數據表和功能添加和刪除我的數據表中的行....任何幫助,將不勝感激......刪除的行出現在添加新行DataTables

  $(document).ready(function(){ 
      var counter = 1 
      $('#addRow').on('click', function (a,b,c,d,e,f) { 

      a = "test" 

      t.row.add([ 
       counter +a, 
       counter +'test', 
       counter +'test', 
       counter +'test', 
       counter +'test', 
       counter +'test' 
      ]).draw(false); 
       counter ++; 
      }); 


      $('#example tbody').on('click', 'tr', function (e) { 
       if ($(this).hasClass('selected')) { 
        $(this).removeClass('selected'); 
        $(this).css('background-color', '') 
       } 
       else { 
        t.$('tr.selected');//.removeClass('selected'); 
        $(this).addClass('selected'); 
        $(this).css('background-color', 'blue'); 
       } 
      }); 


      $('#removeRow').click(function() { 
       var anSelected = fnGetSelected(t); 
       $(anSelected).remove(); 
     });  

      var t = $('#example').DataTable({ 
      'fnCreatedRow': function (nRow, aData, iDataIndex) { 
      $(nRow).attr('id', 'my' + iDataIndex); 
      }, 
      "aoColumnDefs": [ { 
       "aTargets": [0,1,2,3,4,5], 
       "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) { 
        var idx = t.cell(nTd).index().column; 
        var title = t.column(idx).header(); 
        $(nTd).attr("id",$(title).html() +"_"+ iRow) 
       } 
      } ] 
    }); 



}); 

      function fnGetSelected(oTableLocal) 
     { 
      return oTableLocal.$('tr.selected'); 
     } 

上面的代碼只是幾件事情來測試功能不需要....

****更新**** 表html是寫在一個模板中,我打電話給一個表單...具有表單代碼的父gsp文件如下... ..

主要GSP文件

<g:form action="saveTravelDetails" id="createRequest" name="createRequest" autocomplete="off">      
<g:render template="newTravelRequest" /> 
<div class="row"> 
    <div class="col-md-12"> 
     <div class="form-group" style="text-align: center;"> 
      <input id="circuit_save" class="btn btn-primary circuit_validate" tabindex="700" class="button_text" type="submit" name="circuit_save" value="Save" /> 
      <input id="circuit_cancel" class="btn btn-default" class="button_text" type="button" name="circuit_cancel" value="Cancel" /> 
      <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#detailsModal">Add new row</button> 
      <button type="button" class="btn btn-primary" id="removeRow">Remove Selected Row</button> 
     </div> 
    </div> 
</div> 
</g:form> 

模板HTML代碼如下

<div class="row"> 
    <div class="col-md-12"> 
     <div class="panel panel-default"> 
      <div class="panel-heading"> 
       <i class="fa fa-user"></i> Travel Details 
       <div class="panel-tools"> 
        <a href="#" class="btn btn-xs btn-link panel-collapse collapses"> 
        </a> 
       </div> 
      </div> 
      <div class="panel-body"> 
      <table cellpadding="0" cellspacing="0" border="0" class="display" id="example"> 
       <thead class="alignCenter"> 
       <tr> 
      <th class="headerclass">Departure Date</th> 
      <th class="headerclass">Return Date</th> 
      <th class="headerclass">Departure Destination</th> 
      <th class="headerclass">Arrival Destination</th> 
      <th class="headerclass">Choose Mode Of Transport</th> 
      <th class="headerclass">Cost of Travel</th>   
       </tr> 
       </thead> 
       <tbody></tbody> 
       <tfoot class="alignCenter headerclass"> 
       <tr> 
      <th class="headerclass">Departure Date</th> 
      <th class="headerclass">Return Date</th> 
      <th class="headerclass">Departure Destination</th> 
      <th class="headerclass">Arrival Destination</th> 
      <th class="headerclass">Choose Mode Of Transport</th> 
      <th class="headerclass">Cost of Travel</th>  
       </tr> 
       </tfoot> 
       </table> 

      </div> 
     </div> 
    </div> 
</div> 

在初始化數據表下面給出主文件更新的JavaScript代碼...這也有函數定義添加和刪​​除行

$(document).ready(function(){ 


     var counter = 1 
    $('#addRow').on('click', function() { 

      t.row.add([ 

       $('#departureDate_1').val(), 
       $('#returnDate_1').val(), 
       $('#startDestination').val(), 
       $('#endDestination').val(), 
       $('#travelMode').val(), 
       $('#travelFare').val(), 

      ]).draw(false); 
      counter ++; 

      $('#detailsModal').modal('hide'); 
     }); 


       $('#example tbody').on('click', 'tr', function (e) { 
        if ($(this).hasClass('selected')) { 
         $(this).removeClass('selected'); 
         $(this).css('background-color', '') 
        } 
        else { 
         t.$('tr.selected');//.removeClass('selected'); 
         $(this).addClass('selected'); 
         $(this).css('background-color', 'blue'); 
        } 
       }); 


      $('#removeRow').click(function() { 
        var anSelected = fnGetSelected(t); 
        $(anSelected).remove(); 
      });  

     var t = $('#example').DataTable({ 
       'fnCreatedRow': function (nRow, aData, iDataIndex) { 
       $(nRow).attr('id', 'my' + iDataIndex); 
       $(nRow).attr('name', 'my' + iDataIndex); // or whatever you choose to set as the id 
       }, 
       "aoColumnDefs": [ { 
        "aTargets": [0,1,2,3,4,5], 
        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) { 
         var idx = t.cell(nTd).index().column; 
         var title = t.column(idx).header(); 
         $(nTd).attr("name",$(title).html() +"_"+ iRow) 
         $(nTd).attr("id",$(title).html() +"_"+ iRow) 
        } 
       } ] 
     }); 

     $('#departureDate').on('change',function(){ 
      $('.bootstrap-datetimepicker-widget').css('display','none'); 
     }); 

     $('#returnDate').on('change',function(){ 
      $('.bootstrap-datetimepicker-widget').css('display','none'); 
     }); 


     $('#departureDate').datetimepicker({ 
      format: 'YYYY-MM-DD', 
      pick12HourFormat: false, 
      autoclose: true 
     }); 

     $('#returnDate').datetimepicker({ 
      format: 'YYYY-MM-DD', 
      pick12HourFormat: false, 
      autoclose: true 
     }); 



    }); 

function fnGetSelected(oTableLocal) 
{ 
    return oTableLocal.$('tr.selected'); 
} 

在此先感謝

+0

你能創建一個包含相關html代碼的代碼片段嗎? – silviagreen

回答

-1

感謝您閱讀我的問題並花時間回答它我找到了解決方案...下面是可用於其他場景的代碼.. ..

$('#removeRow').click(function() { 
    var anSelected = fnGetSelected(t); 
    alert(anSelected); 
    t.row(anSelected).remove().draw(false); 
}); 

function fnGetSelected(oTableLocal) { 
    return oTableLocal.$('tr.selected'); 
} 
+0

請使用您問題上的編輯鏈接添加其他信息。後回答按鈕應該只用於問題的完整答案。 - [來自評論](/評論/低質量帖/ 11615916) –

+0

但我想回答這個問題....應該也是在額外的信息或我的答案不完整...我是相當新的這個論壇 – Apoorv

0

嘗試

$(anSelected).remove().draw(false); 

因爲它是現在你只添加一個新行後重繪表。正如draw() docs說:

不被大多數數據表API操作自動進行一場平局,讓行動組(例如添加多行是更有效的,如果你將它們分組

好。選項是在批量刪除行後繪製。