我尝试在symfony 2.7中添加数据表的服务器端处理。
我有我的js:
{% set jsonOptionsDataBases %}
{
"processing": true,
"serverSide": true,
"ajax": "{{ path("surveillance_bo_dossier_home_tableau",{"type": 0, "client":client}) }}",
"sAjaxDataProp": "data",
"pageLength": 100,
"columns": [
{data: "client"},
{data: "dossier"},
{data: "type"},
{data: "dateD"},
{data: "dateF"},
{data: "analyse"},
{data: "frequence"},
{data: "cc"},
{data: "dateR"},
{data: "action"},
{data: "jur"},
{data: "isActif"},
]
{% endset %}
{{ macro_datatables.datatablesInit('dossiers','dossiers',jsonOptionsDataBases,a_colWithSelect,a_colWithDate,a_colEmpty, a_colWithSelectOtherColumn, a_colWithDateUs) }}
宏生成我的表。
我的控制器:
$users = $Surv->getRepository('t:s\Dossier')->ServerSide($filters, 0, $length);
$output = array(
'data' => array(),
'recordsFiltered' => $I_nbResultatsTotal,
'recordsTotal' => $I_nbResultatsTotal,
);
$i = 0;
foreach ($users as $O_dossier) {
$I_idDossier = $O_dossier->getId();
$I_idRapportLast = 0;
$output['data'][] = [
'client' => 1,
'dossier' => 1,
'type' => 1,
'dateD' => 1,
'dateF' =>1,
'analyse' => 1,
'frequence' => 1,
'dateR' => 1,
'action' => 1,
'cc' =>1,
'jur' => 1,
'isActif' => 1
];
$I_nbResultatsTotal++;
}
return new Response(json_encode($output), 200, ['Content-Type' => 'application/json']);
和函数服务器端:
public function ServerSide($data, $page = 0, $max = NULL, $getResult = true)
{
$qb = $this->_em->createQueryBuilder();
$query = isset($data['query']) && $data['query']?$data['query']:null;
$qb
->select('u')
->from('t:s\Dossier', 'u')
;
if ($query) {
$qb
->andWhere('u.name like :query')
->setParameter('query', "%".$query."%")
;
}
if ($max) {
$preparedQuery = $qb->getQuery()
->setMaxResults($max)
->setFirstResult($page * $max)
;
} else {
$preparedQuery = $qb->getQuery();
}
return $getResult?$preparedQuery->getResult():$preparedQuery;
}
我有137个结果,所以选项“pagelength”我有两个页面,问题是结果计数,我在一页上有137个结果,在我的第一页我需要100个结果,并且我知道了为什么我在这里得到了所有的结果。