Display Foreign Key Field in Responsive Column Template

1 Answer 12 Views
Grid
Reafidy
Top achievements
Rank 1
Iron
Reafidy asked on 22 Apr 2024, 12:04 AM | edited on 27 Apr 2024, 07:55 AM

I have a kendo grid with fields as below:

columns.Bound(p => p.FlightID).Visible(false);
columns.Bound(p => p.FlightDate).Format("{0:d}").Media("(min-width: 450px)");
columns.ForeignKey(p => p.AircraftID, @Model.Aircraft, "AircraftID", "Registration").Title("Aircraft").EditorTemplateName("ComboBox").Media("(min-width: 450px)");
... more columns
columns.Template("#=resColTemplate(data)#").Title("Record").Media("(max-width: 450px)");

And a responsive column template:

<script id="responsive-column-template" type="text/x-kendo-template">
    <p class="col-template-val"><strong>Date: </strong>#=kendo.toString(FlightDate, "dd/MM/yyyy")#</p>
    <p class="col-template-val"><strong>Registration: </strong>#=data.AircraftID#</p>
</script>
Razor PageModel:
public class IndexModel : PageModel
{
    public IndexModel(ApplicationDbContext context)
    {
        _context = context;
    }

   private readonly ApplicationDbContext _context;

   public IEnumerable<Models.Aircraft> Aircraft { get; set; }

   public IEnumerable<Models.Person> Person { get; set; }

   public IEnumerable<Models.Organisation> Organisation { get; set; }

   public async Task OnGet()
   {
       Aircraft = await _context.Aircraft.AsNoTracking().ToListAsync();
       Person = await _context.Person.AsNoTracking().ToListAsync();
       Organisation = await _context.Organisation.AsNoTracking().ToListAsync();
   }
   public async Task<IActionResult> OnPostRead([DataSourceRequest] DataSourceRequest request)
   {
       var flightLogs = await _context.FlightLog.AsNoTracking().ToList().ToDataSourceResultAsync(request);

       return new JsonResult(flightLogs);
   }
....

Instead of the AircraftID field, I would like to display Registration from the ForeignKey field.  Note the ForeignKey field is populated from another model.

Is that possible?

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 01 May 2024, 03:28 PM

Hello Reafidy,

You can access the ForeignKey column data through the "values" property of the Grid columns. For example:

<script id="responsive-column-template" type="text/x-kendo-template">
    # var foreignKeyColData = $("\\#grid").getKendoGrid().columns[2].values; # // Replace "2" with the index of the ForeignKey column
    # var getvalue = $.grep(foreignKeyColData, function(indx, item){ return (indx.value == data.AircraftID) });#
    <p class="col-template-val"><strong>Date: </strong>#=kendo.toString(FlightDate, "dd/MM/yyyy")#</p>
    <p class="col-template-val"><strong>Registration: </strong>#=getvalue[0].text#</p>
</script>

I hope that helps.

 

Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Tags
Grid
Asked by
Reafidy
Top achievements
Rank 1
Iron
Answers by
Mihaela
Telerik team
Share this question
or