[newbie]0 Document and 0Table errors instead of rendering
[newbie]0 Document and 0Table errors instead of rendering
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
Your
table
hasid="table1
so your selector to initialize Datatables needs to use jQuery ID selector. Something like this$('#table1').DataTable();
.Kevin
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.
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
So, it appears to be a problem on my end although I haven't found it as of yet.
Thanks for pointing out the #table1 part, that would've definitely been a problem.
Is it possible that the
$
is being interpreted by Perl!? Try replacing the$
in your Javascript withjQuery
- 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 aloneAllan
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.