How to delay initialization of Datatables?

How to delay initialization of Datatables?

PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
edited August 2010 in General
I tried wrapping the initialization code as follows:


function initTable() {
$(document).ready(function() {
incidentReportsTable =
$('#incidentReportsTable').dataTable( {
"bProcessing": true,
"sPaginationType": "full_numbers",
"bFilter": false,
"sAjaxSource": 'myurl'
}

}

setTimeout (initTable,1000);

The idea is to make the Ajax call after the page has been loaded, to avoid this error:
java.lang.IllegalStateException: getOutputStream() has already been called for this response

However, my approach isn't working. Any ideas?

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    $(document).ready... should make the initialisation there load when the DOM is ready. Where are you getting that error from - it shouldn't be the browser if you've got that setup... :-)

    Allan
  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    Hi Allan,

    I thought that if I wrapped the call to $document.ready within another Javascript function, and then called that function from setTimeout(), that would delay the call to $document.ready.

    I think the real problem was that on the server side (Java under Spring 3 MVC) the server was trying to compile JSPX code into HTML and write out the JSON at the same time. I figured out how to write out the data for the Datatables component directly to JSPX using the JSTL tags, eliminating the need for the Ajax call to get the data.

    Paolo
  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    I may have to switch back to using Ajax to retreive data from the server.

    What is the setting/code to put into my $(document).ready() so that it does not attempt to load the DOM until the page is fully rendered?
  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    I tried moving the $(document).ready() that initializes Datatables to an onLoad() function. I still got the same error - caused by Java and Datatables trying to render at the same time:

    SEVERE: Servlet.service() for servlet jsp threw exception
    java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:610)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:273)
    at java.io.PrintWriter.write(PrintWriter.java:382)

    I'll try a button or something that the user has to click before an Ajax call is made.
  • PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
    The problem is Spring MVC on the server side returns the JSON data in an HttpReponse object while the page might still be rendering from an earlier response from the server. THe exception happens when both try to access the same output stream.

    I think I have to find a way to not allow Datatables to make that getJson call right away. There's got to be a way to just trigger the call off of a button click or something.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    One way of doing it is to initialise DataTables with no data in the table, and then make a $.getJSON() call yourself to get the information from the server and place it into the table (using fnAddData). You can trigger the $.getJSON() off a button click or whatever.

    Seems very odd the $(document).ready() isn't good enough - the DOM should be ready when that fires - unless Spring is loading other stuff via Ajax I suppose...

    Allan
This discussion has been closed.