I'd like to have my own button invoke the GridColumnMenu... can this be done?
1 Answer, 1 is accepted
0
Marin Bratanov
Telerik team
answered on 30 Apr 2021, 03:31 PM
Hi Jeffrey,
You could use JS Interop to click the appropriate element.
Here's an example I made for you:
@inject IJSRuntime _js
<TelerikButton OnClick="@ShowMenuForColumn2">show the menu for the second column</TelerikButton>
@* Move this script to its proper location in your project
It is here with the suppress-error hack to make the sample easy to copy*@
<script suppress-error="BL9992">
functionclickColumnMenu(gridSelector, colIndex) {
let colMenuElems = document.querySelectorAll(gridSelector + " .k-grid-column-menu");
let desiredElem = (colMenuElems != null && colMenuElems.length >= colIndex) ? colMenuElems[colIndex] : null;
debugger;
if (desiredElem) {
desiredElem.click();
}
}
</script>
<TelerikGridData="@MyData"Class="target-grid"Pageable="true"PageSize="5"FilterMode="@GridFilterMode.FilterMenu"Sortable="true"ShowColumnMenu="true"><GridColumns><GridColumnField="@(nameof(SampleData.Id))"Width="80px" /><GridColumnField="@(nameof(SampleData.Name))"Title="Employee Name"Groupable="false" /><GridColumnField="@(nameof(SampleData.Team))"Title="Team" /><GridColumnField="@(nameof(SampleData.HireDate))"Title="Hire Date" /></GridColumns></TelerikGrid>
@code {
async Task ShowMenuForColumn2()
{
await _js.InvokeVoidAsync("clickColumnMenu", ".target-grid", 1);
}
public IEnumerable<SampleData> MyData = Enumerable.Range(1, 30).Select(x =>new SampleData
{
Id = x,
Name = "name " + x,
Team = "team " + x % 5,
HireDate = DateTime.Now.AddDays(-x).Date
});
public classSampleData{
public int Id { get; set; }
public string Name { get; set; }
public string Team { get; set; }
public DateTime HireDate { get; set; }
}
}
I don't think it will be considered anytime soon. Looking into that would weigh down the entire API of the component a lot, and is likely to make a lot of things cumbersome - if nothing else, the columns would have to start exposing references or other form of unique identification, and such a method would likely boil down to similar code to the js interop anyway.