如何在html表中获取记录的id?

时间:2015-10-10 04:52:13

标签: jquery cakephp-2.0

我有一个包含数据库记录的表。我尝试使用JQuery获取这些寄存器的id,但我不知道如何做到这一点。

我怎么能这样做?

<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 as $empresa): ?>
                                        <tr>
                                                <td><?php echo h($empresa['Empresa']['id']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['nomeFantasia']); ?>&nbsp;</td>                                                
                                                <td><?php echo h($empresa['Empresa']['cnpj']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['telefone1']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['telefone2']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['celular']); ?>&nbsp;</td>                                                
                                                <td><?php echo h($empresa['Empresa']['aberto'] == 1 ? "Sim" : "Não"); ?>&nbsp;</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>

1 个答案:

答案 0 :(得分:0)

解决了这个问题。我在$this->Html->link的id中添加了注册ID,并且工作正常。

解决方案

<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 as $empresa): ?>
                                        <tr>
                                                <td><?php echo h($empresa['Empresa']['id']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['nomeFantasia']); ?>&nbsp;</td>                                                
                                                <td><?php echo h($empresa['Empresa']['cnpj']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['telefone1']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['telefone2']); ?>&nbsp;</td>
                                                <td><?php echo h($empresa['Empresa']['celular']); ?>&nbsp;</td>                                                
                                                <td><?php echo h($empresa['Empresa']['aberto'] == 1 ? "Sim" : "Não"); ?>&nbsp;</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>