简体   繁体   English

为什么datatables.js上的搜索功能无法正常工作?

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

I'm making a CakePHP 2.x app and installed datatables.js following the instructions on the website, but although it's there some of the features aren't working. 我正在按照网站上的说明制作一个CakePHP 2.x应用程序并安装了datatables.js,但是尽管有,某些功能却无法使用。 I can't change the amount of entries to display, the search function always returns nothing and it won't split the data into multiple pages (eg if I have it set to display 10 entries per page, it'll still display them all on one page even if it's exceeding 10). 我无法更改要显示的条目数,搜索功能始终不返回任何内容,也不会将数据拆分为多个页面(例如,如果我将其设置为每页显示10个条目,它将仍然显示所有内容在一页上,即使超过10页也是如此。

The only parts that work are the sorting features and the visual aspect of it. 唯一起作用的部分是排序功能及其外观。

Here's a view that I've implemented datatables on: 这是我已在其上实现数据表的视图:

<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>

I guess you are trying to include datatable.js in a CakePHP paginated page. 我猜您正在尝试在CakePHP分页中包含datatable.js。

Both datatable.js and CakePHP Paginator are two different methods to filter out data. datatable.js和CakePHP Paginator都是两种不同的数据过滤方法。 Cake Paginator does all types of filtering in server and displays only resultant data in the table. Cake Paginator在服务器中执行所有类型的过滤,并且仅在表中显示结果数据。 Datatable.js works in client(javascript). Datatable.js可在客户端(javascript)中使用。

When you apply datatable.js over paginated table, it will filter the resultant data only, not the entire data. 当在分页表上应用datatable.js时,它将仅过滤结果数据,而不是整个数据。

You can use any of the following 2 methods 您可以使用以下两种方法中的任何一种

  1. Make use of datatable server side processing (Matt Hughes has written a good documentation for CakePHP here https://datatables.net/development/server-side/php_cake ) 利用数据表服务器端处理(Matt Hughes在此处为CakePHP编写了一个很好的文档, 网址https://datatables.net/development/server-side/php_cake
  2. Use a CakePHP datatable component. 使用CakePHP数据表组件。 (One like https://github.com/cnizzdotcom/cakephp-datatable ) (一个像https://github.com/cnizzdotcom/cakephp-datatable

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

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