Skip to content

Commit 4e49ed9

Browse files
David Martínez RosDavid Martínez Ros
authored andcommitted
make a component for load json files
1 parent 62a796d commit 4e49ed9

File tree

9 files changed

+136
-8
lines changed

9 files changed

+136
-8
lines changed

src/app/app.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<h1>Hello from the {{ title }}!</h1>
22

3+
<app-load></app-load>
4+
35
<app-tree></app-tree>

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { HttpModule } from '@angular/http';
66
import { AppComponent } from './app.component';
77
import { NodeTreeComponent } from './node-tree/node-tree.component';
88
import { TreeComponent } from './tree/tree.component';
9+
import { LoadComponent } from './load/load.component';
910

1011
@NgModule({
1112
declarations: [
1213
AppComponent,
1314
NodeTreeComponent,
14-
TreeComponent
15+
TreeComponent,
16+
LoadComponent
1517
],
1618
imports: [
1719
BrowserModule,

src/app/load/load.component.css

Whitespace-only changes.

src/app/load/load.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Load the file <input type="file" name="file" [(ngModel)]="model"/>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* tslint:disable:no-unused-variable */
2+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
4+
import { DebugElement } from '@angular/core';
5+
6+
import { LoadComponent } from './load.component';
7+
8+
describe('LoadComponent', () => {
9+
let component: LoadComponent;
10+
let fixture: ComponentFixture<LoadComponent>;
11+
12+
beforeEach(async(() => {
13+
TestBed.configureTestingModule({
14+
declarations: [ LoadComponent ]
15+
})
16+
.compileComponents();
17+
}));
18+
19+
beforeEach(() => {
20+
fixture = TestBed.createComponent(LoadComponent);
21+
component = fixture.componentInstance;
22+
fixture.detectChanges();
23+
});
24+
25+
it('should create', () => {
26+
expect(component).toBeTruthy();
27+
});
28+
});

src/app/load/load.component.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Observable } from 'rxjs';
3+
4+
import { Node } from '../node.class';
5+
6+
import { ReturnsJsonArrayService } from '../returns-json-array.service';
7+
8+
@Component({
9+
selector: 'app-load',
10+
templateUrl: './load.component.html',
11+
styleUrls: ['./load.component.css'],
12+
providers: [ReturnsJsonArrayService]
13+
})
14+
export class LoadComponent implements OnInit {
15+
16+
model: any;
17+
18+
static tree: Observable<Array<Node>>;
19+
20+
constructor(private service: ReturnsJsonArrayService) {
21+
this.loadFile();
22+
console.log("AppComponent.data:" + LoadComponent.tree);
23+
}
24+
25+
ngOnInit() {
26+
27+
}
28+
29+
loadFile() {
30+
if(this.model != null) {
31+
LoadComponent.tree = this.service.getTreeFromFile(this.model);
32+
}
33+
LoadComponent.tree = this.service.getTree();
34+
}
35+
36+
}

src/app/returns-json-array.service.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,15 @@ export class ReturnsJsonArrayService {
1818
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
1919
}
2020

21+
getTreeFromFile(model: any): Observable<any> {
22+
/*return this.http.request('./data/people.json')
23+
.map(res => res.json());*/
24+
25+
return this.http.get(model)
26+
// ...and calling .json() on the response to return data
27+
.map((res:Response) => res.json())
28+
//...errors if any
29+
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
30+
}
31+
2132
}

src/app/tree/tree.component.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Observable } from 'rxjs';
33

4-
import { ReturnsJsonArrayService } from '../returns-json-array.service';
5-
64
import { Node } from '../node.class';
75

6+
import { LoadComponent } from '../load/load.component';
7+
88
@Component({
99
selector: 'app-tree',
1010
templateUrl: './tree.component.html',
11-
styleUrls: ['./tree.component.css'],
12-
providers: [ReturnsJsonArrayService]
11+
styleUrls: ['./tree.component.css']
1312
})
14-
export class TreeComponent {
13+
export class TreeComponent implements OnInit {
1514

1615
tree: Observable<Array<Node>>;
1716

18-
constructor(private service: ReturnsJsonArrayService) {
19-
this.tree = service.getTree();
17+
constructor() {
18+
this.tree = LoadComponent.tree;
2019
console.log("AppComponent.data:" + this.tree);
2120
}
2221

22+
ngOnInit() {
23+
24+
}
25+
2326
}

src/data/tree.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@
6868
"name": "Node 2.3",
6969
"idParent": 2,
7070
"sheet": true
71+
},
72+
{
73+
"id": 24,
74+
"name": "Node 2.4",
75+
"idParent": 2,
76+
"sheet": true
77+
},
78+
{
79+
"id": 25,
80+
"name": "Node 2.5",
81+
"idParent": 2,
82+
"sheet": true
7183
}
7284
]
7385
},
@@ -104,6 +116,39 @@
104116
"name": "Node 3.3",
105117
"idParent": 3,
106118
"sheet": true
119+
},
120+
{
121+
"id": 34,
122+
"name": "Node 3.4",
123+
"idParent": 3,
124+
"sheet": false,
125+
"childs":
126+
[
127+
{
128+
"id": 341,
129+
"name": "Node 3.4.1",
130+
"idParent": 34,
131+
"sheet": true
132+
},
133+
{
134+
"id": 342,
135+
"name": "Node 3.4.2",
136+
"idParent": 34,
137+
"sheet": true
138+
},
139+
{
140+
"id": 343,
141+
"name": "Node 3.4.3",
142+
"idParent": 34,
143+
"sheet": true
144+
},
145+
{
146+
"id": 344,
147+
"name": "Node 3.4.4",
148+
"idParent": 34,
149+
"sheet": true
150+
}
151+
]
107152
}
108153
]
109154
}

0 commit comments

Comments
 (0)