2017-09-26 59 views
1

我想將json文件綁定到智能表。如何使用循環功能迭代..請幫助 它只顯示智能表的設計。 沒有從JSON使用json文件中的智能表進行數據綁定

結合的數據,這是使用JSON文件

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

我用來檢索數據

@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)); 
 
     } 
 
}*/

這是組成部分

@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); 
 
    }); 
 
} 
 
}

請任何人知道如何將它綁定..help

回答

0

簡單地更改服務頁面的訂閱部分

var tempData = value;

所以.subscriber看起來

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

它的工作原理..!

相關問題