php - Warning "Cannot reinitialise DataTable" when using Symfony2 and Twig -


every time log on system there's dialog box keeps on popping when running dev environment.

datatables warning: table id=dattable - cannot reinitialise datatable.

but whenever run production environment, works fine. how can rid of this?

// controller

public function indexaction() {     $em = $this->getdoctrine()->getmanager();     $po = $em->getrepository('matrixedibundle:editransactiondetail')->finddocs('850');      return $this->render('matrixedibundle:matrix:index.html.twig', array('po' => $po)); } 

// index.html.twig

{% extends 'layout.html.twig' %} {# {% include 'matrixedibundle:matrix:header.html.twig'%} #} {% block body %} <div class="content"> </br>   <table id="dattable"class="table table-bordered table-condensed table-hover">       <thead>         <th colspan="8">edi matrix</th>         <tr>           <th>po numbers</th>           <th>trading partner id</th>           <th>po 855 acknowledgement</th>           <th>po 997 acknowledgement</th>           <th>advance shipment notice</th>           <th>invoice</th>           <th>purchase order change</th>           <th>order status</th>         </tr>       </thead>       <tbody>       {% tran in po %}         <tr>             <td><a href="{{ path('matrix_edi_showpo', {'po_num':  tran.ponumber}) }}"data-toggle="modal"data-target="#mymodal">{{tran.ponumber}}</td>              <td>{{tran.editransaction.senderid}}</td>              <td><a href="{{ path('matrix_edi_findall', {'po_num':  tran.ponumber, 'sender_id':  tran.editransaction.senderid, 'doc_type': 855}) }}"data-toggle="modal"data-target="#mymodal">             {{ render(controller('matrixedibundle:matrix:matrix', {             'po_num': tran.ponumber, 'sender_id': tran.editransaction.senderid, 'reciever_id': tran.    editransaction.receiverid, 'doc_type': 855, 'gs_number': tran.editransaction.gsnumber })) }}</a>             </td>              <td><a href="{{ path('matrix_edi_poack', {'gs_number':  tran.editransaction.gsnumber, 'receiver_id':  tran.editransaction.receiverid, 'sender_id': tran.editransaction.senderid}) }}"data-toggle="modal"data-target="#mymodal">             {{ render(controller('matrixedibundle:matrix:matrix', {             'po_num': tran.ponumber, 'sender_id': tran.editransaction.senderid, 'reciever_id': tran.    editransaction.receiverid, 'doc_type': 997, 'gs_number': tran.editransaction.gsnumber })) }}</a>             </td>              <td><a href="{{ path('matrix_edi_findall', {'po_num':  tran.ponumber, 'sender_id':  tran.editransaction.senderid, 'doc_type': 856}) }}"data-toggle="modal"data-target="#mymodal">{{ render(controller('matrixedibundle:matrix:matrix', {             'po_num': tran.ponumber, 'sender_id': tran.editransaction.senderid, 'reciever_id': tran.editransaction.receiverid, 'doc_type': 856, 'gs_number': tran.editransaction.gsnumber }))}}</a>             </td>              <td><a href="{{ path('matrix_edi_findall', {'po_num':  tran.ponumber, 'sender_id':  tran.editransaction.senderid, 'doc_type': 810}) }}"data-toggle="modal"data-target="#mymodal">{{ render(controller('matrixedibundle:matrix:matrix', {'po_num': tran.ponumber, 'sender_id': tran.editransaction.senderid, 'reciever_id': tran.editransaction.receiverid, 'doc_type': 810, 'gs_number': tran.editransaction.gsnumber})) }}</a>             </td>              <td><a href="{{ path('matrix_edi_findall', {'po_num':  tran.ponumber, 'sender_id':  tran.editransaction.senderid, 'doc_type': 860}) }}"data-toggle="modal"data-target="#mymodal">{{ render(controller('matrixedibundle:matrix:matrix', {'po_num': tran.ponumber, 'sender_id': tran.editransaction.senderid, 'reciever_id': tran.editransaction.receiverid, 'doc_type':860, 'gs_number': tran.editransaction.gsnumber})) }}</a>             </td>            <td><a href="{{ path('matrix_edi_findall', {'po_num':  tran.ponumber, 'sender_id':  tran.editransaction.senderid, 'doc_type': 870}) }}"data-toggle="modal"data-target="#mymodal">{{ render(controller('matrixedibundle:matrix:matrix', {         'po_num': tran.ponumber, 'sender_id': tran.editransaction.senderid, 'reciever_id': tran.editransaction.receiverid, 'doc_type': 870, 'gs_number': tran.editransaction.gsnumber     })) }}</a></td>          </tr>       {% endfor %}     </tbody>     </table>   </div>    <div id="mymodal"class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="mylargemodallabel" aria-hidden="true">   <div class="modal-dialog modal-lg">     <div class="modal-content">       <div class="modal-header">         <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">&times;</span></button>         <h4 class="modal-title">details</h4>       </div>       <div class="modal-body">         loading content......       </div>       <div class="modal-footer">         <button type="button" class="btn btn-primary" data-dismiss="modal">close</button>       </div>     </div><!-- /.modal-content -->   </div><!-- /.modal-dialog --> </div><!-- /.modal --> {% endblock %}  {% block javascripts %} {% javascripts   'bundles/matrixdoc/js/jquery.js'   'bundles/matrixdoc/js/jquery.datatables.min.js'   'bundles/matrixdoc/js/datatables.bootstrap.js'   'bundles/matrixdoc/js/bootstrap.js'    %}   <script src="{{ asset_url }}"></script>   <script type="text/javascript">   $(document).ready(function() {     $('#dattable').datatable( {       "scrolly": "400px",       "scrollcollapse": true,       "pagingtype": "simple",     });     $('body').on('hidden.bs.modal', '.modal', function () {       $(this).removedata('bs.modal');     });     $(document).on("hidden.bs.modal", function (e) {       $(e.target).removedata("bs.modal").find(".modal-content").empty();     });     }); </script> {% endjavascripts %} {% endblock %} 

cause

error cannot reinitialise datatable means datatable initialized more once $('#dattable').datatable() without being destroyed.

solution

you run $('#dattable').datatable() once in code, therefore there must wrong templates. search on stackoverflow confirmed this, see answer "javascript loading twice in symfony".

you need move block containing javascript code {% block javascripts %} ... {% endjavascripts %} outside of body block {% block body %} ... {% endblock %}.


Comments