Bypassing or leaving tables intact in case of an error

Bypassing or leaving tables intact in case of an error

victomvictom Posts: 2Questions: 1Answers: 0

We are using DataTables to format our regular tables. In some cases, DataTables breaks our more complicated tables and renders them unusable.

Is there a way to somehow bypass the DataTables formatting for a table with errors and leave it intact as in the HTML source? I would like to apply DataTables only for valid HTML tables.

Thanks
Vic

Answers

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    I don't believe there is anything in Datatables that will let reverting back to the orginal HTML table if there is an error during processing. There is the destroy() API that will revert back to an HTML table but under error conditions this might still corrupt the table as its unpredictable what data/information Datatables has processed from the table.

    I'm not sure what types of errors you get or what you mean by complicated table but possibly you could put checks in place for known error conditions and not init Datatables if. any of the error conditions are found.

    Kevin

  • victomvictom Posts: 2Questions: 1Answers: 0

    That is precisely what I want to do. Check before init if the table has any errors. And apply DataTables formatting only when there is no error. Is there any method to achieve this?

    I am trying with something like this, but it doesn't work.

    $('#example').on( 'error.dt', function ( e, settings, techNote, message ) { console.log( 'An error has been reported by DataTables: ', message ); } )

    Thanks
    vic

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

    I don't really understand what you mean by an error in the HTML. Why would you have invalid HTML at all?

    Is it some specific HTML that isn't supported by DataTables and that results in DataTables throwing an error? If so, then what is the HTML? You would test for that.

    Allan

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited February 22

    When you say it doesn't work are you saying the event isn't firing? If you are using Datatables 2.0 the error event has been renamed to dt-error. That could be why.

    I put together a simple example that uses destroy() if there is an error. I removed the first cell of the first row in the HTML table.
    https://live.datatables.net/gokipaba/1/edit

    Datatables has errored on the first row so when it destroys the table it has only the info for the first row so that is what's displayed. I mentioned this might happen above.

    However you can place your own code in the dt-error event to rebuild the HTML table.

    What I meant is to do some checks of the HTML table before initializing Datatables. What these checks need to be is based on knowing what issues you have with the complicated tables that fail.

    Maybe you can come up with a technique to perform a dry run of the initialization. Maybe clone the HTML table into a hidden div. Initialize the original HTML table. If dt-error fires then destroy() and remove the original table from the DOM and display the cloned table.

    Kevin

Sign In or Register to comment.