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
|
```ts
|
||||||
interface DataTableAdapter {
|
interface DataTableAdapter {
|
||||||
|
generateSchema(row: DataRow): col: DataColumn;
|
||||||
getRows(): Array<DataRow>;
|
getRows(): Array<DataRow>;
|
||||||
setRows(rows: Array<DataRow>): void;
|
setRows(rows: Array<DataRow>): void;
|
||||||
getColumns(): Array<DataColumn>;
|
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
|
## Build from sources
|
||||||
|
|
||||||
Alternatively you can build component from sources with the following commands:
|
Alternatively you can build component from sources with the following commands:
|
||||||
|
@@ -367,4 +367,17 @@ describe('ObjectDataRow', () => {
|
|||||||
expect(row.hasValue('some.other.prop')).toBeFalsy();
|
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 _rows: DataRow[];
|
||||||
private _columns: DataColumn[];
|
private _columns: DataColumn[];
|
||||||
|
|
||||||
static generateSchema(rowToExaminate: any) {
|
static generateSchema(data: any[]) {
|
||||||
let schema = [];
|
let schema = [];
|
||||||
|
|
||||||
if (typeof rowToExaminate === 'object') {
|
if (data && data.length) {
|
||||||
for (let key in rowToExaminate) {
|
let rowToExaminate = data[0];
|
||||||
if (rowToExaminate.hasOwnProperty(key)) {
|
|
||||||
schema.push({
|
if (typeof rowToExaminate === 'object') {
|
||||||
type: 'text',
|
for (let key in rowToExaminate) {
|
||||||
key: key,
|
if (rowToExaminate.hasOwnProperty(key)) {
|
||||||
title: key,
|
schema.push({
|
||||||
sortable: false
|
type: 'text',
|
||||||
});
|
key: key,
|
||||||
|
title: key,
|
||||||
|
sortable: false
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -163,7 +163,7 @@ export class WebscriptComponent {
|
|||||||
let datatableShow = true;
|
let datatableShow = true;
|
||||||
try {
|
try {
|
||||||
if (!data.schema) {
|
if (!data.schema) {
|
||||||
data.schema = ObjectDataTableAdapter.generateSchema(data.data[0]);
|
data.schema = ObjectDataTableAdapter.generateSchema(data.data);
|
||||||
}
|
}
|
||||||
if (data.schema && data.schema.length > 0) {
|
if (data.schema && data.schema.length > 0) {
|
||||||
this.data = new ObjectDataTableAdapter(data.data, data.schema);
|
this.data = new ObjectDataTableAdapter(data.data, data.schema);
|
||||||
|
Reference in New Issue
Block a user