Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use datatable with dynamically created table

I have a jsp which onload calls a js in which a table is created dynamically. The situation is that I have never worked on jquery earlier, all I know is javascript.

I made two scenarios:

case one : when I create static table on the jsp page itself it works flawlessly. case two : when I try to load the same table which is created by dynamic javascript it fails.

Case I Working code

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script  src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/createDynamicTable.js"></script>
<script type="text/javascript" src="../js/jquery.dataTables.js"></script>

<script>
$(document).ready(function() {
$('#testTable').dataTable();
}); 

</script>
</head>
<body>


<div id="tableDataDiv">
<table id="testTable">
<thead>                   
<th>h1</th>
<th>h2</th>
<th>h3</th>                   
</thead>


<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
<td>9</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>

Case II Not Working code

jsp code

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script  src="../js/jquery.js"></script>
<script type="text/javascript" src="../js/createDynamicTable.js"></script>
<script type="text/javascript" src="../js/jquery.dataTables.js"></script>

<script>
$(document).ready(function() {
$('#testTable').dataTable();
}); 

</script>
</head>
<body onload="getTableData();">


<div id="tableDataDiv">

</div>
</body>
</html>


js code

function getTableData(){

var tableDataDivObj = document.getElementById("tableDataDiv");

var tableObj = document.createElement("table");
tableObj.id = 'testTable';

// header
var theadObj = document.createElement("thead");

var thObj = document.createElement("th");
thObj.innerHTML = 'h1';
theadObj.appendChild(thObj);

thObj = document.createElement("th");
thObj.innerHTML = 'h2';
theadObj.appendChild(thObj);

thObj = document.createElement("th");
thObj.innerHTML = 'h3';
theadObj.appendChild(thObj);

tableObj.appendChild(theadObj);

// body
var tbodyObj;
var trObj;
var tdObj;
tbodyObj = document.createElement("tbody");

var count = 1;
for(i = 0; i < 3; i++){
trObj = document.createElement("tr");

for(j = 0; j < 3; j++){
tdObj = document.createElement("td");
tdObj.innerHTML = count;
trObj.appendChild(tdObj);
count++;
}

tbodyObj.appendChild(trObj);
}


tableObj.appendChild(tbodyObj);

tableDataDivObj.appendChild(tableObj);
}

Once the table is created dynamically, we append it to the div on the jsp page.

Kindly suggest any modification, suggestions so that I can get this code work. this is just an example I have created of my real application . which involves complete mvc, where table data is retrived from the service methods and dao files. I am able to get the data into the table(I made sure after getting an alert on jsp page). I am not able to use datatable on the id of the table.

Thanks in advance!

like image 345
Gaurav Arya Avatar asked Oct 05 '22 00:10

Gaurav Arya


1 Answers

When your dataTables initialization is run, the table doesn't exist yes. You can fix by moving the $('#testTable').dataTable(); after the table has been initialized.

$(tableObj).dataTable();

at the end of the getTableData function (which should be defined in your $(document).ready callback.

like image 60
Alex M Avatar answered Oct 10 '22 02:10

Alex M