jquery - How to get the id of a record in a html table? -
i have table contain records of database. i'm trying obtain id of these registers using jquery dont know how this.
how ?
<script type="text/javascript"> $(document).ready(function(){ $('#btnabrirempresa').click(function(){ //id register var id = $(this).closest('tr').attr('[empresa][id]'); alert(id); var loading = $(".imageloading"); $(document).ajaxstart(function () { loading.show(); }); $(document).ajaxstop(function () { loading.hide(); }); $.ajax({ accepts: {json: 'application/json'}, datatype:'json', type: "post", url: "<?php echo $this->html->url("/empresas/openempresa.json")?>", data: {"id":id}, success: function( data ){ console.log(data); }, error: function (xhr, status) { $(".message").html('error: ' + status); } }); return false; }); }); </script> <div class="row"> <div class="col-lg-12"> <h1 class="page-header">empresa</h1> <!--loading--> <div class="imageloading" style="display: none"> <?php echo $this->html->image("ajax-loader.gif", array("height"=>"32", "width"=>"32"));?> </div> <!--/loading--> <span><?php echo $this->html->link(__('novo'), array('action' => 'add')); ?></span> <div class="panel panel-default"> <!-- /.panel-heading --> <div class="panel-body"> <div class="datatable_wrapper"> <table class="table table-striped table-bordered table-hover" id="datatables-example"> <thead> <th><?php echo $this->paginator->sort('id'); ?></th> <th><?php echo $this->paginator->sort('nomefantasia'); ?></th> <th><?php echo $this->paginator->sort('cnpj'); ?></th> <th><?php echo $this->paginator->sort('telefone1'); ?></th> <th><?php echo $this->paginator->sort('telefone2'); ?></th> <th><?php echo $this->paginator->sort('celular'); ?></th> <th><?php echo $this->paginator->sort('aberto'); ?></th> <th class="actions"><?php echo __('actions'); ?></th> </thead> <tbody> <?php foreach ($empresas $empresa): ?> <tr> <td><?php echo h($empresa['empresa']['id']); ?> </td> <td><?php echo h($empresa['empresa']['nomefantasia']); ?> </td> <td><?php echo h($empresa['empresa']['cnpj']); ?> </td> <td><?php echo h($empresa['empresa']['telefone1']); ?> </td> <td><?php echo h($empresa['empresa']['telefone2']); ?> </td> <td><?php echo h($empresa['empresa']['celular']); ?> </td> <td><?php echo h($empresa['empresa']['aberto'] == 1 ? "sim" : "não"); ?> </td> <td class="actions"> <?php echo $this->html->link('<i class="glyphicon glyphicon-open"></i>', array(), array("id"=>"btnabrirempresa", 'title'=>'abrir empresa', 'escape' => false)); ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <p> <?php echo $this->paginator->counter(array( 'format' => __('página {:page} de {:pages}, exibindo {:current} registro de {:count}, início {:start}, final {:end}') )); ?> </p> <div class="paging"> <?php echo $this->paginator->prev('< ' . __(' previous '), array(), null, array('class' => 'prev disabled')); echo $this->paginator->numbers(array('separator' => '')); echo $this->paginator->next(__(' next ') . ' >', array(), null, array('class' => 'next disabled')); ?> </div> </div> <!-- /.panel-body --> </div> <!-- /.panel --> </div> <!-- /.col-lg-12 --> </div> </div>
solved problem. added register id in id of $this->html->link
, works fine.
solution
<script type="text/javascript"> $(document).on('click', '.btnabrirempresa', function(e){ e.preventdefault(); var pai = $(this).closest('tr'); var id = this.id; var loading = $(".imageloading"); $(document).ajaxstart(function () { loading.show(); }); $(document).ajaxstop(function () { loading.hide(); }); $.ajax({ accepts: {json: 'application/json'}, datatype:'json', type: "post", url: "<?php echo $this->html->url("/empresas/openempresa.json")?>", data: {id:id}, success: function( data ){ console.log(data); }, error: function (xhr, status) { $(".message").html('error: ' + status); } }); return false; }); </script> <div class="row"> <div class="col-lg-12"> <h1 class="page-header">empresa</h1> <!--loading--> <div class="imageloading" style="display: none"> <?php echo $this->html->image("ajax-loader.gif", array("height"=>"32", "width"=>"32"));?> </div> <!--/loading--> <span><?php echo $this->html->link(__('novo'), array('action' => 'add')); ?></span> <div class="panel panel-default"> <!-- /.panel-heading --> <div class="panel-body"> <div class="datatable_wrapper"> <table class="table table-striped table-bordered table-hover" id="datatables-example"> <thead> <th><?php echo $this->paginator->sort('id'); ?></th> <th><?php echo $this->paginator->sort('nomefantasia'); ?></th> <th><?php echo $this->paginator->sort('cnpj'); ?></th> <th><?php echo $this->paginator->sort('telefone1'); ?></th> <th><?php echo $this->paginator->sort('telefone2'); ?></th> <th><?php echo $this->paginator->sort('celular'); ?></th> <th><?php echo $this->paginator->sort('aberto'); ?></th> <th class="actions"><?php echo __('actions'); ?></th> </thead> <tbody> <?php foreach ($empresas $empresa): ?> <tr> <td><?php echo h($empresa['empresa']['id']); ?> </td> <td><?php echo h($empresa['empresa']['nomefantasia']); ?> </td> <td><?php echo h($empresa['empresa']['cnpj']); ?> </td> <td><?php echo h($empresa['empresa']['telefone1']); ?> </td> <td><?php echo h($empresa['empresa']['telefone2']); ?> </td> <td><?php echo h($empresa['empresa']['celular']); ?> </td> <td><?php echo h($empresa['empresa']['aberto'] == 1 ? "sim" : "não"); ?> </td> <td class="actions"> <?php echo $this->html->link('<i class="glyphicon glyphicon-open"></i>', array(), array("id"=>$empresa['empresa']['id'], 'class'=>'btnabrirempresa', 'title'=>'abrir empresa', 'escape' => false)); ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <p> <?php echo $this->paginator->counter(array( 'format' => __('página {:page} de {:pages}, exibindo {:current} registro de {:count}, início {:start}, final {:end}') )); ?> </p> <div class="paging"> <?php echo $this->paginator->prev('< ' . __(' previous '), array(), null, array('class' => 'prev disabled')); echo $this->paginator->numbers(array('separator' => '')); echo $this->paginator->next(__(' next ') . ' >', array(), null, array('class' => 'next disabled')); ?> </div> </div> <!-- /.panel-body --> </div> <!-- /.panel --> </div> <!-- /.col-lg-12 --> </div> </div>
Comments
Post a Comment