test and doc generateSchema

This commit is contained in:
Eugenio Romano
2016-07-19 10:21:22 +01:00
parent c86e23351c
commit beb37a2ded
4 changed files with 63 additions and 12 deletions

View File

@@ -307,6 +307,7 @@ a custom `DataTableAdapter` using the following interfaces:
```ts
interface DataTableAdapter {
generateSchema(row: DataRow): col: DataColumn;
getRows(): Array<DataRow>;
setRows(rows: Array<DataRow>): void;
getColumns(): Array<DataColumn>;
@@ -363,6 +364,39 @@ let data = new ObjectDataTableAdapter(
);
```
## Generate schema
Is possible to auto generate your schema if you have only the data row
```ts
let data = [
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
];
let schema = ObjectDataTableAdapter.generateSchema(data);
/*Auto generated schema value:
[
{
type: 'text',
key: 'id',
title: 'Id',
sortable: false
},
{
type: 'text',
key: 'name',
title: 'Name',
sortable: false
}
]
*/
```
## Build from sources
Alternatively you can build component from sources with the following commands:

View File

@@ -367,4 +367,17 @@ describe('ObjectDataRow', () => {
expect(row.hasValue('some.other.prop')).toBeFalsy();
});
it('should generateSchema generate a schema from data', () => {
let data = [
{ id: 2, name: 'abs' },
{ id: 1, name: 'xyz' }
];
let schema = ObjectDataTableAdapter.generateSchema(data);
expect(schema.length).toBe(2);
expect(schema[0].title).toBe('id');
expect(schema[1].title).toBe('name');
});
});

View File

@@ -32,22 +32,26 @@ export class ObjectDataTableAdapter implements DataTableAdapter {
private _rows: DataRow[];
private _columns: DataColumn[];
static generateSchema(rowToExaminate: any) {
static generateSchema(data: any[]) {
let schema = [];
if (typeof rowToExaminate === 'object') {
for (let key in rowToExaminate) {
if (rowToExaminate.hasOwnProperty(key)) {
schema.push({
type: 'text',
key: key,
title: key,
sortable: false
});
if (data && data.length) {
let rowToExaminate = data[0];
if (typeof rowToExaminate === 'object') {
for (let key in rowToExaminate) {
if (rowToExaminate.hasOwnProperty(key)) {
schema.push({
type: 'text',
key: key,
title: key,
sortable: false
});
}
}
}
}
}
return schema;
}