Table (class)

Categories:Text

Overview

Summary

The table class is designed to output an Array of data in table form. This is most useful for quickly checking or comparing multiple properties across a range of similar objects, such as library items, or selected elements.

Contents

API

Table(rows, caption, keys, maxColWidth, maxRowHeight)

Table constructor

Parameters:

  • rows Array An input Array of objects
  • rows Object An input Object, the properties & values of which will become rows
  • caption String An optional String caption that is shown at the top of the table
  • keys Array An optional array of columns to extract from the data
  • keys String An optional anything-delimted string to extract from the data
  • keys Number An optional Table ORDER Constant to order the columns
  • maxColWidth Number Max Row Width (chars)
  • maxRowHeight Number Max Column Height (carriage returns)

Tables can be created in serveral ways, expecting an Array of objects as their first argument, and an array of columns as their next argument, OR a Table ORDER constant. See the section following the API guide for more information.

The following example creates a table with the current library items:

var table = new Table($library.items);
table.render();
+-------------------+------------+----------------+-------------------+------------------+-------------+-----------------+------------+-----------+--------------------+--------------------+--------------------+---------------------------+-------------------+------------------+------------------+------------+
| timeline          | symbolType | sourceFilePath | sourceLibraryName | sourceAutoUpdate | scalingGrid | scalingGridRect | itemType   | name      | linkageExportForAS | linkageExportForRS | linkageImportForRS | linkageExportInFirstFrame | linkageIdentifier | linkageClassName | linkageBaseClass | linkageURL |
+-------------------+------------+----------------+-------------------+------------------+-------------+-----------------+------------+-----------+--------------------+--------------------+--------------------+---------------------------+-------------------+------------------+------------------+------------+
|                   |            |                |                   |                  |             |                 | folder     | Folder    |                    |                    |                    |                           |                   |                  |                  |            |
| [object Timeline] | button     |                |                   |                  | false       | [object Object] | button     | Button    | false              | false              | false              |                           |                   |                  |                  |            |
| [object Timeline] | graphic    |                |                   |                  | false       | [object Object] | graphic    | Graphic   | false              | false              | false              |                           |                   |                  |                  |            |
| [object Timeline] | movie clip |                |                   |                  | false       | [object Object] | movie clip | MovieClip | false              | false              | false              |                           |                   |                  |                  |            |
+-------------------+------------+----------------+-------------------+------------------+-------------+-----------------+------------+-----------+--------------------+--------------------+--------------------+---------------------------+-------------------+------------------+------------------+------------+

It is also possible to pass in a non-Array object, such as a single library item, and when Table detects this, it automatically displays Property and Value columns. Passing in true as the keys parameter will also output a third Type column.

The following example prints a table of the properties and values of a single Library Item:

var table = new Table($library.items[0]);
table.render();		
+---------------------------+----------------------+
| Property                  | Value                |
+---------------------------+----------------------+
| timeline                  | [object Timeline]    |
| symbolType                | movie clip           |
| sourceFilePath            |                      |
| sourceLibraryName         |                      |

You may also add a caption to the table in the second parameter, which can be useful when reporting or debugging.

The following example displays a short caption about the

var clip = $selection[0];
var table = new Table(clip, $timeline.name + ' > ' + clip.name); table.render();
+-------------------------------------------+
| Scene 1 > Symbol_02                       |
+---------------------+---------------------+
| Property            | Value               |
+---------------------+---------------------+
| instanceType        | symbol              |
| symbolType          | movie clip          |
| effectSymbol        | false               |
| libraryItem         | [object SymbolItem] |

The last two parameters in the constructor allow you to specify maximum column widths, and row heights, which is useful if you have a lot of data in your table and want to show more or less. The default maximum column width is 100, and the maximum row height is 2.


print(rows, caption, keys, maxColWidth, maxRowHeight)

Static table method to print a table

Parameters:

  • rows Array An input Array of objects
  • rows Object An input Object, the properties & values of which will become rows
  • caption String An optional String caption that is shown at the top of the table
  • keys Array An optional array of columns to extract from the data
  • keys String An optional anything-delimted string to extract from the data
  • keys Number An optional Table ORDER Constant to order the columns
  • maxColWidth Number Max Row Width (chars)
  • maxRowHeight Number Max Column Height (carriage returns)

Table.print() is a one-line shortcut to instantiating and rendering a table, and is the easiest way to output a table in development or when when debugging:

Table.print($library.items);

render(output)

Parameters:

  • output Boolean An optional flag to print the table table to the Output panel, defaults to true

Returns:

  •   String The table output

Renders the data in the class as an ASCII table

This example skips rendeing the table to the Output panel, and instead saves it to a file:

var table   = new Table($library.items);
var text    = table.render(false);

if(save('/temp/items.txt', text))
{
	alert('Items saved!');
}

Table ORDER constants

Table ORDER constants can be passed in to the constructor or print methods instead of an Array of column names to affect the ordering of columns in some obvious and some more subtle ways. This can be useful when comparing items with differing sets of object properties, especially with larger datasets.

The following section will demonstrate the 5 ORDER constants with a simple dataset:

var data =
[
    {name:'Folder 1', value:200, symbol:'folder'},
    {name:'Folder 1/Folder 2', value:50, symbol:'folder'},
    {path:'Folder 1/Folder 2/Movieclip 1', value:-7, symbol:'movie clip'},
]; 

Table.ORDER_FOUND

Sort table columns in the order they are first found. This is the default order, so you don't need to pass it in.

Table.print(data, Table.ORDER_FOUND);
+-------------------+-------+------------+-------------------------------+
| name              | value | symbol     | path                          |
+-------------------+-------+------------+-------------------------------+
| Folder 1          |   200 | folder     |                               |
| Folder 1/Folder 2 |    50 | folder     |                               |
|                   |    -7 | movie clip | Folder 1/Folder 2/Movieclip 1 |
+-------------------+-------+------------+-------------------------------+

Table.ORDER_ALPHA

Sort table columns in alphabetical order.

Table.print(data, Table.ORDER_ALPHA);
+-------------------+-------------------------------+------------+-------+
| name              | path                          | symbol     | value |
+-------------------+-------------------------------+------------+-------+
| Folder 1          |                               | folder     |   200 |
| Folder 1/Folder 2 |                               | folder     |    50 |
|                   | Folder 1/Folder 2/Movieclip 1 | movie clip |    -7 |
+-------------------+-------------------------------+------------+-------+

Table.ORDER_COLUMN

Sort table columns by the most popular columns (object keys) first. This tends to bunch the data up towards the left-hand side.

Table.print(data, Table.ORDER_COLUMN);
+------------+-------+-------------------+-------------------------------+
| symbol     | value | name              | path                          |
+------------+-------+-------------------+-------------------------------+
| folder     |   200 | Folder 1          |                               |
| folder     |    50 | Folder 1/Folder 2 |                               |
| movie clip |    -7 |                   | Folder 1/Folder 2/Movieclip 1 |
+------------+-------+-------------------+-------------------------------+

Table.ORDER_ROW

Sort table columns by the most popular rows (types, or families of object) first. This tends to bunch the data up towards the top of the table first.

Table.print(data, Table.ORDER_ROW);
+-------------------+-------+------------+-------------------------------+
| name              | value | symbol     | path                          |
+-------------------+-------+------------+-------------------------------+
| Folder 1          |   200 | folder     |                               |
| Folder 1/Folder 2 |    50 | folder     |                               |
|                   |    -7 | movie clip | Folder 1/Folder 2/Movieclip 1 |
+-------------------+-------+------------+-------------------------------+

Table.ORDER_FIRST

Sort table columns by the first row's keys only (this will hide data for some objects!)

Table.print(data, Table.ORDER_FIRST);
+-------------------+-------+------------+
| name              | value | symbol     |
+-------------------+-------+------------+
| Folder 1          |   200 | folder     |
| Folder 1/Folder 2 |    50 | folder     |
|                   |    -7 | movie clip |
+-------------------+-------+------------+

Comments are closed.