DataTable Documentation is extremely confusing
Most people will be getting their data from a REST API through HTTP. There needs to be information on how this ties into the table component. What is the DataSource class? Where is the documentation on it? Currently there is not enough information provided to the end user to connect these dots.
PR for this in progress #5766
I do agree there should be more documentation on this issue. If you are looking for how to implement server data with data-table this an example how you replace the ExampleDatabase class :
export class ExampleDatabase { dataChange stores the data for the cdk/md tables
dataChange: BehaviorSubject<UserData[]> = new BehaviorSubject<UserData[]>([]);
get data(): UserData[] { return this.dataChange.value; }
Make sure to pass your http service to the constructor of the export class
constructor(private studentService: StudentService, private router: Router, private http: Http) {
this.getStudents();
}
Your getter method for the class service
getStudents() {
this.studentService.getStudents().subscribe(
accountData => this.onLoad(accountData),
error => this.handleError(error)
);
}
Error handling:
private handleError(errorMessage: string) {
alert("Error: " + errorMessage);
}
Method adds server data into the dataChange
private onLoad(accountData) {
let body = accountData;
/**You can any method for looping through your server JSON response. I just use a for loop.*/
for (var x = 0; x < body.count; x++) {
const copiedData = this.data.slice();
copiedData.push({ id: body.students[x].id, name: body.students[x].firstName + " " + body.students[x].lastName, username: body.students[x].username });
this.dataChange.next(copiedData);
}
}
}`
HTTP example is coming through soon. For more details on the implementation and background of the table, be sure to check out the cdk-table docs at https://material.angular.io/guide/cdk-table
If you have any suggestions on how the docs could be more informative or intuitive, please feel free to open up a PR that we can review and discuss.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.