[newbie]0 Document and 0Table errors instead of rendering

[newbie]0 Document and 0Table errors instead of rendering

nm_dennisnm_dennis Posts: 4Questions: 1Answers: 0

I'm just getting started with DataTables and not getting it to work as expected. I tried finding in the forums without success.

This is for a large table -- not quite 10K rows but for now, am using a smaller subset of around 400 rows.

Head section: I'm including the lines

  <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
  <script type="text/javascript" language="javascript" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

My table definition is:

  <table id="table1"cellspacing="0">

and I've tried initializing in both the head section and after the table in the body section with:

  <script type="text/javascript" class="init">
    $(document).ready(function () {
      $('table1').DataTable();
    });\n
  </script>

The table is rendered but without DataTable at all and regardless of whether the initialization is in the header or at the end of the body, I'm getting

  <script type="text/javascript" class="init">
  0 0document).ready(function () {
      0 0'table1').DataTable();
  });
  </script>

Any help would be much appreciated to get me started.

This question has an accepted answers - jump to answer

Answers

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

    Your table has id="table1 so your selector to initialize Datatables needs to use jQuery ID selector. Something like this $('#table1').DataTable();.

    Kevin

  • nm_dennisnm_dennis Posts: 4Questions: 1Answers: 0

    Unfortunately, still a no-go. I put the init in the head section of the page and changed it to use the jquery ID selector (new to jquery as well).

    I'll put together an example as soon as possible and re-post.

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

    Not sure how you are populating the table. If you are directly crating the HTML with the table data then you need to make sure this is complete before initializing Datatables. Otherwise Datatables will find an empty table when initialized.

    You also need to have a thead defined as shown in the Installation docs. If this doesn't help then please provide a link to a test case so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • nm_dennisnm_dennis Posts: 4Questions: 1Answers: 0

    So, it appears to be a problem on my end although I haven't found it as of yet.

    • The HTML page is created using a perl script every 15 minutes and then accessed via web browser.
    • If copy the correct part for the init section into the web page, loading the page via web browser works without an issue.
    • I do have a thead and tbody section. I had seen that in the demos and it was easy to add, so just added it.

    Thanks for pointing out the #table1 part, that would've definitely been a problem.

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

    using a perl scri

    Is it possible that the $ is being interpreted by Perl!? Try replacing the $ in your Javascript with jQuery - I've got a feeling it will then work.

    If that is the case then you should look into escaping the $ in the Perl script to get it to just leave it alone :)

    Allan

  • nm_dennisnm_dennis Posts: 4Questions: 1Answers: 0

    Found it: Stupid developer (me) tricks.

    In perl, $ indicates a scalar variable (single value), so you need to escape them if. I had completely forgotten to do so. Add \ before each of the $ and all is good now.

Sign In or Register to comment.