0

So basically, I get a little bit of idea about using data tables. However, my question is a bit different based on my requirement. I'm not even sure whether it's possible.

As you may see inside the code, $("#tableContainer tbody").append(productList); basically creates a regular table (NOT a datatable).

Is there a way I can use productList directly inside a datatable so that it is created within the table standards?

      $(document).ready(function() {
          $.post('query-data.php', {
              type: "view-products"
          }, function(response) {
              var productList;
              var parsedResponse = JSON.parse(response);
             
              for (i = 0; i < parsedResponse.length; i++) {
                  productList = $('<tr><td>' + (parsedResponse[i].IS_AVILABLE == 1 ? '<i class="fa fa-check fa-fw"></i>' : '<i class="fa fa-times" aria-hidden="true"></i>') + '</td><td>' + (parsedResponse[i].IMAGE_URL != "" && parsedResponse[i].IMAGE_URL ? '<i class="fa fa-check fa-fw"></i>' : '<i class="fa fa-times" aria-hidden="true"></i>') + '</td><td>' + parsedResponse[i].PRODUCT_ID + '</td><td><a href="edit-product.php?product_id=' + parsedResponse[i].PRODUCT_ID + '&product_name=' + parsedResponse[i].NAME + '&business_id=' + parsedResponse[i].BUSINESS_ID + '&business_name=' + parsedResponse[i].BUSINESS_NAME + '&is_avilable=' + parsedResponse[i].IS_AVILABLE + '&image_url=' + parsedResponse[i].IMAGE_URL + '&start_time=' + parsedResponse[i].START_TIME + '&end_time=' + parsedResponse[i].END_TIME + '&category_ids=' + parsedResponse[i].CATEGORY_ID + '&category_names=' + parsedResponse[i].CATEGORY_NAME + '&product_description=' + parsedResponse[i].PRODUCT_DESCRIPTION + '&product_price=' + parsedResponse[i].PRODUCT_PRICE + '">' + parsedResponse[i].NAME + '</a></td><td>' + parsedResponse[i].CATEGORY_NAME + '</td><td>' + parsedResponse[i].BUSINESS_NAME + '</td></tr>');
                  $("#tableContainer tbody").append(productList);
              }
             
          });
      });
 <table id="tableContainer" class="table hover view-menus">
        <thead>
        <tr>
            <th>Avail</th>
            <th>Img</th>
            <th>Prod ID</th>
            <th>Prod Name</th>
            <th>Cat Name</th>
            <th>Biz Name</th>
        </tr>
        </thead>
        <tbody>
        
        </tbody>
        <tfoot>
        <tr>
            <th>Avail</th>
            <th>Img</th>
            <th>Prod ID</th>
            <th>Prod Name</th>
            <th>Cat Name</th>
            <th>Biz Name</th>
        </tr>
        </tfoot>
    </table>

2
  • Try to move var t = $('#tableContainer').DataTable(); after your for loop, if you convert your table into DataTable first, it is suggested to use DataTable API to add new node rather than adding HTML dom Commented Feb 24, 2017 at 15:50
  • My apologies. Actually, I'd put this line by mistake in the code var t = $('#tableContainer').DataTable();. I actually don't know how to map my dynamically created tr to datatable Commented Feb 24, 2017 at 16:03

1 Answer 1

1

Whooppie!! Turns out, it was quite simple.

First declare var table = $('table').DataTable(); before starting the for loop.

Then add table.row.add($(productList )).draw(); while iterating, i.e. inside the for loop. DONE! :)

 var table = $('table').DataTable();
 for (i = 0; i < parsedResponse.length; i++) {
     productList = $('<tr><td>' + (parsedResponse[i].IS_AVILABLE == 1 ? '<i class="fa fa-check fa-fw"></i>' : '<i class="fa fa-times" aria-hidden="true"></i>') + '</td><td>' + (parsedResponse[i].IMAGE_URL != "" && parsedResponse[i].IMAGE_URL ? '<i class="fa fa-check fa-fw"></i>' : '<i class="fa fa-times" aria-hidden="true"></i>') + '</td><td>' + parsedResponse[i].PRODUCT_ID + '</td><td><a href="edit-product.php?product_id=' + parsedResponse[i].PRODUCT_ID + '&product_name=' + parsedResponse[i].NAME + '&business_id=' + parsedResponse[i].BUSINESS_ID + '&business_name=' + parsedResponse[i].BUSINESS_NAME + '&is_avilable=' + parsedResponse[i].IS_AVILABLE + '&image_url=' + parsedResponse[i].IMAGE_URL + '&start_time=' + parsedResponse[i].START_TIME + '&end_time=' + parsedResponse[i].END_TIME + '&category_ids=' + parsedResponse[i].CATEGORY_ID + '&category_names=' + parsedResponse[i].CATEGORY_NAME + '&product_description=' + parsedResponse[i].PRODUCT_DESCRIPTION + '&product_price=' + parsedResponse[i].PRODUCT_PRICE + '">' + parsedResponse[i].NAME + '</a></td><td>' + parsedResponse[i].CATEGORY_NAME + '</td><td>' + parsedResponse[i].BUSINESS_NAME + '</td></tr>');
     table.row.add($(productList)).draw();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

@Katrin Yes, it did. Was about to write this comment. Thanks a lot :).

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.