dataTable seems to be initializing as the css is applied but there is no functionality

dataTable seems to be initializing as the css is applied but there is no functionality

dyetubedyetube Posts: 20Questions: 5Answers: 0

I am populating a page with (possibly) multiple datatables by:

(in the <head> of the HTML document):

  1. Read an external .JSON file.

  2. Get number of columns for the table(s) and the table header(s)/footer(s) names along with column variables.

  3. Build HTML table(s) based off the number of arrays in the the .JSON and append to the HTML document.

  4. Populate the header(s)/footer(s) with the names from the .JSON file thereby setting the number of columns.

  5. Populate the HTML table(s) with the data from the. JSON file.

(in the <body> of the HTML document right before the </body> tag)

  1. Initialize the datatable(s) using the document ready function and append the button div(s) so there is a print button option for the table(s).

I currently have 3 datatables that should populate and be initialized but the script never gets past the first init.
I'm thinking that somehow this is an asynchronous issue but can't figure it out.

Here's the fiddle of my init script: http://jsfiddle.net/9stvn7c3/2/

Here's the fiddle of my Header script: http://jsfiddle.net/fxm7pL4m/

I am running the HTML locally so don't have a server where you can view the page but can upload the files if need be.

Thanks,
Jeff

This question has accepted answers - jump to:

Answers

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    edited November 2015

    Yup. you have an async problem. You can't use document ready as a trigger on init for DT because as far as jquery is concerned, the document is "ready" long before all that ajax data comes back. You'll need to move that DT init into the success event of your last ajax call. I've used semaphores to control this sort of "Wait until everyone is ready" logic. One ajax success sets a "I'm done" flag. When the second ajax completes, it loops until the first flag is true, then it continues and inits Datatables. Of course I'm not using a loop, I'm using setinterval().

  • dyetubedyetube Posts: 20Questions: 5Answers: 0

    So I moved the init to the success: event of the jquery.ajax function in the head and when viewing the console can see that the init occurs after the table is populated with the data ( I used some console.log's to show me plus I stepped through the functions while debugging as well), but my buttons are still not showing nor do I have column sorting or the search box populating. I no longer think it's an async problem but still not sure what it could be...

  • dyetubedyetube Posts: 20Questions: 5Answers: 0

    I'm intializing the table with the var table = $('datatable').Datatable ({});. Then to populate the div with the buttons I use: table.buttons( 0, null ).containers().appendTo( '#buttons' );.

    When I put a breakpoint on the table.buttons, I never reach the breakpoint. For some reason everything stops after the var table = is run.

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    edited November 2015
      $('#example').DataTable();
    

    not

      $('#example').Datatable();
    

    There is an article on why that capital T matters, somewhere.

    You've got this in your code

      "<table id='dataTable" + i + "' class='display'>" + 
    

    So I think you want (for a value i=1)

     $('#dataTable1').DataTable();
    
    
  • dyetubedyetube Posts: 20Questions: 5Answers: 0

    I actually do have it as $('#example').DataTable(); in my code, when I retyped it here, I forgot to use the capital t. One thing I am noticing is that I'm getting a "TypeError: e is undefined" error from the jquery.datatables.min file. I know it's not a error with that file but It's in my code.

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    I forgot to ask the basic question, "Does it work if you remove all the Buttons code?"

  • dyetubedyetube Posts: 20Questions: 5Answers: 0

    Nope. Just tried it and no luck.

  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    post an example of the html out put,

  • dyetubedyetube Posts: 20Questions: 5Answers: 0
  • ThomDThomD Posts: 334Questions: 11Answers: 43
    Answer ✓

    thanks, but I mean, post some of the HTML output from the code, with data.

  • dyetubedyetube Posts: 20Questions: 5Answers: 0

    I'm thinking it's some type of formatting issue. I'm trying to populate the table from the JSON like this: https://jsfiddle.net/vLLcspr7/ Which is where I am having the issue. If I populate like this: https://jsfiddle.net/c41bqtue/ It works just fine.

  • dyetubedyetube Posts: 20Questions: 5Answers: 0

    This issue was a formatting issue. I was adding an extra <td> tag at the end of each line so the formatting was off and throwing an error.

This discussion has been closed.