How to add hyperlinks to text displayed in grid

How can I add hyperlink to text in one of the column in the grid?

3 Replies

RN Rahul Narayanasamy Syncfusion Team July 30, 2018 07:32 AM UTC

Hi Nithin, 
 
Thanks for contacting Syncfusion support. 
 
Query: How can I add hyperlink to text in one of the column in the grid? 
 
We have validated your query and you can achieve your requirement by using the below method. You can add hyperlink to one of the columns in the grid by using column template. Please find the below code example and documentation link for your reference. 
 
[Code example] 
<div> 
    <ejs-grid id="Grid" actionComplete="actionComplete" allowPaging="true"> 
        <e-datamanager url="/Home/UrlDatasource" adaptor="UrlAdaptor"></e-datamanager> 
        <e-grid-editSettings allowAdding="true"></e-grid-editSettings> 
        <e-grid-pagesettings pageCount="5"></e-grid-pagesettings> 
        <e-grid-columns> 
            <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" validationRules="@(new { required=true})" textAlign="Right" width="120"></e-grid-column> 
            <e-grid-column field="CustomerID" headerText="Customer Name" validationRules="@(new { required=true})" width="150"></e-grid-column> 
            <e-grid-column field="Freight" headerText="Freight" validationRules="@(new { required=true})" textAlign="Right" editType="numericedit" format="C2" width="120"></e-grid-column> 
            <e-grid-column field="ShipCity" headerText="Ship City" template="#template" width="170"></e-grid-column> 
            <e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column> 
        </e-grid-columns> 
    </ejs-grid> 
</div> 
 
<script id="template" type="text/x-template"> 
    <div> 
        <a rel='nofollow' href="#">${ShipCity}</a>     //put your hyperlink here  
    </div> 
</script> 

We have prepared a simple sample based on your requirement. Please find the sample in below link. 
 


Please get back to us if you need further assistance on this. 

Regards, 
Rahul Narayanasamy. 



NB Nithin Bodla July 30, 2018 08:08 AM UTC

Sorry I had forgot to mention,I am not explicitly specifying columns in my grid, they are auto generated from DataSource.
How to template the columns which are auto generated?
How to add extra columns to grid which has auto generated columns(from data source)?
Error while trying to access the selected row data:
...
<script> 
    function rowSelected(args) {  
        var grid = $("#Grid").ejGrid("instance"); 
        ....


PK Prasanna Kumar Viswanathan Syncfusion Team August 3, 2018 10:22 AM UTC

Hi Nithin, 

Query 1: How to template the columns which are auto generated? 
 
We can achieve your requirement using the create event and columns method of ejGrid control. In the below code example we have update the column template for the CustomerID column in Grid dataSource. 
 

@(Html.EJ().Grid<OrdersView>("FlatGrid") 
        .Datasource((IEnumerable<object>)ViewBag.datasource) 
        .AllowSorting() 
        
        .ClientSideEvents(evs=> evs.Create("onGridCreateEvent"))) 
) 
 
<script type="text/javascript"> 
 
   function onGridCreateEvent(args) { 
 
        for (var i = 0; i < this.model.columns.length; i++) { 
 
            if (this.model.columns[i]['field'] == 'CustomerID') { 
                this.model.columns[i]['template'] = "#template"; 
            } 
        } 
        this.columns(this.model.columns); 
    } 
 
</script> 
 
<script id="template" type="text/x-jsrender"> 
    <div> 
        <a rel='nofollow' rel='nofollow' href="#">{{:CustomerID}}</a> 
    </div> 
</script> 


Refer the help documentation. 

 


Query 2: How to add extra columns to grid which has auto generated columns(from data source)? 
 
By default, all the fields which is in the dataSource get bind to the Grid columns. If you want to add any additional column in Grid then we suggest you to use the columns method of ejGrid control. 
 
Refer the help documentation. 
 

Query 3: Error while trying to access the selected row data ? 
 
As per your given detail you have used rowSelected event of ejGrid. The rowSelected event will be triggered after the row is selected. In this event we can access the selected row data using arguments.  
 
Refer the below code example. 
 
 
@(Html.EJ().Grid<OrdersView>("FlatGrid") 
        .Datasource((IEnumerable<object>)ViewBag.datasource) 
         
          --- 
 
       .ClientSideEvents(evs=> evs.RowSelected("onGridRowSelectedEvent")) 
) 
 
<script type="text/javascript"> 
 
    function onGridRowSelectedEvent(args) { 
 
        var selectedData = args.selectedData; // you can get the selected data 
    } 
 
</script> 
 
 
Refer the help documentation. 
 

If you still facing the same issue then please provide the following details for better assistance. 

  1. Share the stack trace detail if you facing any script errors in console window.
  2. Share the full Grid code example.
  3. Share the Essential Studio version details.
 
We have prepared a sample and it can be downloadable from the below location. 
 

Regards, 
Prasanna Kumar N.S.V 


Loader.
Up arrow icon