jquery - 如何正确使用 jQuery DataTables 和 Doctrine 分页器?

标签 jquery symfony doctrine-orm pagination

我需要使用 jQuery DataTables 管理来自 Doctrine Paginator 查询的大量记录的显示。现在,我正在寻找完成此任务的最佳方法。

In terms of performance, it is better to paginate the results at database level, so you can avoid the overhead of mapping the results to objects, decrease the network traffic and your database server wont need to read/filter that many records [...] @Xocoatzin on this comment

我编写了一个 Symfony Controller 操作来从数据库检索帐户的数据:

AccountController

/**
 * Lists all Account entities.
 *
 * @Route("/list/{page}", defaults={"page" = 1}, name="account_index")
 * @Template("account/index.html.twig")
 * @Method("GET")
 */
public function indexAction($page)
{
    $limit = 15;
    $offset = ($page - 1) * $limit;
    $accountRepo = $this->getDoctrine()->getRepository('SupportBundle:Account');
    $query = $accountRepo->createQueryBuilder('a')
             ->setFirstResult($offset)
             ->setMaxResults($limit)
             ->getQuery();
    $paginator = new Paginator($query);
    dump((int )$paginator->count());
    $total = (int) ceil($paginator->count() / $limit);
    dump($total);
    $accounts = $query->getResult();

    return array(
        'accounts' => $accounts,
        'title' => 'Accounts list',
        'paginator' => $paginator->getIterator(),
        'pages' => $total,
        'current_page' => $page
    );
}

为了渲染 View ,我实现了一个简单的 Twig 模板,其中包含填充我的表格的 for 。

我还发现了this gist用于 Symfony2/DataTables 集成。

我正在使用:

  • Symfony 2.8.2
  • jQuery 1.12
  • 数据表 1.10.8

我发现了 Symfony 的几个分页器 bundle ,但我的目的是仅使用 Doctrine 分页器从头开始维护一种简单的方法,这样我将来就能够在不依赖第三方组件的情况下进行更改。我认为对于兼容性和解耦来说也会更好。

Paginator Symfony 组件:

最佳答案

关于jquery - 如何正确使用 jQuery DataTables 和 Doctrine 分页器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35450746/

相关文章:

javascript - 切换 CSS3 淡入淡出?

symfony - 如何在 Symfony (Twig) 中包含可重用的小部件?

mapping - 有没有一种方法可以在配置文件之外修改该学说的实体映射配置?

doctrine-orm - 带有连接表的 Doctrine 2 ManyToOne

javascript - jquery 带有类别的复选框过滤器

javascript - 使用 javascript 或 jquery 根据表头列对表行进行排序

symfony - Behat 验证 Symfony2 用户

symfony - sonata_type_model/symfony 形成一对多关系不保存

mysql - Doctrine2 Criteria() 生成错误的 MySQL 查询

javascript - jquery随机动画div(放大和缩小)