GithubHelp home page GithubHelp logo

andrejgajdos / nested-datatables Goto Github PK

View Code? Open in Web Editor NEW
32.0 5.0 18.0 1.56 MB

jQuery DataTables plugin for rendering nested DataTables in rows. Inner tables are independent of the data and layout from the outer table.

Home Page: https://andrejgajdos.github.io/nested-datatables/

License: MIT License

JavaScript 100.00%
jquery datatables hierarchy innersource nested-structures nested-objects javascript

nested-datatables's Introduction

Nested-datatables

jQuery DataTables plugin for rendering nested DataTables in rows. Inner tables are independent of the data and layout from the outer table.

Installation

Download the latest version and include nested.datatables.min.js file

NPM

$ npm install nested-datatables

Usage

var table = new nestedTables.TableHierarchy("example", data, settings);
table.initializeTableHierarchy();

Methods

TableHierarchy(wrapperID, data, settings)

Main NestedTables constructor.

wrapperID

Type: String

ID of a DOM element where will be table hierarchy rendered.

data

Type: Array.<Object>

Data used for building table hierarchy. Each item consists of property data and property kids, which represents array of child elements.

Check examples below how to define json data.

settings

Type: Object

Settings for jQuery DataTables constructor.

.initializeTableHierarchy()

Build nested table hierarchy.

Events

onShowChildHierarchy

Triggered when a child hierarchy is shown

// '#example' is wrapper ID for table hierarchy
var tableEle = document.querySelector("#example .table");
tableEle.addEventListener("onShowChildHierarchy", function (e) {
  console.log(e);
});

onHideChildHierarchy

Triggered when a child hierarchy is hidden

// '#example' is wrapper ID for table hierarchy
var tableEle = document.querySelector("#example .table");
tableEle.addEventListener("onHideChildHierarchy", function (e) {
  console.log(e);
});

Example

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />

    <script src="./dist/nested.datatables.min.js"></script>
  </head>
  <body>
    <div id="example" class="container"></div>

    <script>
      var dataInJson = [
        {
          data: {
            name: "b1",
            street: "s1",
            city: "c1",
            departments: 10,
            offices: 15,
          },
          kids: [
            {
              data: {
                department: "HR",
                supervisor: "Isidor Bristol",
                floor: 1,
                employees: 15,
              },
              kids: [
                {
                  data: {
                    name: "Klement Nikodemos",
                    phone: "+938462",
                    hire_date: "January 1, 2010",
                    id: 3456,
                  },
                  kids: [],
                },
                {
                  data: {
                    name: "Madhava Helmuth",
                    phone: "+348902",
                    hire_date: "May 23, 2002",
                    id: 1234,
                  },
                  kids: [],
                },
                {
                  data: {
                    name: "Andria Jesse",
                    phone: "456123",
                    hire_date: "October 23, 2011",
                    id: 9821,
                  },
                  kids: [],
                },
              ],
            },
            {
              data: {
                department: "development",
                supervisor: "Jim Linwood",
                floor: 2,
                employees: 18,
              },
              kids: [
                {
                  data: {
                    name: "Origenes Maxwell",
                    phone: "345892",
                    hire_date: "February 1, 2004",
                    id: 6234,
                  },
                  kids: [],
                },
              ],
            },
            {
              data: {
                department: "testing",
                supervisor: "Zekeriya Seok",
                floor: 4,
                employees: 11,
              },
              kids: [],
            },
          ],
        },
        {
          data: {
            name: "b2",
            street: "s10",
            city: "c2",
            departments: 3,
            offices: 10,
          },
          kids: [
            {
              data: {
                department: "development",
                supervisor: "Gallagher Howie",
                floor: 8,
                employees: 24,
              },
              kids: [
                {
                  data: {
                    name: "Wat Dakota",
                  },
                  kids: [],
                },
              ],
            },
            {
              data: {
                department: "testing",
                supervisor: "Shirley Gayle",
                floor: 4,
                employees: 11,
              },
              kids: [],
            },
          ],
        },
        {
          data: {
            name: "b3",
            street: "s3",
            city: "c3",
            departments: 2,
            offices: 1,
          },
          kids: [
            {
              data: {
                department: "development",
              },
              kids: [
                {
                  data: {
                    name: "Wat Dakota",
                  },
                  kids: [],
                },
              ],
            },
            {},
          ],
        },

        {
          data: {
            name: "b4",
            city: "c4",
          },
          kids: [],
        },
      ];

      var settings = {
        iDisplayLength: 20,
        bLengthChange: false,
        bFilter: false,
        bSort: false,
        bInfo: false,
      };

      var table = new nestedTables.TableHierarchy(
        "example",
        dataInJson,
        settings
      );
      table.initializeTableHierarchy();
    </script>
  </body>
</html>

Example 2

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />

    <script src="./dist/nested.datatables.min.js"></script>
  </head>
  <body>
    <div id="example" class="container"></div>

    <script>
      var dataInJson2 = [
        {
          data: {
            date: "2018-08",
          },
          kids: [
            {
              data: {
                " ": '&lt;input type="checkbox" name="exampleCheckbox" value="exampleCheckbox"&gt;',
                img: "&lt;img src=https:\/\/picsum.photos\/100\/100 width=20 \/&gt;",
                like: {
                  value: 47,
                  cellClass: "likeCell",
                  headerClass: "likeHeader",
                },
                perf: { value: 130.55555555555554, cellClass: "perf" },
                date: "2018-08-30",
              },
              kids: [],
            },
            {
              data: {
                " ": '&lt;input type="checkbox" name="exampleCheckbox2" value="exampleCheckbox2"&gt;',
                img: "&lt;img src=https:\/\/picsum.photos\/100\/100 width=20 \/&gt;",
                like: 24,
                perf: 66.66666666666667,
                date: "2018-08-31",
              },
              kids: [],
            },
          ],
        },
      ];

      var settings = {
        iDisplayLength: 20,
        bLengthChange: false,
        bFilter: false,
        bSort: false,
        bInfo: false,
      };

      var table = new nestedTables.TableHierarchy(
        "example",
        dataInJson,
        settings
      );
      table.initializeTableHierarchy();
    </script>
  </body>
</html>

License

MIT © Andrej Gajdos

Notion Api Connector · SalesforceToNotion

nested-datatables's People

Contributors

andrejgajdos avatar ignalpha4 avatar lordzerohour avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

nested-datatables's Issues

using external JSON data

How/Can you use this for externally sourced (AJAX) data? Or would you have to do this as a separate AJAX call and have the dataInJson array already pre-populated before running initializeTableHierarchy?

Cannot initialize plugin

hello.. i tried the new build right after i saw your message. first thing i noticed was that name of the library has changed to nested.tables.min.js. after i replaced the include with the latest build, i started getting the following error : "Uncaught ReferenceError: lib is not defined".. in short it seems that lib is no longer available for initialization.

Adding sorting arrows on the table headers


                   var getTableHeader = $('.tableWrapper table.dataTable th');
              
                   getTableHeader.append('<span class="arrow-direction descArr"></span>');

                   getTableHeader.click(function(e){
                   	  e.preventDefault();
                         $(this).find('span').toggleClass('ascArr descArr');
                         
                   });

Thanks once again for your great plugin. I would like to add a sorting arrow on the table headers. When I try with the above code, it just adds on the parent table headers only. Do you have any suggestions ?

Having multiple nested tables causing error.

having two different nested tables in one web page, causing error when expanding any row in the first table.

Uncaught TypeError: Cannot read property '0' of undefined
at mt.fn.init.fnOpen (nested.tables.min.js:formatted:12961)
at HTMLTableRowElement. (nested.tables.min.js:formatted:10517)
at HTMLDivElement.dispatch (nested.tables.min.js:formatted:5101)
at HTMLDivElement.g.handle (nested.tables.min.js:formatted:5033)

Select event handler..?

This plugin helped me a big deal, thank you for putting it together.. Is there a way to catch the event of a row selection which expands to show the detail table?

onShowChildHierarchy handler

var tableElement = document.querySelector("#example");
tableElement.addEventListener("onShowChildHierarchy", function(e) {
console.log(e);
});

how do you find the row that was clicked, please?

Editable nested datatable?

Hi,

Can you please let me know if it is possible to implement an editable version of the datatable the jQuery datatables support as shown here?.

Configuring the nested tables?

Hi there!

I'm glad to find your example!

Curious if you could explain how to nest the tables without the headers and pagination? I'm hoping to achieve something like the attached image with datatables.

Christen

nested

Getting Error Uncaught TypeError: Cannot read properties of undefined (reading '0') on data update

Hi,

I am fetching data from api and assigning it to nested data table by using following function.
function createTree(data)
{
var settigns = {
iDisplayLength: 5,
bLengthChange: true,
bFilter: true,
bSort: true,
bInfo: true,
};

  var table = new nestedTables.TableHierarchy(
    "treetable",
    ajaxData,
    settigns
  );
  table.initializeTableHierarchy();

}

So for the first time I am able to see the nested data table and all rows are getting expanded but after 30 Sec I want to update the nested table so I am calling the same function with new data. At that time I am getting below error.

Uncaught TypeError: Cannot read properties of undefined (reading '0')
at DataTable.fnOpen (jquery.dataTables.js:667:1)
at HTMLTableRowElement.eval (NestedTables.js:151:1)
at HTMLDivElement.dispatch (jquery.js:5206:1)
at elemData.handle (jquery.js:5014:1)

Please let me know how can i fix this error.

How to include checkbox / input in cells of the nested table?

Very interesting this plugin congratulations! But I consulted knowing that the nested table is loaded by means of JSON as specific that I will include checkbox or input to add editable features. Is it possible for the current version of the plugin?

nested.datatables.min.js file not found

Hi Andrej,

Please share with me the old nested.datatables.min.js file.

As nested.tables.min.js file does not run my code and gives lib is not defined error.

Please reply soon..

Thanks,
Omkar Kadam

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.