symfony从datatable获取请求数据

时间:2017-09-05 07:09:22

标签: symfony datatable

我有一个数据表,他在我的控制器中调用ajax并创建这样的数据表:

$max = 10;
$page = 1;
        $Surv = $this->getDoctrine()->getManager('surveillance');
    $users = $Surv->getRepository('t:Surveillance\Dossier')->ServerSide($page, $max);

    $output = array(
        'data' => array(),
        'recordsFiltered' => count($Surv->getRepository('t:Surveillance\Dossier')->ServerSide(0, false)),
        'recordsTotal' => count($Surv->getRepository('t:Surveillance\Dossier')->ServerSide(0, false))
    );

    foreach ($users as $O_dossier) {
        $I_idDossier = $O_dossier->getId();
        $I_idRapportLast = 1;
        $output['data'][] = [
            'multi' => "<input type='checkbox' value='".$O_dossier->getId()."' class='case-$type'/>",
            'client' => $this->_O_dossiersM->setClientForBoHomeDatatableAjax($O_dossier->getClient(), $this->get('router')),
            'dossier' => $this->_O_dossiersM->setDossierForBoHomeDatatableAjax($O_dossier, $this->get('router')),
            'type' => $O_dossier->getType()->getNom(),
            'dateD' => $this->_O_dossiersM->setDateForDatatableAjax($O_dossier->getDateDebutAt(), 'Y-m-d', $this->_s),
            'dateF' => $this->_O_dossiersM->setDateForDatatableAjax($O_dossier->getDateFinAt(), 'Y-m-d', $this->_s),
            'analyse' => $O_dossier->getAnalyse()->getType(),
            'frequence' => $this->_O_dossiersM->setFrequenceForBoHomeDatatableAjax($O_dossier->getFrequence()),
            'dateR' => $this->_O_dossiersM->setDateLastRapportForBoHomeDatatableAjax($O_dossier, $A_listeDossierWithDateLastRapport, 'Y-m-d', $this->_s),
            'action' => $this->render('m:TemplateForAjax:tableauBoDossierTdAction.html.twig', array('O_dossier' => $O_dossier, 'I_idRapportLast' => $I_idRapportLast))->getContent(),
            'cc' => $this->_O_clientsM->getCcPrenomNomByClient($O_dossier->getClient()),
            'jur' => $O_dossier->getJuriste()->getNom(),
            'isActif' => $i++//($O_dossier->getIsActif()) ? 'active' : 'inactive'
        ];
    }

    return new Response(json_encode($output), 200, ['Content-Type' => 'application/json']);

对于$ max和$ page,我需要获取数据表定义的结果和页面。 例如,如果我点击页面&#34; 2&#34;在我的数据表上,我想得到这样的数据:

$request->request('page');

这是怎么做到的?

$ request-&gt; request-all()返回null ...

我的阿贾克斯:

$(function() {
    $('#user-list').DataTable({
        "processing": true,
        "serverSide": true,
        "pageLength": 10,
        "lengthMenu": [[5,10, 25, 50, -1], [5,10, 25, 50, 'All']],
        "ajax": "{{  path("surveillance_bo_dossier_home_tableau",{"type": 0, "client":client}) }}",
        "sAjaxDataProp": "data",
        "columns":[
            {data: "multi"},
            {data: "client"},
            {data: "dossier"},
            {data: "type"},
            {data: "dateD"},
            {data: "dateF"},
            {data: "analyse"},
            {data: "frequence"},
            {data: "cc"},
            {data: "dateR"},
            {data: "action"},
            {data: "jur"},
            {data: "isActif"},
        ]
    });
});

<table id="user-list">
        <thead>
        <tr>
            <th>multi</th>
            <th>client</th>
            <th>dossier</th>
            <th>type</th>
            <th>dateD</th>
            <th>dateF</th>
            <th>analyse</th>
            <th>frequence</th>
            <th>cc</th>
            <th>dateR</th>
            <th>action</th>
            <th>jur</th>
            <th>isActif</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

2 个答案:

答案 0 :(得分:0)

你能告诉我你从datatable调用你的javascript吗?

因为dadatable不需要获取页码,所以你可以得到一个json响应,它会加载你的数据。

&#13;
&#13;
$(document).ready(function() {
    $('#example').DataTable( {
        "ajax": '../ajax/data/arrays.txt'
    } );
} );
&#13;
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>


<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Extn.</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </tfoot>
    </table>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

确定我的问题已经解决了。 获取数据:$ request-&gt; get('start')和$ request-&gt; get('length')我需要添加我的ajax类型帖子:

"ajax": {
            "url": "{{ path("surveillance_bo_dossier_home_tableau",{"type": 0, "client":client}) }}",
            "type": "POST"
        },

现在我可以从我的数据表中获取数据