为什么不是'我的datatables.js上的搜索函数正在工作

Why isn't the search function on my datatables.js working?

本文关键字:搜索 函数 工作 js datatables 我的 为什么不      更新时间:2023-09-26

我正在制作一个CakePHP 2.x应用程序,并按照网站上的说明安装了datatables.js,但尽管它在那里,但有些功能不起作用。我无法更改要显示的条目数量,搜索功能总是不返回任何内容,也不会将数据拆分为多个页面(例如,如果我将其设置为每页显示10个条目,即使超过10个,它仍然会在一个页面上显示所有条目)。

唯一有效的部分是排序功能和它的视觉方面

以下是我在上面实现数据表的视图:

<div class="products index">
    <h2><?php echo __('Products'); ?></h2>
    <table cellpadding="0" cellspacing="0" id="prod-tbl">
        <thead>
            <head>
                <!--<script src="jquery-1.11.1.min.js"></script>-->
                <!-- DataTables CSS -->
                <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.3/css/jquery.dataTables.css">
                <!-- jQuery -->
                <script type="text/javascript" charset="utf8" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
                <!-- DataTables -->
                <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.3/js/jquery.dataTables.js"></script>
                <script >$(document).ready( function () {$('#prod-tbl').DataTable();} );</script>
            </head>
            <tr>
                <th><?php echo $this->Paginator->sort('name'); ?></th>
                <th><?php echo $this->Paginator->sort('product_code'); ?></th>
                <th><?php echo $this->Paginator->sort('image_name'); ?></th>
                <th><?php echo $this->Paginator->sort('image_url'); ?></th>
                <!--<th><?php echo $this->Paginator->sort('brand_id'); ?></th>
                <th><?php echo $this->Paginator->sort('reward_id'); ?></th>-->
                <th><?php echo $this->Paginator->sort('product_status_id'); ?></th>
                <th><?php echo $this->Paginator->sort('serial_id'); ?></th>
                <th><?php echo $this->Paginator->sort('category_id'); ?></th>
                <th class="actions"><?php echo __('Actions'); ?></th>
            </tr>
        </thead>
    <?php foreach ($products as $product): ?>
    <tbody>
        <tr>
            <td><?php echo h($product['Product']['name']); ?>&nbsp;</td>
            <td><?php echo h($product['Product']['product_code']); ?>&nbsp;</td>
            <td><?php echo h($product['Product']['image_name']); ?>&nbsp;</td>
            <td><?php echo h($product['Product']['image_url']); ?>&nbsp;</td>
            <!--<td><?php echo h($product['Product']['brand_id']); ?>&nbsp;</td>
            <td><?php echo h($product['Product']['reward_id']); ?>&nbsp;</td>-->
            <td><?php echo h($product['Product']['product_status_id']); ?>&nbsp;</td>
            <td><?php echo h($product['Product']['serial_id']); ?>&nbsp;</td>
            <td><?php echo h($product['Product']['category_id']); ?>&nbsp;</td>
            <td class="actions">
                <?php echo $this->Html->link(__('View'), array('action' => 'view', $product['Product']['id'])); ?>
                <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $product['Product']['id'])); ?>
                <?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $product['Product']['id']), null, __('Are you sure you want to delete # %s?', $product['Product']['id'])); ?>
            </td>
        </tr>
    </tbody>
<?php endforeach; ?>
    </table>
    <p>
    <?php
    echo $this->Paginator->counter(array(
    'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {: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>
<div class="actions">
    <h3><?php echo __('Actions'); ?></h3>
    <ul>
        <li><?php echo $this->Html->link(__('New Product'), array('action' => 'add')); ?></li>
    </ul>
</div>

我猜您正试图在CakePHP分页页面中包含datatable.js。

datatable.js和CakePHP分页器都是过滤数据的两种不同方法。CakePaginator在服务器中执行所有类型的过滤,并且只在表中显示结果数据。Datatable.js在客户端(javascript)中工作。

当您在分页表上应用datatable.js时,它将只过滤结果数据,而不是整个数据。

您可以使用以下2种方法中的任何一种

  1. 利用数据表服务器端处理(Matt Hughes在这里为CakePHP编写了一份很好的文档https://datatables.net/development/server-side/php_cake)
  2. 使用CakePHP数据表组件。(一个喜欢https://github.com/cnizzdotcom/cakephp-datatable)