Hyperlinks based on condition in cell

Hi,

I would like to add hyperlinks in my grid rows. These hyperlinks must only display based on a certain condition. Is this possible? The hyperlinks should also have parameters attached to the for the specific row.


4 Replies 1 reply marked as answer

SK Sujith Kumar Rajkumar Syncfusion Team November 15, 2021 12:34 PM UTC

Hi Eddie, 
 
Greetings from Syncfusion support. 
 
You can achieve your requirement by using the column template and conditional rendering(using if, else statements) as demonstrated in the below code snippet, 
 
<div id="ControlRegion"> 
    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col => 
   { 
       col.Field("EmployeeID").HeaderText("Employee ID").Width("120").Add(); 
       col.Field("Country").Template("#template").Width("150").Add(); 
   }).Render() 
</div> 
 
<script id="template" type="text/x-template">  
    <div> 
        ${if(Country)} 
            <a rel='nofollow' href=https://en.wikipedia.org/wiki/${Country}>${Country}</a> 
        ${else} 
            <span>Not provided</span> 
        ${/if} 
    </div> 
</script> 
 
We have prepared a sample based on this for your reference. You can find it below, 
 
 
More details on column template and conditional rendering in template can be checked in the below documentation links, 
 
                               https://ej2.syncfusion.com/aspnetmvc/documentation/grid/columns/#using-condition-template 
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 


Marked as answer

EW Eddie Willcox November 15, 2021 01:56 PM UTC

Thanks for the sample.

Can the condition be based on other cells in the grid?

As in your example provided, can the template in country column be based on a condition in the employee id column?

if employee id = 1 then rel='nofollow' href must contain employee id and country values as parameters



EW Eddie Willcox November 16, 2021 07:06 AM UTC

I have answered my own question. Yes, the condition can be based on other cells in the grid.


Thank you for your assistance



AG Ajith Govarthan Syncfusion Team November 17, 2021 02:09 AM UTC

Hi Eddie, 

Thanks for the update. 

Based on your query, you want render anchor tag elements based on other column values in your grid application. So, we have prepared sample in that we have rendered the anchor tag elements using column template based on the employee id values for Country column. For your convenience we have attached the sample, please refer them for your reference. 

Code example: 
DataGridFeatures.cshtml 

@{ 
    ViewBag.Title = "DataGridFeatures"; 
} 
 
<div id="ControlRegion"> 
    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col => 
   { 
       col.Field("EmployeeID").HeaderText("Employee ID").Width("120").Add(); 
       col.Field("FirstName").HeaderText("Name").Width("125").Add(); 
       col.Field("Title").HeaderText("Title").Width("170").Add(); 
       col.Field("Country").Template("#template").Width("150").Add(); 
       col.Field("ReportsTo").HeaderText("ReportsTo").Width("120").Add(); 
   }).Render() 
</div> 
<script id="template" type="text/x-template">  
    <div> 
        ${if(EmployeeID != 1)} 
        <a rel='nofollow' href=https://en.wikipedia.org/wiki/${Country}>${Country}</a> 
        ${else} 
        <a rel='nofollow' href=https://en.wikipedia.org/wiki/${Country} rel="nofollow">${EmployeeID} </a> 
        ${/if} 
    </div> 
</script> 


Please get back to us if you need further assistance. 

Regards, 
Ajith G. 


Loader.
Up arrow icon