Hi,
Just started using igGrid here, and was wondering what would be the best approach to show a button for each row of data in igGrid ? for instance, I want to have a 'Copy' button for each row of data.
I've been looking around and found something called rowTemplate, but the jQuery js file kept throwing error, below is the code snippet:
@(Html.Infragistics().Grid<Matrix>() .AutoGenerateColumns(false) .ID("testGrid") .RenderCheckboxes(true) .Columns(column => { column.For(x => x.ID).HeaderText(" ").Width("80"); column.For(x => x.Description).DataType("string").HeaderText("Description"); column.For(x => x.Status).DataType("string").HeaderText("Status"); column.For(x => x.EffectiveFrom).DataType("date").HeaderText("Effective From"); column.For(x => x.EffectiveTo).DataType("date").HeaderText("Effective To"); column.For(x => x.CreatedBy).DataType("string").HeaderText("Created By"); }).RowTemplate("<tr><td>test</td><tr>").JQueryTemplating(true)
Your best option is to use feature called "Column template".
Here you can find the help topic on the subject and here you can find the sample in our samples browser.
Attached you can find sample demonstrating column template for your scenario.
Here is the grid initialization code:
In the sample I've defined template which creates button for the ProductID column. I also added data-id attribute to the button so I can later use it to identify the row from the grid. There is also a onclick handler which shows the data from the data-id attribute.
Note that you need to define template on the column from the data source(i.e. you cannot define unbound column).
Hope this helps,
Martin Pavlov
Infragistics, Inc.
This is a solution for some scenarios but it does not work when you have an ID column that must be displayed and the button also need to be tied to that same key. In your scenario where you bound the Product ID to the button, if you needed to also have a column to display the Product ID and button to Remove that item (tied to Product ID), the template version would place a button in any column that is tied to the key "ProductID".
Any other suggestions for putting a button in the grid that is "associated with" but not directly tied to that key?
For example, I have a grid full of users. In the far right column, I want a button that says "Remove" for each user. The far left column has "User ID". When the user clicks the Remove button for a specific user, I need to be able to get the User ID associated with that button. I don't want the button to be bound to any key at all. I just want to know when one of the buttons was clicked what ID is associated with it so I can handle the removal in the background.
I actually just went around the problem by adding an additional property to my view model. I now have UserId and UserId2 and I just bind the button's id attribute to UserId2. Everything works fine. I wish there was a better/cleaner way though.
Off the top of my head, I can think of two solutions:
- Do you have row selection enabled ? if so, you can grab the selected row because I think the row is selected no matter where you click on the row using: $(".selector").igGridSelection("selectedRow");
$(
".selector"
).igGridSelection(
"selectedRow"
);
then grab the ID from the row object returned from that call
- If you're using .NET MVC, you can pass the Product ID in a viewbag, and put it as a property of the button, then on click you should be able to retrieve the ID from the button property