コード例 #1
0
        public void AddColumn(BTableColumnBase column)
        {
            if (Table.DataType == null)
            {
                throw new BlazuiException($"表格 {Table.GetType().Name} 没有设置 DataType 属性");
            }
            if (column.Property == null && !(column is BTableTemplateColumnBase))
            {
                throw new BlazuiException($"列 {column.Text} 没有设置 {nameof(BTableColumnBase.Property)} 属性");
            }

            PropertyInfo property = null;

            if (!string.IsNullOrWhiteSpace(column.Property))
            {
                property = Table.DataType.GetProperty(column.Property);
                if (property == null)
                {
                    throw new BlazuiException($"属性 {column.Property} 在 {Table.DataType.Name} 中不存在");
                }
            }
            var columnConfig = new TableHeader
            {
                Property = property,
                Eval     = column.Property == null ? null : (Func <object, object>)(row =>
                {
                    var value = property.GetValue(row);
                    if (string.IsNullOrWhiteSpace(column.Format))
                    {
                        return(value);
                    }
                    if (value == null)
                    {
                        return(null);
                    }

                    try
                    {
                        return(Convert.ToDateTime(value).ToString(column.Format));
                    }
                    catch (InvalidCastException)
                    {
                        throw new BlazuiException("仅日期列支持 Format 参数");
                    }
                }),
                Text       = column.Text,
                Width      = column.Width,
                IsCheckBox = column.IsCheckBox,
                Template   = column.ChildContent,
                Format     = column.Format
            };

            Table.Headers.Add(columnConfig);
        }
コード例 #2
0
 private void Clear(TableHeader header)
 {
     header.RawValue     = null;
     header.EditingValue = null;
     if (header.EditorRenderConfig == null)
     {
         return;
     }
     header.EditorRenderConfig.RawValue      = null;
     header.EditorRenderConfig.EditingValue  = null;
     header.EditorRenderConfig.RawLabel      = null;
     header.EditorRenderConfig.RawInfoHasSet = false;
 }
コード例 #3
0
        public void AddColumn(BTableColumn column)
        {
            if (Table.DataType == null)
            {
                throw new BlazuiException($"表格 {Table.GetType().Name} 没有设置 {nameof(BTable.DataSource)} 属性或该属性为空");
            }
            if (column.Property == null && !(column is BTableTemplateColumn))
            {
                throw new BlazuiException($"列 {column.Text} 没有设置 {nameof(BTableColumn.Property)} 属性");
            }

            PropertyInfo property = null;

            if (!string.IsNullOrWhiteSpace(column.Property))
            {
                property = Table.DataType.GetProperty(column.Property);
                if (property == null)
                {
                    throw new BlazuiException($"属性 {column.Property} 在 {Table.DataType.Name} 中不存在");
                }
            }
            var columnConfig = new TableHeader
            {
                EvalRaw = row =>
                {
                    object value = property.GetValue(row);
                    return(value);
                },
                Property   = property,
                Text       = column.Text,
                SortNo     = column.SortNo,
                Width      = column.Width,
                IsCheckBox = column.IsCheckBox,
                Template   = column.ChildContent,
                Format     = column.Format,
                IsTree     = column.IsTree,
                IsEditable = column.IsEditable
            };

            if (columnConfig.Property != null)
            {
                columnConfig.Eval = displayRender.CreateRenderFactory(columnConfig).CreateRender(columnConfig);
            }
            Table.Headers.Add(columnConfig);
        }
コード例 #4
0
        private void InitilizeHeaderEditor(PropertyInfo property, EditorGeneratorAttribute editorConfig, TableHeader tableHeader)
        {
            var controlInfo = map.GetControl(property);

            tableHeader.EditorRender       = (IControlRender)provider.GetRequiredService(controlInfo.RenderType);
            tableHeader.EditorRenderConfig = new RenderConfig()
            {
                InputControlType = controlInfo.ControlType,
                IsRequired       = editorConfig.IsRequired,
                RequiredMessage  = editorConfig.RequiredMessage ?? $"{tableHeader.Text}不能为空",
                Placeholder      = editorConfig.Placeholder,
                Property         = property,
                DataSourceLoader = controlInfo.DataSourceLoader,
                Page             = Page
            };
        }
コード例 #5
0
        private void CreateOperationColumn()
        {
            Headers.RemoveAll(x => x.IsOperation);
            var lastIndex       = Headers.Count;
            var operationHeader = new TableHeader()
            {
                Text        = "操作",
                IsOperation = true
            };
            BButton btnUpdate = null, btnDelete = null;

            operationHeader.Template = row =>
            {
                return((builder) =>
                {
                    builder.OpenComponent <BButtonGroup>(0);
                    builder.AddAttribute(1, nameof(BButtonGroup.ChildContent), (RenderFragment)(builder =>
                    {
                        builder.OpenComponent <BButton>(1);
                        builder.AddAttribute(2, nameof(BButton.Type), ButtonType.Primary);
                        builder.AddAttribute(3, nameof(BButton.Size), ButtonSize.Small);
                        builder.AddAttribute(4, nameof(BButton.ChildContent), (RenderFragment)(builder =>
                        {
                            if (row == editingRow)
                            {
                                builder.AddMarkupContent(5, "保存");
                            }
                            else
                            {
                                builder.AddMarkupContent(5, "更新");
                            }
                        }));
                        if (Page == null)
                        {
                            throw new BlazuiException(1, "表格启用可编辑功能后必须在外面套一层 CascadingValue,值为 BDialogBase(this),名称为 Page");
                        }
                        builder.AddAttribute(6, nameof(BButton.OnClick), EventCallback.Factory.Create <MouseEventArgs>(Page, async(e) =>
                        {
                            if (editingTaskCompletionSource == null)
                            {
                                editingTaskCompletionSource = new TaskCompletionSource <int>();
                                editingRow = row;
                                btnUpdate.MarkAsRequireRender();
                                Refresh();
                                return;
                            }
                            btnUpdate.IsLoading = true;
                            Refresh();
                            _ = SaveDataAsync(SaveAction.Update);
                            var result = await editingTaskCompletionSource.Task;
                            editingTaskCompletionSource = null;
                            btnUpdate.IsLoading = false;
                            if (result != 0)
                            {
                                Refresh();
                                editingTaskCompletionSource = new TaskCompletionSource <int>();
                                return;
                            }
                            foreach (var item in Headers)
                            {
                                item.EditingValue = (string)(item.EditorRenderConfig == null ? null : item.EditorRenderConfig.EditingValue = null);
                            }
                            btnUpdate.MarkAsRequireRender();
                            Refresh();
                        }));
                        builder.AddComponentReferenceCapture(7, btn =>
                        {
                            if (row == editingRow || editingRow == null)
                            {
                                btnUpdate = (BButton)btn;
                            }
                        });
                        builder.CloseComponent();
                        builder.OpenComponent <BButton>(8);
                        builder.AddAttribute(9, nameof(BButton.Type), ButtonType.Danger);
                        builder.AddAttribute(10, nameof(BButton.Size), ButtonSize.Small);
                        builder.AddAttribute(11, nameof(BButton.ChildContent), (RenderFragment)(builder =>
                        {
                            builder.AddMarkupContent(12, "删除");
                        }));
                        builder.AddAttribute(13, nameof(BButton.OnClick), EventCallback.Factory.Create <MouseEventArgs>(Page, async(e) =>
                        {
                            var confirmResult = await ConfirmAsync("确认要删除吗?");
                            if (confirmResult != MessageBoxResult.Ok)
                            {
                                return;
                            }
                            foreach (var item in Headers)
                            {
                                item.RawValue = row;
                            }
                            editingTaskCompletionSource = new TaskCompletionSource <int>();
                            btnDelete.IsLoading = true;
                            btnDelete.Refresh();
                            _ = SaveDataAsync(SaveAction.Delete);
                            var result = await editingTaskCompletionSource.Task;
                            btnDelete.IsLoading = false;
                            editingTaskCompletionSource = null;
                            if (result != 0)
                            {
                                btnDelete.Refresh();
                                editingTaskCompletionSource = new TaskCompletionSource <int>();
                                return;
                            }
                            editingTaskCompletionSource = null;
                            editingRow = null;
                            Toast("删除成功");
                        }));
                        builder.AddComponentReferenceCapture(14, btn => btnDelete = (BButton)btn);
                        builder.CloseComponent();
                    }));
                    builder.CloseComponent();
                });
            };
            Headers.Insert(lastIndex++, operationHeader);
        }
コード例 #6
0
        private void InitilizeHeaders()
        {
            if (AutoGenerateColumns && !headerInitilized)
            {
                headerInitilized = true;
                DataType.GetProperties().Where(p => !IgnoreProperties.Contains(p.Name)).Reverse().ToList().ForEach(property =>
                {
                    if (Headers.Any(x => x.Property?.Name == property.Name))
                    {
                        return;
                    }
                    var attrs = property.GetCustomAttributes(true);
                    if (attrs.OfType <TableIgnoreAttribute>().Any())
                    {
                        Headers.Add(new TableHeader()
                        {
                            Ignore   = true,
                            Property = property
                        });
                        return;
                    }
                    var columnConfig = attrs.OfType <TableColumnAttribute>().FirstOrDefault() ?? new TableColumnAttribute()
                    {
                        Text = property.Name
                    };

                    if (columnConfig.Ignore)
                    {
                        Headers.Add(new TableHeader()
                        {
                            Ignore   = true,
                            Property = property
                        });
                        return;
                    }
                    var editorConfig = attrs.OfType <EditorGeneratorAttribute>().FirstOrDefault() ?? new EditorGeneratorAttribute()
                    {
                        Control = typeof(BInput <string>)
                    };

                    var formConfig     = attrs.OfType <FormControlAttribute>().FirstOrDefault();
                    var propertyConfig = attrs.OfType <PropertyAttribute>().FirstOrDefault();

                    var tableHeader = new TableHeader()
                    {
                        EvalRaw = row =>
                        {
                            object value = property.GetValue(row);
                            return(value);
                        },
                        SortNo     = columnConfig.SortNo,
                        IsCheckBox = property.PropertyType == typeof(bool) || Nullable.GetUnderlyingType(property.PropertyType) == typeof(bool),
                        Property   = property,
                        Text       = columnConfig.Text,
                        Width      = columnConfig.Width,
                        IsEditable = columnConfig.IsEditable
                    };
                    tableHeader.Eval = displayRender.CreateRenderFactory(tableHeader).CreateRender(tableHeader);
                    if (IsEditable && columnConfig.IsEditable)
                    {
                        InitilizeHeaderEditor(property, editorConfig, tableHeader);
                    }
                    Headers.Insert(0, tableHeader);
                }
                                                                                                                   );
                if (IsEditable)
                {
                    CreateOperationColumn();
                }
                chkAll?.MarkAsRequireRender();
                ResetSelectAllStatus();
            }
            else if (!AutoGenerateColumns && !headerInitilized && Headers.Any())
            {
                headerInitilized = true;
                foreach (var header in Headers)
                {
                    if (!CanEdit(header))
                    {
                        continue;
                    }
                    InitilizeHeaderEditor(header.Property, header.Property.GetCustomAttribute <EditorGeneratorAttribute>() ?? new EditorGeneratorAttribute(), header);
                }
                CreateOperationColumn();
                Refresh();
            }
        }
コード例 #7
0
 private bool CanEdit(TableHeader header)
 {
     return(header.IsEditable && header.Eval != null);
 }