2015-05-22 2 views
1

У меня есть таблица, которая отображает результат запроса. Кстати, я использую codeigniter. Я хочу добавить флажок в каждую строку. И каждый раз, когда флажок установлен, я активировать кнопку редактирования и кнопку удаления ..Как добавить флажок в datatable php

Я сделал это так, но только на первом ряду, это включить и отключить кнопку:

<div style="margin-top: 10px;margin-left: 10px;margin-bottom: 10px"> 
       <a type="button" class="btn btn-default" href="<?php echo site_url('home/create')?>" ><span class=" fa fa-plus-circle"></span> Create </a> 
       <button type="button" id="edit" class="btn btn-default" disabled><span class=" fa fa-edit "></span> Edit</button> 
       <button type="button" class="btn btn-default" disabled > <span class=" fa fa-trash-o"></span> Delete</button> 
      </div> 


     <div class="table-responsive" style="margin-right: 10px;margin-left: 10px;margin-bottom: 10px"> 
     <table class="table table-bordered table-hover table-striped" id="recorddata"> 

     <thead class="header"> 
      <tr class="well"> 
       <th style="width: 1px"></th> 
       <th style="font-size: 14px;" >Date</th> 
       <th style="font-size: 14px;">Name</th> 
       <th style="font-size: 14px;">Status</th> 


      </tr> 

     </thead> 
     <tbody > 
      <?php if($result!=null){ 
       foreach($result as $row){?> 
        <tr> 
        <td align="center"><input type="checkbox" id="recs" name="recs[]" value="<?php echo $row->id;?>"/></td> 
       <td style="font-size: 15px;padding-left: 20px;"><?php echo $row->datee;?></td> 
        <td style="font-size: 15px;padding-left: 20px;"><?php echo $row->name;?></td> 
        <td style="font-size: 15px;padding-left: 20px;"><?php echo $row->status;?></td> 
       </tr> 
       <?php } 
       } 
       ?> 



     </tbody> 
     </table><!-- END Table--></div> 

здесь мой JQuery:

<script> 
    $('#recs').click(function() { 
     if($('#recs').is(":checked")){ 
      $('#edit').prop('disabled',false) 
     } 
     else{ 
      $('#edit').prop('disabled',true) 
     } 

    }); 
</script> 

ответ

1

Добавить класс в этой строке

<td align="center"><input type="checkbox" id="recs" name="recs[]" value="<?php echo $row->id;?>"/></td> 

Bind JQuery на события изменения как

$(document).ready(function(){ 
    $('.your_class').click(function(){ 
     // check if checkbox is checked 
     var checked = false; 
     $(".your_class").each(function() { 
      if($(this).is(":checked")) 
      { 
       checked = true; 
      } 
     }); 
     if(checked) 
     { 
      // enable buttons here 

      return; 
     } 
     // disable buttons here 
    }); 
}); 
+0

вы имеете в виду, как это, '<тип = имя входного "галочка" класс = "РИК" = "recs []" value = " id;?>" /> '? –

+0

да, и вставьте его вместо 'your_class' – Viral

+0

спасибо за отличную помощь :) –

2

Вы должны класса вместо использования идентификатора для флажка. Проверьте, установлен ли какой-либо флажок и разрешено ли редактировать, удалить кнопку.

<td align="center"><input type="checkbox" class="recs" name="recs[]" value="<?php echo $row->id;?>"/></td>