New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Table] Add example with dynamic data (add/remove/edit rows) #5917
Comments
Sounds good to me, I'll make an example that includes adding/removing/editing rows. |
I hope I can add to this request without creating a new Issue, I've been looking at the example code provided in both the angular material docs and the cdk guide and I'm struggling to understand how to just send my data (a simple array of objects) to the table. Do I need my data to be a BehaviorSubject ? Do I need the typescript get too ? The example generates data with loops and is quite confusing to me. A simpler example with just a small simple array would really help ! I'll create a new Issue if needed. |
hello @andrewseguin It would be nice if you add collapsible row too. |
Hi @andrewseguin, |
@Lakston IMO your request deserves a separate issue. While the ExampleDatabase is great as a supplier of 100 rows of random data, it convolutes the Basic Example, and I think the docs could benefit from an even simpler example with maybe a single emission of a hardcoded array of data with @ilmars luckily an example with an HTTP request just got merged yesterday and will be available on the docs site soon #5766 |
@Lakston what I did to accomplish that was to setup a service to provide the data array to the |
for anyone interested in an unpublished example: https://github.com/angular/material2/pull/5093/files https://github.com/angular/material2/tree/master/src/demo-app/table |
Some help on this would be greatly appreciated :) https://stackoverflow.com/questions/45382599/angular-2-table-invoke-connect-function |
Agreed. I wept for joy when this component arrived. But after about six hours of fiddling with this thing, I still can't get it to work with a meaningful, real-world example. I expected to just point it at an array, and I'd be off and running. But having to create a datasource and a database is especially cumbersome. If the grid is supposed to be truly unopinionated, imo it shouldn't be coupled to observables. I should be able to just point it at an array of data and an array of column definitions (like most other analogous components). |
@etovian You don't need to create a database. That's just used in the example because the example needs to generate a lot of random data to display. You barely even have to use Observables. They're used in the examples because they massively simplify manipulating the data in response to user events and are generally very powerful and flexible. If you just want to render an array once, you can use // component
_dataSubject = new BehaviorSubject<any[]>([])
constructor() {
this.dataSource = new MyDataSource(this._dataSubject)
}
updateValues(myArray: any[]) {
this._dataSubject.next(myArray)
}
// MyDataSource
constructor(private _data: BehaviorSubject<any[]>) { }
connect() {
return this._data.asObservable()
} |
@willshowell Thanks for the tip! I sort of got this working:
The only problem is that the data doesn't load when the component using the dataSource initializes. Oddly, if I click elsewhere on the page (not even inside the component), then table displays the data. Here's all of the code:
And the template:
|
@willshowell Oddly, this is working exactly as expected:
Since this much better represents how I'd do things in production, I'm not necessarily concerned with why the code from my previous post isn't working. Thanks again for your help! You definitely saved me some continued hair-pulling. I can certainly get by with this, but I think that the option to work directly with an array would help folks get up and running much more quickly, even if the DataSource is a more performant solution. Thanks to all the devs for their hard work! |
@willshowell What does your getInvitations() method look like in your InvitationService service? |
|
I am not getting any error but the data from web API is not rendering in datatable. Only columns are displayed. Below is my code: ` @component({ export class UserListComponent implements OnInit {
} export class userDataSource extends DataSource{
} Below is my html `
User List using Table
First Name
{{element.FirstName}}
`
Not getting where exactly the issue is. |
@etovian Can you provide an example of filtering such table? |
EXPLAIN ME please, HOW does it work? When I add new item to data source's array – table doesn't show it immediately. It shows no changes until I click on header for sorting or paginator and only after that table will re-render. |
@shrralis I think the key in this line - https://github.com/angular/material2/blob/master/src/material-examples/table-filtering/table-filtering-example.ts#L68 |
@shalugin for my sorry unfortunately no. I already have something like that:
I know the code looks strange but this is because of my experiments on this to make it work. I can make plnkr with my code. |
Hmmm..
Maybe this indicating a problem. Try insert ChangeDetectorRef into contructor and trigger changes manually.
|
I'm trying with ApplicationRef (tick() method) because of I'm using a service. But there is no effect.
|
@shrralis Can you create a plunker or stackblitz with your code? |
@shalugin here it is: https://stackblitz.com/edit/angular-h3ckjw |
Your plunker code is not really working would you please check on your dependencies @irossimoline |
@rima-smart , I have updated the dependencies. |
Here is what worked for me.
|
Is there something less hacky than using this.dataSource.filter = ""; after pushing data, to get a re-render? Also, I add new data, and new rows are added, but the cells in the new row are empty...even though the new data is good. If I open "Dev Tools" and click around and inspect, then the table will render the cells. So I know something is wrong with this lib. |
You can use the method called |
Took me some time but I finally got everything working. So, here's my CRUD implementation if anyone gets in trouble with this: Or you can test it on gist: |
@marinantonio cool, how did you create that GIF? |
I tried this: self.dataSource.data.push(m as any);
self.dataSource._updateChangeSubscription(); unfortunately didn't work...my table renders new rows, but the rows are empty - the cells don't get rendered. This is all very unfortunate. What kind of library is this? I can't add rows to a table? FML. |
@ORESoftware |
@andrewseguin I want to create mat table with dynamic columns and I have array of object in which each object has fields which i want to show it vertically. In above image I have object where all column fields are in one object and I want to show columns of every object in array. |
@marinantonio how to post it permanently you are just pushing it temporary how to post it using post method in angular2 |
@chethan1095 What do you mean? You push post method from DataService.ts and then depending on result (successful or not) you push front-end update to a mat-table. |
I need some help, in rendering the value in the data table when the datasource has multiple json array. below is the service json response where i'm trying to iterate the response and bind them to material datatable. It is all good but i can't access the attributes inside the clientHeaders array. here is my material table html statements and it is not returning any value.
|
@andrewseguin Great job man! Can you please share a link to the repository for the updates you made that includes adding/removing/editing rows and if possible, editing individual cells. Thank you. |
Are the examples still coming to the documentation? |
Vielen Dank für Ihre Nachricht.
Ich bin vom 25. Juni bis 1. Juli im Urlaub. Ab Montag, dem 2. Juli bin ich wieder gerne für Sie erreichbar.
Ihre E-Mail wird an info@bynary.de weitergeleitet. In dringenden Fällen wenden Sie sich bitte an Sebastian Homeier (E-Mail: s.homeier@bynary.de, Tel.: +49 (0) 941 462 971 30).
Danke für Ihr Verständnis!
Viele Grüße
Johannes Homeier
|
@andrewseguin This is really important! Is an example coming out soon? |
Hello, I have problem with my code. Any ideas? Thank you ! https://stackoverflow.com/questions/51478019/binding-the-data-into-mat-table-angular |
@irossimoline I liked your solution, Can I use it with reactive forms and formArray? |
I created a library to solve that (seamless http paginating/sorting/filtering with MatTable), the docs are not ready yet, but you have a complete functional usage example in src/app: https://github.com/avatsaev/ngx-mat-table-extensions If any adventurous folks want to try it out and let me know if I need to add anything else before official release? online demo is here: https://stackblitz.com/github/avatsaev/ngx-mat-table-extensions |
this.dataSource.data = [...this.dataSource.data, {}]; |
1 similar comment
this.dataSource.data = [...this.dataSource.data, {}]; |
Any updates on this issue? |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug, feature request, or proposal:
cdk table API docs need instructions and/or an example of updating the dataSource in the table
connecting the table to a data source
What is the expected behavior?
seeing implementation instructions and/or an example of how to send the updated dataSource to the table so that the connect function is called
What is the current behavior?
The docs only cover rendering data on initial load of the component, but if a service updates the data, there isn't a documented way to update the data-table
What are the steps to reproduce?
Providing a Plunker (or similar) is the best way to get the team to see your issue.
Plunker template: https://goo.gl/DlHd6U
What is the use-case or motivation for changing an existing behavior?
the md-table API doc hints at an approach but also lacks clarity
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
angular 4
material 2
typescript 2.4
windows
Is there anything else we should know?
The text was updated successfully, but these errors were encountered: