简体   繁体   English

Cakephp中的jQuery Datatables可点击行

[英]jQuery Datatables clickable row in cakephp

I am using jQuery Datatables in a custom cakePHP script - due to the facts that the information in my tables are quite long I would like to reduce my table a bit by removing the last column (view / edit) and make the entire row clickable. 我在自定义的CakePHP脚本中使用jQuery Datatables-由于事实是我表中的信息很长,我想通过删除最后一列(查看/编辑)并使整个行可单击来减少我的表。

Here my current code: 这是我当前的代码:

    <table class="responsive_table" id="quotes_table">
        <thead>
            <tr>
                <th>Date</th>
                <th>Client</th>
                <th>Project</th>
                <th class="center">Total</th>
                <th class="center">Status</th>
                <th class="center no_sort">View/Edit</th>
            </tr>
        </thead>
        <?php foreach($quotes as $quote) : ?>
            <tr>
                <td><?php echo $quote['Quote']['quote_date']?></td>
                <td ><?php echo $quote['Client']['client_name']?></td>
                <td><?php echo $quote['Quote']['project']?></td>
                <td class="right"><?php echo $currency. number_format($quote['Quote']['quote_total'], 2, '.', ',')?></td>
                <td class="quote_status"><?php echo $quote['Quote']['status']?></td>
                <td class="center small_cell">
                <?php echo $this->Html->link('View/Edit', array('action' => 'view', $quote['Quote']['id'], '?' => array('nocache' => time())), array('class' => 'view')); ?>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>

    <?php echo $this->Html->script('jquery.dataTables');?>
    <script type="text/javascript">
        if ( $('#quotes_table')[0] ) { 
            $('#quotes_table').dataTable( {
            "aaSorting": [[ 0, "desc" ]],
            "sDom": '<"top"f>irt<"bottom"lp><"clear">',
            "oLanguage": {
             "sSearch": "Search (Auto Filter):"},
             "fnDrawCallback": function( oSettings ) { updateClasses(); }
            } 
        ); }
    </script>

Please also note that my edit link contains some sort of array. 另请注意,我的编辑链接包含某种数组。 What is the best way to have this working? 进行此工作的最佳方法是什么?

As the cakephp link() method is hard to penetrate, you can leave it in place and process the table rows client-side - hide the "view/edit" cell and trigger its href when its table row is clicked. 由于cakephp link()方法难以渗透,因此您可以将其放置在适当的位置并在客户端处理表行-隐藏“查看/编辑”单元格,并在单击表行时触发其href。

First, to make jQuery selection simpler, wrap the table rows in a <tbody>...</tbody> : 首先,为了简化jQuery选择,将表行包装在<tbody>...</tbody>

</thead>
<tbody>
<?php foreach($quotes as $quote) : ?>
    ...
<?php endforeach; ?>
</tbody>

Then chain some extra jQuery to your existing $('#quotes_table').dataTable({...}) block. 然后将一些额外的jQuery链接到您现有的$('#quotes_table').dataTable({...})块。

$('#quotes_table').dataTable({
    "aaSorting": [[ 0, "desc" ]],
    "sDom": '<"top"f>irt<"bottom"lp><"clear">',
    "oLanguage": {
        "sSearch": "Search (Auto Filter):"
    },
    "fnDrawCallback": function( oSettings ) { updateClasses(); }
}).find("tbody tr").each(function() {
    var $a = $(this).find("a.view"); //find view/edit anchor 
    $a.closest("td").hide(); //hide view/edit cell
    $(this).on('click', function() { //on clicking the table row, trigger a click on the anchor. 
        $a.trigger('click');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM