DevExpress.Blazor项目定制DataGridToolbar工具栏

前端DataGridToolbar.razor根据实现需要增加工具栏链接

@inherits DataGridToolbarBase


                       BeginGroup="true"
                   Click="@OnClearButtonClick"
                   IconCssClass="mdi mdi-18px mdi-minus-box-outline"
                   CssClass="font-bigger"
                   Vislble="@ClearButtonVisible"/>
                       BeginGroup="true"
                   Click="@OnAddButtonClick"
                   IconCssClass="mdi mdi-18px mdi-file-outline"
                   Enabled="@(IsBusy == false && CanCreate)"
                   CssClass="font-bigger"
                   Visible="@(IsAddButtonVisible && AddButtonVisible && ReadOnly == false)"/>
                       Click="@OnEditButtonClick"
                   IconCssClass="mdi mdi-18px mdi-file-edit-outline"
                   Enabled="@(IsBusy == false && SelectedCount == 1 && CanEdit)"
                   CssClass="font-bigger"
                   Visible="@(IsEditButtonVisible && EditButtonVisible && ReadOnly == false)"/>
                       Click="@OnDeleteButtonClick"
                   IconCssClass="mdi mdi-18px mdi-delete-outline"
                   Enabled="@(IsBusy == false && SelectedCount >= 1 && IsPermanent == false && CanDelete)"
                   CssClass="font-bigger"
                   Visible="@(IsDeleteButtonVisible && DeleteButtonVisible && ReadOnly == false)"/>
                       Click="@OnRestoreButtonClick"
                   BeginGroup="true"
                   IconCssClass="mdi mdi-18px mdi-restore"
                   Enabled="@(IsBusy == false && SelectedCount >= 1 && IsPermanent == false && CanRestore)"
                   CssClass="font-bigger"
                   Visible="@(IsRestoreButtonVisible && RestoreButtonVisible && ReadOnly == false)"/>

    @ChildContent

                       Alignment="ToolbarItemAlignment.Right"
                   Tooltip="Show filter row"
                   Click="@OnFilterButtonClick"
                   IconCssClass="mdi mdi-18px mdi-filter-outline"
                   Enabled="@(IsBusy == false)"/>

    @if (IsShowColumnChooser)
    {
                                                    Alignment="ToolbarItemAlignment.Right"
                                            Tooltip="Column chooser"/>
    }

后端代码实现

using System.Threading.Tasks;
using DevExpress.Blazor;
using Microsoft.AspNetCore.Components;
using DataServices.Client.Services;
using DataServices.Shared.Data.Models;

namespace DataServices.Client.Components
{
    public class DataGridToolbarBase : ComponentBase
    {
        private string _cssClass = "mb-2";
        private bool _clearButtonVisible = true;
        private bool _addButtonVisible = true;
        private bool _deleteButtonVisible = true;
        private bool _editButtonVisible = true;

        private bool _isAdmin;
        private bool _isBusy;
        private bool _isShowColumnChooser = true;
        private bool _neededToRefresh;
        private TgClaimTypes _requiredClaimType;
        private bool _restoreButtonVisible = true;
        private int _selectedCount;

        protected bool IsAddButtonVisible;
        protected bool IsDeleteButtonVisible;
        protected bool IsEditButtonVisible;
        protected bool IsRestoreButtonVisible;
        [Inject] private IUserService UserService { get; set; }

        [Parameter] public RenderFragment ChildContent { get; set; }

        [Parameter] public EventCallback IsBusyChanged { get; set; }
        [Parameter] public EventCallback RequiredClaimTypeChanged { get; set; }
        [Parameter] public EventCallback SelectedCountChanged { get; set; }
        [Parameter] public EventCallback IsShowColumnChooserChanged { get; set; }
        [Parameter] public EventCallback OnToolbarClick { get; set; }
        [Parameter] public EventCallback CssClassChanged { get; set; }
        [Parameter] public EventCallback NeededToRefreshChanged { get; set; }

        [Parameter]
        public bool IsBusy
        {
            get => _isBusy;
            set
            {
                if (Equals(_isBusy, value)) return;
                _isBusy = value;
                IsBusyChanged.InvokeAsync(value);
                StateHasChanged();
            }
        }

        [Parameter]
        public TgClaimTypes RequiredClaimType
        {
            get => _requiredClaimType;
            set
            {
                if (Equals(_requiredClaimType, value)) return;
                _requiredClaimType = value;
                RequiredClaimTypeChanged.InvokeAsync(value);
                StateHasChanged();
            }
        }

        [Parameter]
        public int SelectedCount
        {
            get => _selectedCount;
            set
            {
                if (Equals(_selectedCount, value)) return;
                _selectedCount = value;
                SelectedCountChanged.InvokeAsync(value);
                StateHasChanged();
            }
        }

        [Parameter]
        public bool IsShowColumnChooser
        {
            get => _isShowColumnChooser;
            set
            {
                if (Equals(_isShowColumnChooser, value)) return;
                _isShowColumnChooser = value;
                IsShowColumnChooserChanged.InvokeAsync(value);
                StateHasChanged();
            }
        }

        [Parameter]
        public string CssClass
        {
            get => _cssClass;
            set
            {
                if (Equals(_cssClass, value)) return;
                _cssClass = value;
                CssClassChanged.InvokeAsync(value);
                StateHasChanged();
            }
        }

        [Parameter]
        public bool NeededToRefresh
        {
            get => _neededToRefresh;
            set
            {
                if (Equals(_neededToRefresh, value)) return;
                _neededToRefresh = value;
                if (_neededToRefresh) StateHasChanged();

                NeededToRefreshChanged.InvokeAsync(false);
            }
        }

        [Parameter]
        public bool ClearButtonVisible
        {
            get => _clearButtonVisible;
            set
            {
                if (Equals(_clearButtonVisible, value)) return;
                _clearButtonVisible = value;
                StateHasChanged();
            }
        }
        
        [Parameter]
        public bool AddButtonVisible
        {
            get => _addButtonVisible;
            set
            {
                if (Equals(_addButtonVisible, value)) return;
                _addButtonVisible = value;
                StateHasChanged();
            }
        }

        [Parameter]
        public bool EditButtonVisible
        {
            get => _editButtonVisible;
            set
            {
                if (Equals(_editButtonVisible, value)) return;
                _editButtonVisible = value;
                StateHasChanged();
            }
        }

        [Parameter]
        public bool DeleteButtonVisible
        {
            get => _deleteButtonVisible;
            set
            {
                if (Equals(_deleteButtonVisible, value)) return;
                _deleteButtonVisible = value;
                StateHasChanged();
            }
        }

        [Parameter]
        public bool RestoreButtonVisible
        {
            get => _restoreButtonVisible;
            set
            {
                if (Equals(_restoreButtonVisible, value)) return;
                _restoreButtonVisible = value;
                StateHasChanged();
            }
        }

        [Parameter] public bool ReadOnly { get; set; }
        [Parameter] public bool IsPermanent { get; set; }

        [Parameter] public bool CanCreate { get; set; } = true;
        [Parameter] public bool CanEdit { get; set; } = true;
        [Parameter] public bool CanDelete { get; set; } = true;
        [Parameter] public bool CanRestore { get; set; } = true;

        protected override async Task OnInitializedAsync()
        {
            await UserService.LoadAsync();
            _isAdmin = UserService.IsAdminLevel;

            var permission = await UserService.GetPermissionAsync(RequiredClaimType);

            IsAddButtonVisible = _isAdmin || permission.Value >= TgPermissions.Create.Value;
            IsEditButtonVisible = _isAdmin || permission.Value >= TgPermissions.Update.Value;
            IsDeleteButtonVisible = _isAdmin || permission.Value >= TgPermissions.Delete.Value;
            IsRestoreButtonVisible = _isAdmin;
        }

        protected void OnClearButtonClick(ToolbarItemClickEventArgs args)
        {
            var eventArgs = new TgDataGridToolbarClickEventArgs
            {
                Name = "Clear",
                MouseEventArgs = args.MouseEventArgs
            };
            OnToolbarClick.InvokeAsync(eventArgs);
        }
        
        protected void OnAddButtonClick(ToolbarItemClickEventArgs args)
        {
            var eventArgs = new TgDataGridToolbarClickEventArgs
            {
                Name = "Add",
                MouseEventArgs = args.MouseEventArgs
            };
            OnToolbarClick.InvokeAsync(eventArgs);
        }

        protected void OnEditButtonClick(ToolbarItemClickEventArgs args)
        {
            var eventArgs = new TgDataGridToolbarClickEventArgs
            {
                Name = "Edit",
                MouseEventArgs = args.MouseEventArgs
            };
            OnToolbarClick.InvokeAsync(eventArgs);
        }

        protected void OnDeleteButtonClick(ToolbarItemClickEventArgs args)
        {
            var eventArgs = new TgDataGridToolbarClickEventArgs
            {
                Name = "Delete",
                MouseEventArgs = args.MouseEventArgs
            };
            OnToolbarClick.InvokeAsync(eventArgs);
        }

        protected void OnRestoreButtonClick(ToolbarItemClickEventArgs args)
        {
            var eventArgs = new TgDataGridToolbarClickEventArgs
            {
                Name = "Restore",
                MouseEventArgs = args.MouseEventArgs
            };
            OnToolbarClick.InvokeAsync(eventArgs);
        }

        protected void OnFilterButtonClick(ToolbarItemClickEventArgs args)
        {
            var eventArgs = new TgDataGridToolbarClickEventArgs
            {
                Name = "Filter",
                MouseEventArgs = args.MouseEventArgs
            };
            OnToolbarClick.InvokeAsync(eventArgs);
        }
    }
}

你可能感兴趣的:(状态模式,c#)