Mixing HTML initialization with server side refreshes

Mixing HTML initialization with server side refreshes

bruce808bruce808 Posts: 5Questions: 0Answers: 0
edited September 2010 in General
Hi Allan,

I like that the system is based on progressive enhancement and what I'm trying to do is generate the HTML table on the initial load (so it gets indexed in search results) and then do subsequent refreshes via Ajax. I've got it working that way however it seems a little contrived -- I set bServerSide and fnServerData and then ignore the initial call to the server triggered by the initialization. Is there a better way to do this?

Thanks!
Bruce

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Bruce,

    That sounds a little complicated :-). Have you tried simply having a basic HTML table (perhaps with the first 10 results) rendered on page load (i.e. no Javascript) and then initialising DataTables with server-side processing? DataTables will remove the rows for the original table (basically discard them) and draw in the new information brought in by Ajax.

    Allan
  • bruce808bruce808 Posts: 5Questions: 0Answers: 0
    Hi Allan,

    Yes, I thought of that but I wanted to avoid the extra round-trip and processing for the DataTables initialization server-side load. It seems redundant since the initial HTML layout looks exactly like the results produced by DataTables. I looked through the code a bit and it seems all that would be required would be an initialization flag telling DataTables to just add its formatting and events but not call the fnServerData function. This way DataTables would be initialized, the current HTML would still be displayed, and there wouldn't be the extra data load.

    I wouldn't bother doing all the extra work if getting indexed by the search engines wasn't so important. The reason I'm switching to DataTables is because of the in-place progressive enhancement so I can have a fully formatted table for non-javascript users (search engines) while still adding all the cool Ajax/javascript features for normal users.

    Awesome job though... really clean implementation.

    Best
    Bruce
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi Bruce,

    Ah yes I see what you mean. I hadn't considered the XHR at page load, which as you rightly point out in this situation is effectively redundant. There is currently no easy option to avoid this extra XHR I'm afraid. The cleanest method would probably be to put a catch into DataTables which will stop the Ajax load function from doing anything when the draw count is 1 (first draw). This is certainly something I'll think about adding in future - I'm 100% for progressive enhancement!

    Regards,
    Allan
  • carteriiicarteriii Posts: 25Questions: 1Answers: 0
    Hey Alan, I was just looking to see if I could hack this into datatables myself. I was looking at this particular line of _fnDraw:

    if ( oSettings.oFeatures.bServerSide && !_fnAjaxUpdate( oSettings ) )

    I thought it might be as simple as adding an extra clause to that of && oSettings.iDraw > 0, but I'm uncertain of the consequences of doing that. Looks like if we simply had another case there for the first time, that might let us really have the progressive enhancement and avoid the unnecessary first XHR call.

    Am I on the right track? If you were to add an additional setting for this, is that where you'd try to put the check for the setting?
  • srayner02srayner02 Posts: 15Questions: 0Answers: 0
    I too would like to do this.
    It sounds simple enough to add an extra check to see if this is the first draw. Any thoughts on carteriii's sugestion of modifying _fnDraw?
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    I would have sworn that there was a discussion about this recently when we came to a conclusion about it, but I can't find it (that's the problem with having 2700+ threads now...). Having said that I did find this thread from a while back: http://datatables.net/forums/comments.php?DiscussionID=314 . There are two issues to be aware of with this:

    1. The table doesn't have all of the information needed at load time, so the information element will be wrong

    2. For complete progressive enhancement you'll need to provide pagination for users without Javascript so they can access all records.

    Allan
This discussion has been closed.