title: Add nested datatable within child row of another datatable
body: <p>I have seen where others have asked this question (like here: <a href="https://stackoverflow.com/questions/34516552/add-child-row-as-nested-datatable-within-exisiting-datatable">Add child row as nested datatable within exisiting datatable</a>) but I have yet to see it answered.</p>
  
  <p>I have a "master" datatable that makes use of the child row feature.  Here's the code that initializes the "master" datatable. (I'm including it just to be thorough, but everything here works perfect.)</p>
  
  <pre><code>var table = $('#members');
  
  var oTable = table.dataTable({
  
      "language": {
          "aria": {
              "sortAscending": ": activate to sort column ascending",
              "sortDescending": ": activate to sort column descending"
          },
          "emptyTable": "No data available in table",
          "info": "Showing _START_ to _END_ of _TOTAL_ entries",
          "infoEmpty": "No entries found",
          "infoFiltered": "(filtered1 from _MAX_ total entries)",
          "lengthMenu": "_MENU_ entries",
          "search": "Search:",
          "zeroRecords": "Loading..."
      },
      "buttons": [],
      "columnDefs": [
          {className: 'control'},
          {orderable: false, targets: 0 }
      ],
      // setup responsive extension: http://datatables.net/extensions/responsive/
      "responsive": true,
      "order": [
  
          [1, 'asc']
  
      ],
      "order-column": false,
      "lengthMenu": [
          [5, 10, 15, 20, -1],
          [5, 10, 15, 20, "All"] // change per page values here
      ],
      // set the initial value
      "pageLength": 5,
      "dom": "&lt;'row' &lt;'col-md-12'B&gt;&gt;&lt;'row'&lt;'col-md-6 col-sm-12'l&gt;&lt;'col-md-6 col-sm-12'f&gt;r&gt;&lt;'table-scrollable't&gt;&lt;'row'&lt;'col-md-5 col-sm-12'i&gt;&lt;'col-md-7 col-sm-12'p&gt;&gt;", 
  });
  </code></pre>
  
  <p>This next bit of code is located right after the above code and accomplishes 2 things:
  1) It prevents multiple child rows from being opened at once (just a personal preference) and
  2) Uses an AJAX call to populate the child row that was just toggled open.</p>
  
  <pre><code> table.on('click', 'tr', function () {
  
          var id = $(this).attr('id');
          if($(this).hasClass('parent'))
          {
              table.$('tr.parent').not(this).find('td:first-child').trigger('click');
  
              var load_member_leads = $(this).next().find('td.child &gt; ul &gt; li span.dtr-data');
              //var load_member_leads = $('#test');
  
              $.ajax({
                  url: base_url + 'process/load_member_leads',
                  type: 'POST',
                  data: {test:id},
                  dataType: "html", 
                  success: function(data){
                      console.log ("success");
                      load_member_leads.html(data);
                      formRepeater();
                      initTable1(id);
                  },
                  failure: function (data) {
                      console.log ("failed");
                  }
              });
          }
      });
  </code></pre>
  
  <p>This portion of the code appears to be working well, but here is where it gets tricky.  Within the returned AJAX data, I have an html table that I would like to initialize using the datatables plugin. Essentially what I'd have is a nested datatable within a child row of another "master" datatable.  The problem is, everything works great until I try to initialize the datatable plugin on the nested table using:</p>
  
  <pre><code>  initTable1(id);
  </code></pre>
  
  <p>(Note: the id variable being passed is to prevent multiple tables with the same ids.  You can see in the code below how it's appended to the datatable init call.) Once this function is called, everything in the child row disappears and is erased from the DOM.  Just gone. And I have no idea why. </p>
  
  <p>Here is the code within the initTable1() function that initializes the 2nd datatable:</p>
  
  <pre><code> var table2 = $('#leads_' + id);
  
      var oTable2 = table2.dataTable({
          "language": {
              "aria": {
                  "sortAscending": ": activate to sort column ascending",
                  "sortDescending": ": activate to sort column descending"
              },
              "emptyTable": "No data available in table",
              "info": "Showing _START_ to _END_ of _TOTAL_ entries",
              "infoEmpty": "No entries found",
              "infoFiltered": "(filtered1 from _MAX_ total entries)",
              "lengthMenu": "_MENU_ entries",
              "search": "Search:",
              "zeroRecords": "No matching records found"
          },
  
          destroy: true,
  
          //setup buttons extentension: http://datatables.net/extensions/buttons/
          buttons: [],
  
          // setup responsive extension: http://datatables.net/extensions/responsive/
          responsive: {
              details: {
                  type: 'column',
                  target: -1
              }
          },
          "columnDefs": [ {
             className: 'control',
             orderable: false,
             targets:   -1
          } ],
          "order": [
              [0, 'asc']
          ],
          "ordering": false,
          "lengthMenu": [
              [5, 10, 15, 20, -1],
              [5, 10, 15, 20, "All"] // change per page values here
          ],
          // set the initial value
          "pageLength": 10,
          "dom": ""
  
      });
  </code></pre>
  
  <p>And for good measure, here is the code being fetched by the AJAX call. (Note the $test variable is the same as the id variable being passed to the initTable1 function)</p>
  
  <pre><code> &lt;table class="table table-striped table-bordered dt-responsive" width="100%" id="leads_&lt;?php echo $test; ?&gt;"&gt;
                              &lt;thead&gt;
                                  &lt;tr&gt;
                                      &lt;th class="all"&gt;Column 1&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 2&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 3e&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 4&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 5&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 6&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 7e&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 8&lt;/th&gt;
                                      &lt;th class="none"&gt;Column 9&lt;/th&gt;
                                      &lt;th class="all"&gt;Column 10&lt;/th&gt;
                                  &lt;/tr&gt;
                              &lt;/thead&gt;
                              &lt;tbody&gt;
                              &lt;!-- table rows populated here - this is currently hardcoded for testing --&gt;
                              &lt;/tbody&gt;
                          &lt;/table&gt;
  </code></pre>
  
  <p>Here's what DOES work, which may shed some light as to what's actually causing the problem.</p>
  
  <p>1) It works if I comment out the initTable1() function within the AJAX call...well everything but the initialization of the datatable plugin on the 2nd table.  But the html populates where it should within the child row of the "master" datatable.</p>
  
  <p>2) It works if I change where the AJAX data is being populated - specifically, it works outside of the "master" datatable. It not only populates where it should (note the commented out var load_member_leads that points to $('#test').), but it initializes datatables correctly as well. </p>
  
  <p>It only seems to break if I both initialize datatables on the second table, and place said table in the child row of the "master" datatable.  And by break, I mean completely disappear as if the AJAX call failed - which it isn't according to the console.log.</p>
  
  <p>This problem is driving me crazy, what am I doing wrong? Any help is much appreciated! </p>