2020-11-08 23:29:05 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
using System.Linq;
|
2020-06-02 19:15:15 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-10-25 18:31:31 +08:00
|
|
|
|
using AntDesign.Core.HashCodes;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
using AntDesign.TableModels;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class Table<TItem> : AntDomComponentBase, ITable
|
|
|
|
|
{
|
2020-06-05 16:06:23 +08:00
|
|
|
|
private static readonly TItem _fieldModel = (TItem)RuntimeHelpers.GetUninitializedObject(typeof(TItem));
|
2020-11-08 23:29:05 +08:00
|
|
|
|
private static readonly EventCallbackFactory _callbackFactory = new EventCallbackFactory();
|
2020-06-05 16:06:23 +08:00
|
|
|
|
|
2020-10-25 18:31:31 +08:00
|
|
|
|
private bool _shouldRender = true;
|
|
|
|
|
private int _parametersHashCode = 0;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderMode RenderMode { get; set; } = RenderMode.Always;
|
|
|
|
|
|
2020-05-30 01:46:41 +08:00
|
|
|
|
[Parameter]
|
2020-06-19 23:06:33 +08:00
|
|
|
|
public IEnumerable<TItem> DataSource
|
|
|
|
|
{
|
|
|
|
|
get => _dataSource;
|
|
|
|
|
set
|
|
|
|
|
{
|
2020-10-30 22:40:19 +08:00
|
|
|
|
_waitingReload = true;
|
2020-07-10 17:57:20 +08:00
|
|
|
|
_dataSourceCount = value?.Count() ?? 0;
|
2020-07-02 20:30:01 +08:00
|
|
|
|
_dataSource = value ?? Enumerable.Empty<TItem>();
|
2020-06-19 23:06:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment<TItem> ChildContent { get; set; }
|
|
|
|
|
|
2020-11-09 22:17:27 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment<TItem> RowTemplate { get; set; }
|
|
|
|
|
|
2020-11-08 23:29:05 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment<RowData<TItem>> ExpandTemplate { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<RowData<TItem>, bool> RowExpandable { get; set; } = _ => true;
|
|
|
|
|
|
2020-11-09 22:17:27 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<TItem, IEnumerable<TItem>> TreeChildren { get; set; } = _ => Enumerable.Empty<TItem>();
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<QueryModel<TItem>> OnChange { get; set; }
|
|
|
|
|
|
2020-06-02 19:15:15 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Loading { get; set; }
|
|
|
|
|
|
2020-07-02 20:30:01 +08:00
|
|
|
|
[Parameter]
|
2020-09-16 13:58:16 +08:00
|
|
|
|
public string Title { get; set; }
|
2020-07-02 20:30:01 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-09-16 13:58:16 +08:00
|
|
|
|
public RenderFragment TitleTemplate { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Footer { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment FooterTemplate { get; set; }
|
2020-07-02 20:30:01 +08:00
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-06-29 15:52:28 +08:00
|
|
|
|
public TableSize Size { get; set; }
|
|
|
|
|
|
2020-09-07 22:46:50 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public TableLocale Locale { get; set; } = LocaleProvider.CurrentLocale.Table;
|
|
|
|
|
|
2020-07-02 20:30:01 +08:00
|
|
|
|
[Parameter]
|
2020-06-29 15:52:28 +08:00
|
|
|
|
public bool Bordered { get; set; } = false;
|
|
|
|
|
|
2020-06-19 23:06:33 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string ScrollX { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string ScrollY { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public int ScrollBarWidth { get; set; } = 17;
|
|
|
|
|
|
2020-11-09 22:17:27 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public int IndentSize { get; set; } = 15;
|
|
|
|
|
|
2020-06-19 23:06:33 +08:00
|
|
|
|
public ColumnContext ColumnContext { get; set; } = new ColumnContext();
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
private IEnumerable<TItem> _showItems;
|
|
|
|
|
|
2020-06-19 23:06:33 +08:00
|
|
|
|
private IEnumerable<TItem> _dataSource;
|
2020-07-02 20:30:01 +08:00
|
|
|
|
|
2020-08-01 23:45:48 +08:00
|
|
|
|
private bool _waitingReload = false;
|
2020-11-08 16:55:34 +08:00
|
|
|
|
private bool _waitingReloadAndInvokeChange = false;
|
2020-11-09 22:17:27 +08:00
|
|
|
|
private bool _treeMode = false;
|
2020-08-01 23:45:48 +08:00
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
private bool ServerSide => _total > _dataSourceCount;
|
|
|
|
|
|
2020-11-09 22:17:27 +08:00
|
|
|
|
bool ITable.TreeMode => _treeMode;
|
|
|
|
|
|
|
|
|
|
int ITable.IndentSize => IndentSize;
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
public void ReloadData()
|
|
|
|
|
{
|
2020-07-10 17:57:20 +08:00
|
|
|
|
PageIndex = 1;
|
|
|
|
|
|
|
|
|
|
FlushCache();
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
this.Reload();
|
|
|
|
|
}
|
2020-06-19 23:06:33 +08:00
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
void ITable.Refresh()
|
2020-06-19 23:06:33 +08:00
|
|
|
|
{
|
2020-11-09 22:17:27 +08:00
|
|
|
|
_shouldRender = true;
|
2020-07-08 16:12:49 +08:00
|
|
|
|
StateHasChanged();
|
2020-06-19 23:06:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
void ITable.ReloadAndInvokeChange()
|
2020-06-19 23:06:33 +08:00
|
|
|
|
{
|
2020-07-08 16:12:49 +08:00
|
|
|
|
ReloadAndInvokeChange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReloadAndInvokeChange()
|
|
|
|
|
{
|
|
|
|
|
var queryModel = this.Reload();
|
|
|
|
|
if (OnChange.HasDelegate)
|
2020-06-19 23:06:33 +08:00
|
|
|
|
{
|
2020-07-08 16:12:49 +08:00
|
|
|
|
OnChange.InvokeAsync(queryModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private QueryModel<TItem> Reload()
|
|
|
|
|
{
|
|
|
|
|
var queryModel = new QueryModel<TItem>(PageIndex, PageSize);
|
|
|
|
|
|
2020-09-01 17:23:31 +08:00
|
|
|
|
foreach (var col in ColumnContext.Columns)
|
|
|
|
|
{
|
|
|
|
|
if (col is IFieldColumn fieldColumn && fieldColumn.Sortable)
|
|
|
|
|
{
|
|
|
|
|
queryModel.AddSortModel(fieldColumn.SortModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
if (ServerSide)
|
2020-07-08 16:12:49 +08:00
|
|
|
|
{
|
|
|
|
|
_showItems = _dataSource;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-05 20:27:52 +08:00
|
|
|
|
if (_dataSource != null)
|
2020-06-19 23:06:33 +08:00
|
|
|
|
{
|
2020-09-05 20:27:52 +08:00
|
|
|
|
var query = _dataSource.AsQueryable();
|
|
|
|
|
foreach (var sort in queryModel.SortModel)
|
|
|
|
|
{
|
2020-09-08 17:13:26 +08:00
|
|
|
|
query = sort.Sort(query);
|
2020-09-05 20:27:52 +08:00
|
|
|
|
}
|
2020-05-30 01:46:41 +08:00
|
|
|
|
|
2020-09-05 20:27:52 +08:00
|
|
|
|
query = query.Skip((PageIndex - 1) * PageSize).Take(PageSize);
|
|
|
|
|
queryModel.SetQueryableLambda(query);
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
2020-09-05 20:27:52 +08:00
|
|
|
|
_showItems = query;
|
|
|
|
|
}
|
2020-06-19 23:06:33 +08:00
|
|
|
|
}
|
2020-06-05 16:06:23 +08:00
|
|
|
|
|
2020-05-30 01:46:41 +08:00
|
|
|
|
StateHasChanged();
|
2020-07-08 16:12:49 +08:00
|
|
|
|
|
|
|
|
|
return queryModel;
|
2020-05-30 01:46:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 23:06:33 +08:00
|
|
|
|
private void SetClass()
|
|
|
|
|
{
|
|
|
|
|
string prefixCls = "ant-table";
|
|
|
|
|
ClassMapper.Add(prefixCls)
|
|
|
|
|
.If($"{prefixCls}-fixed-header", () => ScrollY != null)
|
2020-06-29 15:52:28 +08:00
|
|
|
|
.If($"{prefixCls}-bordered", () => Bordered)
|
|
|
|
|
.If($"{prefixCls}-small", () => Size == TableSize.Small)
|
|
|
|
|
.If($"{prefixCls}-middle", () => Size == TableSize.Middle)
|
2020-06-19 23:06:33 +08:00
|
|
|
|
//.Add( "ant-table ant-table-ping-left ant-table-ping-right ")
|
|
|
|
|
.If($"{prefixCls}-fixed-column {prefixCls}-scroll-horizontal", () => ColumnContext.Columns.Any(x => x.Fixed.IsIn("left", "right")))
|
|
|
|
|
.If($"{prefixCls}-has-fix-left", () => ColumnContext.Columns.Any(x => x.Fixed == "left"))
|
|
|
|
|
.If($"{prefixCls}-has-fix-right {prefixCls}-ping-right ", () => ColumnContext.Columns.Any(x => x.Fixed == "right"))
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-11-09 22:17:27 +08:00
|
|
|
|
if (RowTemplate != null)
|
|
|
|
|
{
|
|
|
|
|
ChildContent = RowTemplate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TreeChildren != null && DataSource.Any(x => TreeChildren(x).Any()))
|
|
|
|
|
{
|
|
|
|
|
_treeMode = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 23:06:33 +08:00
|
|
|
|
SetClass();
|
2020-07-10 17:57:20 +08:00
|
|
|
|
|
|
|
|
|
InitializePagination();
|
|
|
|
|
|
|
|
|
|
FlushCache();
|
2020-06-19 23:06:33 +08:00
|
|
|
|
|
2020-07-08 16:12:49 +08:00
|
|
|
|
ReloadAndInvokeChange();
|
2020-06-05 16:06:23 +08:00
|
|
|
|
}
|
2020-07-10 17:57:20 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnAfterRender(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
base.OnAfterRender(firstRender);
|
|
|
|
|
|
2020-11-08 16:55:34 +08:00
|
|
|
|
if (_waitingReloadAndInvokeChange)
|
|
|
|
|
{
|
|
|
|
|
_waitingReloadAndInvokeChange = false;
|
|
|
|
|
_waitingReload = false;
|
|
|
|
|
|
|
|
|
|
ReloadAndInvokeChange();
|
|
|
|
|
}
|
2020-11-09 22:17:27 +08:00
|
|
|
|
else if (_waitingReload)
|
2020-08-01 23:45:48 +08:00
|
|
|
|
{
|
|
|
|
|
_waitingReload = false;
|
|
|
|
|
Reload();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 17:57:20 +08:00
|
|
|
|
if (!firstRender)
|
|
|
|
|
{
|
|
|
|
|
this.FinishLoadPage();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-25 18:31:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
|
|
|
|
|
if (this.RenderMode == RenderMode.ParametersHashCodeChanged)
|
|
|
|
|
{
|
|
|
|
|
var hashCode = this.GetParametersHashCode();
|
|
|
|
|
this._shouldRender = this._parametersHashCode != hashCode;
|
|
|
|
|
this._parametersHashCode = hashCode;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this._shouldRender = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 23:29:05 +08:00
|
|
|
|
protected override bool ShouldRender() => this._shouldRender;
|
|
|
|
|
|
|
|
|
|
private void ToggleExpandRow(RowData<TItem> rowData)
|
2020-10-25 18:31:31 +08:00
|
|
|
|
{
|
2020-11-08 23:29:05 +08:00
|
|
|
|
rowData.Expanded = !rowData.Expanded;
|
2020-10-25 18:31:31 +08:00
|
|
|
|
}
|
2020-05-30 01:46:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|