0

I have a tab panel on my html where I am rendering a table.

<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<div role="tabpanel" class="tab-pane" id="manualProcess">
<div class="container" id="annotationTable" class="display nowrap" cellspacing="0" width="100%">
</div>
</div>

I am rendering the table through a php script from mysql. Function getManual() gets triggered on clicking the tab panel.

function getManual() {
  var folder = $('#workingDir').val();
  $.post('manualAnnotation.php', {'folder': folder}, function(data) {
    $('#annotationTable').html(data).show();
  })
}

And my php that renders the table is as follows:

$result = mysqli_query($connect,$sql)or die(mysqli_error());

echo "<div class='col-md-5'><table class='dataTable' cellspacing='0' id='manTab'>";
echo "<thead><tr><th>Select</th><th>Image</th><th>Location</th><th>Brand</th><th>Run</th></tr></thead>";

while($row = mysqli_fetch_array($result)) {
    $ID = $row['ID'];
    $Image = $row['Image'];
    $Location = $row['Location'];
    $Brand = $row['Brand'];
    echo "<tbody><form><tr>
    <td><div class='radio' style='padding:0px;margin:0px'><label><input type='radio' value='$ID' id='manualTab' name='manualTab'></label></div></td>
    <td>".$Image."</td>
    <td>".$Location."</td>
    <td>".$Brand."</td>
    <td><a href='#' onclick='runManual()'>RUN</a></td>
    </tr></tbody></form>";
} 

echo "</table></div>";
mysqli_close($connect);

I am trying to enable the jQuery dataTable on the table id "manTab". On my index page, i have added this code:

 $(document).ready(function() {
      $('#manTab').DataTable({
  });
});      

While this renders the table the functionalities of datatable such as search, sort or pagination is not enabled.

2
  • @plonknimbuzz I tried that, but getting an error indicating that annotationTable is a div and not a table. Commented Mar 22, 2018 at 2:31
  • yep. i dont read your php before. i think i know the problem. but i try to build fiddle first to make sure that i'm correct Commented Mar 22, 2018 at 2:37

2 Answers 2

1

you trying to convert a table which not already finish rendering (from request) yet.

function getManual() {
  var folder = $('#workingDir').val();
  $.post('manualAnnotation.php', {'folder': folder}, function(data) {
    $('#annotationTable').html(data).show();
   $('#manTab').DataTable(); //place here
  });
}

above will working if you have few rows in your table. but if you have thousand or more. you need using callback/promise to wait until your table is finish rendered.

function getManual() {
  var folder = $('#workingDir').val();
  $.post('manualAnnotation.php', {'folder': folder}, function(data) {
    $('#annotationTable').html(data).show();
  }).done(function(){
    $('#manTab').DataTable(); //place here
  });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a million....I do have 100's of rows...and the .done function() is a new learning for me....thank you again.
while the css got enabled the sort or search not working....do I have to add the column defs inside the mantab datatable function?
0

You can position individual component based on configuration like this.

$(document).ready(function() {

    $('#example').DataTable( {

        "dom": '<"top"i>rt<"bottom"flp><"clear">'

    } );

} );

Look into official Documentation for more detail

l - length changing input control
f - filtering input
t - The table!
i - Table information summary
p - pagination control
r - processing display element

1 Comment

This is the first time I am using datatables...could you explain a bit please? I just want to add a search and sort function to the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.