Implementing DataTables to create several nested tables while keeping the column definitions unchanged

Situation: I have a complex setup with 7 DataTables generated through PHP and HTML directly on the page. Each summary level DataTable contains 6 nested detail level DataTables, except for one. Utilizing the responsive option, I'm able to display the content of the detail tables in a child row structure like demonstrated at https://datatables.net/examples/api/row_details.html

Challenge: My issue arises when trying to initialize each child table within the initComplete: function(){} of the parent table. While it does seem to trigger some action, the child tables lose the functionality typically associated with the DataTables libraries (such as column widths defined via columnDefs).

The primary concern is that my DataTable options, especially setting widths through columnDefs, are being ignored:

Is there something crucial that I'm overlooking? Why might the system be disregarding or overriding my column width specifications? The parent table supports both responsive and columnDefs.

Refer to the snippet below for an example scenario:

$('#summary_table').DataTable({
  paging: false,
  autoWidth: false,
  searching: false,
  columnDefs: [{
      'width': '3%',
      'targets': [0]
    },
    {
      'width': '10%',
      'targets': [1, 2]
    },
    {
      "className": "dt-center",
      "targets": "_all"
    },

  ],
  initComplete: function() {
    console.log("Initialisation of table complete");
    var sub_table = $('#summary_table').find('.ic-detail-table');
    if (sub_table.length > 0) {
      var sub_table_inst = $(sub_table).DataTable({
        paging: false,
        autoWidth: false,
        searching: false,
        columnDefs: [
          //IGNORED????
          {
            'width': '10%',
            'targets': [0]
          },
          {
            'width': '25%',
            'targets': [1]
          },
          {
            'width': '25%',
            'targets': [2]
          },
          {
            'width': '40%',
            'targets': [3]
          },
          {
            "className": "dt-center",
            "targets": "_all"
          },
        ],
        ordering: true,
        sorting: true,
        initComplete: function() {
          console.log("SUB TABLE INIT COMPLETE");
        },
        responsive: true,
        dom: '<"clear">rt',
        order: [
          [1, 'asc']
        ]
      });
    }
  },
  ordering: false,
  responsive: true,
  dom: '<"clear">rt',

  order: [
    [1, 'asc']
  ]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.4/r-2.2.2/sl-1.2.6/datatables.min.js"></script>
<link href="https://cdn.datatables.net/v/dt/dt-1.10.18/b-1.5.4/r-2.2.2/sl-1.2.6/datatables.min.css" rel="stylesheet" />
<table class='table table-bordered display compact' id='summary_table'>
  <thead>
    <tr>
      <th></th>
      <th>Heading one</th>
      <th>Heading two</th>
      <th>Heading three</th>
      <th class='none'>Detail table</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td>cell one</td>
      <td>cell two</td>
      <td>cell three</td>
      <td>
        <table class='table compact' class='ic-detail-table'>
          <thead>
            <tr>
              <th>Heading one</th>
              <th>Heading two</th>
              <th>Heading three</th>
              <th>Heading four</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Heading one</td>
              <td>Heading two</td>
              <td>Heading three</td>
              <td>Heading four</td>
            </tr>
          </tbody>
        </table>
      </td>
    </tr>
  </tbody>
</table>

Answer №1

If you're hoping to achieve a specific layout with datatables, it's not a built-in feature but can be customized by setting maximum width for the classes dtr-details and compact.

To implement this custom layout, you can add the following CSS code snippet:

.dtr-details,
.compact {
  width: 100%!important;
}

Note that in the provided JavaScript code, ''targets': [1, 2]' has been changed to ''targets': [1, 2, 3]'. Also, unnecessary options in child datatables can be omitted as they are not considered.

If you want to adjust the first column's width to 3% in an inner table with the id innerTable, you can use the following CSS:

#innerTable thead tr th:first-child,
#innerTable tbody tr td:first-child {
  width: 3%!important;
}

You can find a complete example implementation at this JSFiddle link: https://jsfiddle.net/6fp3kbnh/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the error message "Unable to access 'http' property of undefined" to appear in this validator?

Can someone help me with creating an asynchronous validator for a reactive form control that checks if a username already exists? Below is the code for the async validator: userdata.service.ts import { HttpClient } from '@angular/common/http'; i ...

Remove displayed image preview by utilizing jQuery to manipulate the input selection

I have implemented a feature to add image preview/delete before uploading using jQuery. The HTML code is as follows: <input type='file' id='input1'> <img id='imagepreview1' src="http://placehold.it/100x100" /> An ...

Utilizing jQuery/AJAX to dynamically load PHP pages and reveal targeted links for page display within a designated Div container

I have a MySQL table with id, name, and desc fields. I am using a PHP select query to display this data in a div. <div class="show_name"> while( $row = $data->fetch_array(MYSQLI_ASSOC)) { ?> <div><?php echo $row[' ...

Ways to retrieve a webpage component that has multiple values within its class attribute

My dilemma lies in this HTML snippet: https://i.sstatic.net/E7zj0.png Within the code, there are two instances of <div class="sc-fjhmcy dbJOiq flight-information"></div>. I am trying to target these elements using their class attribu ...

Instead of just displaying a count after updating in the database, the updated object will be

Updating an entry in a food truck database requires some adjustments. Here is the model function for handling updates: function updateTruckInfo(changes, id) { return db('trucks') .where({ id }) .update(changes) .then( ...

When using NVDA, the close button is being read multiple times, whereas in JAWS it is being read correctly

Here is the code for a close button within a modal pop-up: <h2 class="sc-ejdXBC byjYiM MuiTypography-root MuiTypography-h6 sc-EoWXQ kOYWJk MuiDialogTitle-root" id="popupTitle"> Default Pop-UP Title <button class="sc-dm ...

sending information from a PHP form to a JavaScript window

Currently, I am in the process of developing a game using javascript and jquery. In this game, when a player interacts with another character, it triggers the opening of text from an external file using the window.open('') function. At the start ...

Establishing communication between a parent window and a popup window that have distinct domains

I'm currently developing a browser extension using JavaScript. My main tasks include: Sending javascript objects from a webpage located at "foo.com" to a popup page on "bar.com" For example, from "foo.com/some_page/" to "bar.com/another_page.htm ...

Conceal mobile button

Welcome to my website: Is there a way to hide the button on mobile phones? I want to create different buttons specifically for mobile devices. Below is the HTML code for the buttons. HTML <a href="http://site9171756.91.webydo.com/Info.html" target="p ...

Managing data in an Angular Material table and dialog for creating, reading

Currently, I am struggling with implementing a CRUD method using angular material table and dialog components. I am facing difficulties in understanding how to update records using the dialog and pass data between components. Despite my efforts, the modif ...

Having difficulty retrieving the value of a dynamically generated cell with document.getElementById

I have been facing an issue with adding rows to an HTML table that already contains 2 rows, using the following JavaScript code: <table id="dataTable" class="CSSTableGenerator"> <tbody> <tr> <td>< ...

Loading data for Jquery Autocomplete in one go (Choosing between $.ajax or $.get/$.post)

Seeking assistance as I am very interested in this topic. I am looking to have autocomplete fetch data source only once during page load. I have attempted two methods. The first method, using $.ajax(GET/POST) was successful $.ajax({ type: "GET", u ...

Save the text found within an element to Appim wd.js

I am a beginner with Appium and I am currently exploring the workshop Git project, which utilizes wd.js and Grunt. From my research in the documentation and forums, I have learned that there are two methods for accessing the text of native elements within ...

jQuery Validate not triggering remote action method

I am currently working with ASP.NET MVC4, jQuery Validate, and jQuery Validate unobtrusive. Among the elements on my form, there is only one element that requires validation. The rest of the elements do not need to be validated. Specifically, I have a tex ...

Not successfully integrating an angular component

In my Angular application, I am working on creating a new component and injecting it into the app. Below is the code for the angular component: (function(angular) { 'use strict'; angular.module('some.someModule', ['bm.component.t ...

What is the best way to use Vue to fetch data from an API asynchronously in order to create a table?

My HTML Code <div id="app"> <table> <thead> <tr> <th v-for="(header, key) in column" :key="key">{{ header }}</th> </tr> </t ...

Revamp the state within a React component

I created a component that organizes logos within a grid system. The component receives 2 props: grid (which contains the logos) and limit (indicating the number of logos to be displayed). type Props = { grid: [], limit: number, }; type Sta ...

Changing the appearance of your website on various pages using WordPress: activating unique stylesheets

When connecting my stylesheets to my WordPress theme, I include the following in my customtheme/style.css file: @import url('bootstrap/bootstrap.min.css'); @import url('includes/styles1.css'); @import url('includes/styles2.css&ap ...

HTML Client Lightswitch: Assign value to modal picker upon screen initialization

After conducting a considerable amount of research on this issue, I have found that none of the examples provided are helpful or applicable. My goal is to have the Details Picker display a specific name when the Add screen loads, instead of requiring the u ...

The styling for the <fieldset> element is not functioning correctly

There seems to be an issue with styling <fieldset>. In Google Chrome, the divs are adjusting to the content. .table { border: none; border-collapse: collapse; display: table; margin: 0; min-width: auto; padding: 0; table-layout: fixe ...