Hi Carlo,
Thanks for using Syncfusion Products.
Query: #1 adding a dynamic templated column to a syncfusion grid whose content depends on each row (let's say the ID)
You can add the columns dynamically to Grid by using the Columns method. Please refer to the following Help Document and code example,
http://helpjs.syncfusion.com/js/api/ejgrid#methods:columns
@(Html.EJ().Button("btn").Text("AddDynamic column").ClientSideEvents(eve => eve.Click("btnclick")))
<script> function btnclick() { var gridobj = $("#FlatGrid").ejGrid("instance"); var col = [{ headerText: "Template column", template: true, templateID: "#columnTemplate" }]; gridobj.columns(col, "add"); } </script> |
Query: #2 For instance, I may want to add a dynamic templated column which contains a syncfusion button with red background when the row ID is odd, and a green background when it's even. Then, if ID=3, no button should be shown at all.
You can customize the Button in TemplateRefresh event which triggers when refreshing the template column elements in the Grid. Please refer to the following Help document,
http://helpjs.syncfusion.com/js/api/ejgrid#events:templaterefresh
@(Html.EJ().Grid<object>("FlatGrid") .Datasource((IEnumerable<object>)ViewBag.datasource) .AllowScrolling() .AllowPaging() /*Paging Enabled*/ .Columns(col => . . . . . .ClientSideEvents(eve => eve.TemplateRefresh("templaterefresh")))
|
We have rendered the Button to the column in TemplateRefresh event and customized the Button based on the ID value (apply color whether odd/even).
<script>
function templaterefresh(args) { var val = args.data["EmployeeID"]; var cell = $(args.cell);
//render the button control if (val != 3) { cell.find(".template").ejButton({ width: "100px", text: "Button", click: "click" }); } if (val == 3) cell.find(".template").remove();
//customize the color to button if (val % 2 == 0) cell.find(".template").css("background-color", "red"); //if even else cell.find(".template").css("background-color", "green");//if odd
} </script> |
Query: #3 when I click on one of these buttons and I get redirected to this dynamic page, the ID of the row to which the button belongs isn't shown in the address bar.
We can redirect the Button to dynamic page in Button click event. Please refer to the below code example,
<script> function templaterefresh(args) {
//render the button control if (val != 3) { cell.find(".template").ejButton({ width: "100px", text: "Button", click: "click" }); } . . . .
}
function click(args) { var gridobj = $("#FlatGrid").ejGrid("instance"); val = gridobj.getSelectedRecords()[0]["EmployeeID"]; window.open(window.location.rel='nofollow' href + val); //redirect to dynamic page } </script> |
Please provide below information about how you wanted to redirect to the dynamic page when clicking the button that will be helpful to provide a solution at the earliest.
1. Do you want to pass the ID value to the dynamic page?
2. Do you want to pass the ID using query string but need to hide it from address bar?
Please refer to sample in following link:
Regards,
Balaji Marimuthu
Please find the response.
Query: # when I click on one of these buttons and I get redirected to this dynamic page, the ID of the row to which the button belongs isn't shown in the address bar. Maybe that should be handled with a POST request?
Your requirement is achieved by using the AJAX post and we have sent the ID value to server side. Please refer to the following sample and code example,
Sample: Forum-120073
function click(args) { var gridobj = $("#FlatGrid").ejGrid("instance"); val = gridobj.getSelectedRecords()[0]["EmployeeID"]; $.ajax({ url: "/Grid/Data", type: "POST", data: {data: val}, success: function (data) { window.open(window.location.origin + data); }, error: function (e) { } });
} |
public string Data(int? data) { Session["ID"] = data; //you can get the session value var val=""; if (data == 1) val = "/Home/Index"; else val = "/Home/About"; return val; } |
App.component.html
<ej-grid [allowpaging]="true" [allowsorting]="true" [datasource]="gridData">
<e-columns>
<e-column headertext="EmployeeImage" width="100" [template]="template"></e-column>
...
</e-columns>
</ej-grid>
App.component.ts
export class GridComponent {
public gridData;
public template;
constructor()
{
this.gridData = window.employeeView;
this.template = "#columnTemp";
. .
}
}
Index.html
<script type="text/x-jsrender" id="columnTemp">
<img style="width: 75px; height: 70px" src="app/Employees/{{:EmployeeID}}.png" alt="{{: EmployeeID }}" />
</script>
|
Template as String:-
App.component.ts
export class GridComponent {
public gridData;
public template= "<img style="width: 75px; height: 70px" src="app/Employees/{{:EmployeeID}}.png" alt="{{: EmployeeID }}" ";
constructor()
{
this.gridData = window.employeeView;
. .
}
}
</script>
Ng-template:-
<ej-grid id="Grid" [datasource]="gridData" allowpaging="true" [pagesettings]="pageSettings">
<e-columns>
<e-column headertext="Photo">
<ng-template e-template let-data>
<div *ngif="data.EmployeeID">
<img style="width: 75px; height: 70px" [attr.src]="'app/Content/images/grid/Employees/' + data.EmployeeID + '.png' " [alt]="data.EmployeeID" />
</div>
</ng-template>
</e-column>
. . .
</e-columns>
</ej-grid>
|
App.compoent.html
<ej-grid #grid [allowpaging]="true" [datasource]="gridData" [toolbarsettings]="toolbarItems" (load)="load($event)">
<e-columns>
<e-column headertext="Button" width="20">
<ng-template ngfor #link [ngforof]="links">
<a rel='nofollow' href="#" (click)="onClick(link)"></a>
</ng-template>
</e-column>
</ej-grid>
App.component.ts
export class GridComponent {
public gridData: any;
onClick(link: LinkObj) {
. . .
}
|