This is a migrated thread and some comments may be shown as answers.

Show/Hide Command Button based on data content in field

2 Answers 1695 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Veteran
Jason asked on 26 May 2020, 08:02 PM

Good afternoon, 

 

I am relatively new developer. I am working the the Telerik Grid and it is fantastic! The sorting and searching and editing I love it!

I have made my own custom command button that when clicked pops a modal dialog that displays a pdf. The link to display this pdf is stored in the database. All of this works great!

Now I would like to only display a button or enable this button if this field has something in it.

I am not picky about how its done either. I have tried to bind the enable event to a Has_Link boolean field...I have tried a bunch of stuff.

 

I realize this probably has more to do with my lack of knowledge than it does the control itself. If someone could point me in the right direction, I would be grateful!

Here is my code below.

<AuthorizeView>
    <Authorized>
        <h3>Search Permit</h3>
 
 
        @if (Permits == null)
        {
            <p><em>Loading...</em></p>
        }
        else
        {
            <TelerikGrid Data=@Permits EditMode="@GridEditMode.Incell" @ref="Grid" Sortable="true" FilterMode="GridFilterMode.FilterMenu" Class="table-striped"
                         Height="750px"
                         Pageable="true" PageSize=@PageSize
                         OnUpdate=@UpdateItem OnDelete=@DeleteItem>
                <GridToolBar>
                    <br />
                </GridToolBar>
                <GridColumns>
                    <GridColumn Field=@nameof(Permit.Permit_NO) Title="Permit Number" />
                    <GridColumn Field=@nameof(Permit.Parcel) />
                    <GridColumn Field=@nameof(Permit.PD_Owner) Title="Owner Name" />
                    <GridColumn Field=@nameof(Permit.Submission_Date) Title="Submit Date" />
                    <GridColumn Field=@nameof(Permit.Permit_Issue_Date) Title="Issue Date" />
                    <GridColumn Field=@nameof(Permit.Permit_Type) Title="Permit Type" />
                    <GridColumn Field=@nameof(Permit.Estimated_Cost) Title="Cost" />
                    <GridColumn Field=@nameof(Permit.Fee) Title="Cost" />
                    <GridColumn Field=@nameof(Permit.Note) />
                    <GridColumn Field=@nameof(Permit.Image_Link) />
                    <GridColumn Field=@nameof(Permit.Has_Link) />
                    <GridCommandColumn>
                        <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
                        <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Save</GridCommandButton>
                        <GridCommandButton Enabled="ShowImageIsEnabled" OnClick="Show_Image" Icon="information">Show Permit</GridCommandButton>
                    </GridCommandColumn>
                </GridColumns>
            </TelerikGrid>
        }
    </Authorized>
    <NotAuthorized>
        <h1>Access Denied!</h1>
    </NotAuthorized>
</AuthorizeView>
 
@code {
    private List<PermitModel> Permits = new List<PermitModel>();
    private PermitModel Permit = new PermitModel();
 
    public TelerikGrid<PermitModel> Grid { get; set; }
 
    public bool ShowImageIsEnabled { get; set; } = true;
 
    int PageSize = 15;
 
    protected override async Task OnInitializedAsync()
    {
        Permits = await _db.GetPermitData();
 
        await SetGridSort();
 
    }
 
    private void DeleteItem(GridCommandEventArgs args)
    {
        var argsItem = args.Item as PermitModel;
 
        Permits.Remove(argsItem);
        _db.DeletePermit(argsItem);
 
    }
 
    private void UpdateItem(GridCommandEventArgs args)
    {
        var argsItem = args.Item as PermitModel;
        var index = Permits.FindIndex(i => i.Record_ID == argsItem.Record_ID);
        if (index != -1)
        {
            Permits[index] = argsItem;
        }
 
        _db.DeletePermit(argsItem);
        _db.InsertPermit(argsItem);
    }
 
    async Task SetGridSort()
    {
        GridState<PermitModel> desiredState = new GridState<PermitModel>()
        {
            SortDescriptors = new List<SortDescriptor>()
        {
                new SortDescriptor { Member = "Permit_NO", SortDirection = ListSortDirection.Descending }
 
            }
        };
 
        await Grid.SetState(desiredState);
 
 
    }
 
    void Show_Image(GridCommandEventArgs args)
    {
 
        var argsItem = args.Item as PermitModel;
        var parameters = new ModalParameters();
        parameters.Add(nameof(ImageModal.image), argsItem.Image_Link);
        Modal.Show<ImageModal>(argsItem.Permit_NO, parameters);
    }
}

2 Answers, 1 is accepted

Sort by
0
Svetoslav Dimitrov
Telerik team
answered on 28 May 2020, 01:38 PM

Hello Jason,

There is an open Feature Request on our Feedback Portal for GridCommandColumn that has the model context. You can see it from this link: https://feedback.telerik.com/blazor/1461283-pass-the-model-context-to-command-button. I have given a Vote for it on your behalf, in order to raise its popularity. You can Follow it to receive email notification on status updates. Also, in that thread I have posted a workaround solution on how to make a custom GridCommandColumn that has the context so you can show or hide buttons based on data from your model.

Regards,
Svetoslav Dimitrov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Jason
Top achievements
Rank 1
Veteran
answered on 28 May 2020, 07:26 PM
Oh, great! Thank you very much!
Tags
Grid
Asked by
Jason
Top achievements
Rank 1
Veteran
Answers by
Svetoslav Dimitrov
Telerik team
Jason
Top achievements
Rank 1
Veteran
Share this question
or