new FlexTable(headers, values)
Constructor. It accepts an another FlexTable-like object (will be copied by reference). It also accepts 2 arrays: headers and values.
Parameters:
Name | Type | Description |
---|---|---|
headers |
Array.<string>
|
TableSkel
|
null
|
array of strings (headers) or an object with headers and values. |
values |
Array.<string>
|
null
|
rows. |
Methods
appendRow(row)
Append a row.
let table = new FlexTable(["col1"]);
// 0 rows
table.appendRow(["first"]);
// 1 row
table.appendRow(["last"]);
// 2 rows
Parameters:
Name | Type | Description |
---|---|---|
row |
Array.<col>
|
Row to append to the values. |
Throws:
if the number of cols doesn't match the number of headers.
appendRows(rows)
Append rows.
It calls #appendRow
for every row passed in the rows
param.
Parameters:
Name | Type | Description |
---|---|---|
rows |
Array.<row>
|
Rows to append to the values. |
clone() → {Table}
Clone the table headers and values. It instantiates a new table (by value).
format(type, fmt) → {string}
Format the table as a string.
let table = new FlexTable(["a", "b", "c"], [[1.23, 1.02, "str"], [2, 2.335, ""]]);
let str = table.format('markdown', { header: '%s', float: '%.1f'});
// saves a markdown table in the variable. Every float value will be with 1 decimal.
console.log(table.format('md', { float: '%.1f', columns: [null, '%.2f', null]}));
// prints a markdown table. Every float is printed with 1 decimal, but the second column with 2 decimals.
console.log(table.format('csv');
// prints the table as CSV
Parameters:
Name | Type | Description |
---|---|---|
type |
formatter
|
Formatter type. |
fmt |
format
|
Format style. |
Returns:
- Type:
-
string
Table formatted.
modifyRows(remove, fn)
Modify rows by a function.
// remove all rows since the third row
table.modifyRows(true, (row, i) => {
return i >= 2;
})
// increment col2 from rows with col1 == "yes"
let idx = table.idx;
table.modifyRows(false, (row, i) => {
return row[idx['col1']] === 'yes' ? row[idx['col2']]++ : null;
})
Parameters:
Name | Type | Description |
---|---|---|
remove |
boolean
|
Remove the row if the function returns true. |
fn |
function
|
Function to be executed per row. |
reset(headers, values)
It accepts an another FlexTable-like object (will be copied by reference). It also accepts 2 arrays: headers and values.
Parameters:
Name | Type | Description |
---|---|---|
headers |
Array.<string>
|
TableSkel
|
null
|
array of strings (headers) or an object with headers and values. |
values |
Array.<string>
|
null
|
rows. |
setColumn(name, values)
Set column with values.
let table = new FlexTable(["col1"], [[1], [2]]);
table.setColumn("col2", ["a", "b"]);
// table has headers ["col1", "col2"] and values [[1, "a"], [2, "b"]]
table.setColumn("col1", [-1, null]);
// table has headers ["col1", "col2"] and values [[-1, "a"], [null, "b"]]
table.setColumn("col1", null);
// table has headers ["col2"] and values [["a"], ["b"]]
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
Header/Column name. |
values |
Array.<col>
|
Column values for every row in values. |
setColumns(names, values)
Set columns with values.
let table = new FlexTable(["col1"], [[1], [2]]);
table.setColumns(["col2", "col1"], [["a", "b"], null]);
// table has headers ["col2"] and values [["a"], ["b"]]
Parameters:
Name | Type | Description |
---|---|---|
names |
Array.<string>
|
Header/Column names. |
values |
Array.<Array.<col>>
|
Column values for every row in values (an array per column). |
setRow(i, row, replace)
Set a row in the values.
// suppose there are at 2 rows at the beginning
table.setRow(0, ["first"], false); // will insert ["first"] in the first row
// now there are 3 rows
table.setRow(2, ["third"], false); // will insert ["third"] in the third row
// total: 3 rows
table.setRow(2, ["none"], true); // will replace ["third"] with ["none"] in the third row
// total: 3 rows
#appendRow
should be used to append a row or insert the first row in an empty table (no values)
Parameters:
Name | Type | Description |
---|---|---|
i |
int
|
Row number to be replaced/inserted. |
row |
Array.<col>
|
Row to append to the values. |
replace |
boolean
|
If the row should replace the existing row. |
sort(mapchain)
Sorts the values. It modifies the rows order in the table.
let table = new FlexTable(["col1", "col2"], [[1, "a"], [2, "b"]]);
table.sort(['col1', '>num']);
// table has headers ["col1", "col2"] and values [[2, "b"], [1, "a"]]
table.sort(['col1', '<num']);
// table has headers ["col1", "col2"] and values [[1, "a"], [2, "b"]]
table = new FlexTable(['ts', 'evname', 'time'],
[
[123, 'begin', 0.0],
[123, 'start', 3.1],
[123, 'end', 4.44],
[124, 'begin', 0.0],
[124, 'start', 2.5],
[124, 'end', 4.1],
]);
let mapchain = [
['ts', function(a, b, i){ // like <num in this case
let ai = a[i];
let bi = b[i];
let less = ai < bi;
let eq = ai === bi;
if (eq){
return 0;
}else{
return less ? -1 : 1;
}
}],
['time', '>num'],
];
// table has:
// {
// headers: [ 'ts', 'evname', 'time' ],
// values: [
// [ 123, 'end', 4.44 ],
// [ 123, 'start', 3.1 ],
// [ 123, 'begin', 0 ],
// [ 124, 'end', 4.1 ],
// [ 124, 'start', 2.5 ],
// [ 124, 'begin', 0 ]
// ]
// }
Parameters:
Name | Type | Description |
---|---|---|
mapchain |
Array.<mapchain>
|
Sorting rules. |