Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best approach to render nested rows in a data-table?

I'm planning to create a tree data table for file browser and wondering how to render rows for files in a folder. The data is in a flat list of file paths but I'm able to re-format it to be the same as the structure of the folders.

I am debating if I have to render children of a folder as nested rows inside a row or if I could have them as sibling rows and just modify the padding to indicate the hierarchy?

This is how the data looks like:

[ {
    "name" : "Parent Folder",
    "path" : "/home/desktop/report",
    "size" : 2156754,
    "createdOn" : -1,
    "fileType" : "FOLDER",
    "itemId" : 478202,
  }, {
    "name" : "nested file",
    "path" : "/home/desktop/report/test.js",
    "size" : 15402,
    "createdOn" : 1595072355000,
    "fileType" : "FILE",
    "itemId" : 478203,
    "parentId" : 478202,
  }, {
    "name" : "nested folder",
    "path" : "/home/desktop/report/build",
    "size" : 2141352,
    "createdOn" : 1595072355000,
    "fileType" : "FOLDER",
    "itemId" : 478204,
    "parentId" : 478202,
    "faulty" : false,
    "newItemCount" : 478498,
    "itemCount" : 478498
  },
  {
    "name" : "nested folder",
    "path" : "/home/desktop/report/build/main",
    "size" : 2141352,
    "createdOn" : 1595072355000,
    "fileType" : "FILE",
    "itemId" : 478204,
    "parentId" : 478204,
  }
  ]
like image 991
tsinat Avatar asked Aug 10 '20 18:08

tsinat


People also ask

How do you make a nested table?

Click inside any cell in the larger table. Once again, use the “Insert” tab to create a table. For example, click on cell 1, go to “Insert,” “Table” and then create a 2-by-2 table. This 2-by-2 is now nested inside of the 3-by-3.

How do I create a nested table within a table in HTML?

To create a nested table, we need to create a table using the <table> tag. This table is known as the outer table. The second table that will be nested table is called the inner table. This table is also created using the <table> tag but there is a special thing that must be kept in mind.

Should use a nested table in a design when?

If you need to run efficient queries on a collection, handle arbitrary numbers of elements, or perform mass insert, update, or delete operations, then use a nested table. See "Design Considerations for Collections".

What are nested rows?

Nested Rows allow you to place more than one element or tile within a single row. For instance, if you have a Row, you can put in a Nested Row with two equally divided columns. From there, you can drag in unlimited amounts of Nested Rows within the Nested Row with the two columns.

Should you use nested tables in your database?

Most of the complex and large tables might include nesting of tables within the main table to have better control in the coding. Using nested tables might help create beautiful and interesting appearances and visuals, but it can create loose end errors.

How many levels can you create a nested table?

You can create nested tables to any number of levels; just remember to create an inner table inside the same cell. Below is an interpretation of nested tables. The below image shows a five-level nesting of tables, with the color ‘Blue’ as the outermost or the container table with nested tables represented with colors White, Red, Yellow, and Green.

What is the use of render option in DataTables?

The data that is returned by the columns.render option (regardless of if it is used as a function, integer or string) is what DataTables will use for the requested data type (this is called the resolved data).

What is table nesting in HTML?

The concept of nesting within the tables becomes more interesting by visual when the programmer uses tables for formatting the complete webpage. The table can then be formatted like any other table and other HTML elements the programmer might nest within. Below are the examples mentioned :


Video Answer


1 Answers

There are a lot of ways to archive your goal, but I doubt that there is the best way, they are just different. And it is always a question: "best it what way?"

But first of all, I would advice you not to use tables at all.

<li> is more semantically correct. Here https://onaircode.com/html-css-tree-view-examples/ you can find a lot of examples with codepens.

But if you really want to use <table>, you definitely don't need nested rows. That will be a total mess both in code and in HTML.

There is nothing simpler that padding. You don't even need some special HTML markup or CSS for that - just add as many &nbsp; as needed to strings with filenames.

td { border-right: solid; }
table { border-spacing: 0px }
<table>
  <tr>
    <td>folder1</td><td></td><td>1 file & 1 folder</td>
  </tr>
  <tr>
    <td>&nbsp;&nbsp;folder2</td><td></td><td>1 file</td>
  </tr>
  <tr>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;file1</td><td>pdf</td><td>123Kb</td>
  </tr>
  <tr>
    <td>&nbsp;&nbsp;file2</td><td>txt</td><td>123Kb</td>
  </tr>
</table>

If you don't like &nbsp; you can instead add <span>s with a padding

td { border-right: solid; }
table { border-spacing: 0px }
span { padding: 3px; }
<table>
  <tr>
    <td>folder1</td><td></td><td>1 file & 1 folder</td>
  </tr>
  <tr>
    <td><span></span>folder2</td><td></td><td>1 file</td>
  </tr>
  <tr>
    <td><span></span><span></span>file1</td><td>pdf</td><td>123Kb</td>
  </tr>
  <tr>
    <td><span></span>file2</td><td>txt</td><td>123Kb</td>
  </tr>
</table>

As for <li> and your need to display additional columns... well yea, that can be a little more difficult, so if you don't strive for semantics or for code composition, you probably should stick with <table>s for now.

But anyway, here is one way to do it:

.tree { width: 90%; }
.file_box { display: flex; flex-direction: row; justify-content: flex-end; }
.filename { margin-right: auto; }
.type { width: 130px; overflow: hidden; border-right: solid; border-left: solid; }
.size { width: 130px; overflow: hidden; }
<ul class="tree">
  <li>
    <div class="file_box"><span class="filename">folder1</span><span class="type"></span><span class="size">1 file & 1 folder</span></div>
    <ul>
      <li>
        <div class="file_box"><span class="filename">folder2</span><span class="type"></span><span class="size">1 file</span></div>
        <ul>
          <li>
            <div class="file_box"><span class="filename">file</span><span class="type">pdf</span><span class="size">123Kb</span></div>
          </li>
        </ul>
      </li>
      <li>
        <div class="file_box"><span class="filename">file</span><span class="type">txtasdfasdfasdfasdfasdfasdf</span><span class="size">123Kb</span></div>
      </li>
    </ul>
  </li>
</ul>

Here you must choose the width of extra columns. Width won't change with the content (see last row). But I don't think that changing width of columns without user's consent is a good idea. If you think it is, you can create separate columns for filename's <li>-tree, for file types and for file sizes and place them next to each other with the help of Flexbox or Grid Layout, of just float them.

like image 125
x00 Avatar answered Nov 09 '22 22:11

x00