mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
test and doc generateSchema
This commit is contained in:
@@ -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:
|
||||
|
@@ -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');
|
||||
});
|
||||
|
||||
});
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user