简体   繁体   中英

Data Binding with Smart table from json file

i want to bind the json file to a smart table. How to use the loop function for the iteration.. please help It only shows the design of smart table. didn't binding the data from json

this is the json file

 [ { "year": 2013, "id": "", "doctor": "Dr. Smith", "illness": "Flu", "apptdate": "3/12/2013", "details":"Patient had flu for 5 days. No medicines prescribed" } ] 

i used to retrieve data using

 @Injectable() export class SmartTablesService { constructor(private http: Http) { } smartTableData = []; loadData() { console.log('loadData'); this.http.get('http://192.168.0.100:8000/medical') .subscribe((data) => { setTimeout(() => { var contactData = []; $.each(data.json(), function (key, value) { var tempData = value.source; contactData.push(tempData); }); this.smartTableData = contactData; }, 1000); }); } getData(): Promise<any> { console.log("Promise"); this.loadData(); return new Promise((resolve, reject) => { setTimeout(() => { console.log(this.smartTableData); resolve(this.smartTableData); }, 3000); }); } } constructor(private http: Http) { } getComments() { return this.http.get('http://192.168.0.100:8000/article' ) .map((res: Response) => res.json()) .catch((error:any) => Observable.throw(error)); } }*/ 

this is the component part

 @Component({ selector: 'new', template: '<ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>' }) export class NewComponent { query: string = ''; settings = { noDataMessage: 'Loading...', columns: { year: { title: 'YEAR', type: 'string' }, id: { title: 'ID', type: 'string' }, doctor: { title: 'DOCTOR', type: 'string' }, illness: { title: 'ILLNESS', type: 'string' }, apptdate: { title: 'APPTDATE', type: 'string' }, details: { title: 'DETAILS', type: 'string' } } }; // data source: LocalDataSource = new LocalDataSource(); constructor(protected service: SmartTablesService){ this.service.getData().then((data) => { this.source.load(data); }); } } 

please anyone anyone know how to bind it ..help

simply change the subscribe part in the service page to

 var tempData = value; 

so .subscriber looks like

 .subscribe((data) => { setTimeout(() => { var contactData = []; $.each(data.json(), function (key, value) { var tempData = value; contactData.push(tempData); }); this.smartTableData = contactData; }, 1000); }); } 

it works..!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM