zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
 
namespace Bootstrap.Admin.Pages.Components
{
    /// <summary>
    /// Table Header 表头组件
    /// </summary>
    public class TableHeaderContent : ComponentBase
    {
        /// <summary>
        /// 获得/设置 Table Header 实例
        /// </summary>
        [CascadingParameter]
        protected TableHeaderBase? Header { get; set; }
 
        /// <summary>
        /// 获得/设置 升序图标
        /// </summary>
        [Parameter]
        public string SortIconAsc { get; set; } = "fa fa-sort-asc";
 
        /// <summary>
        /// 获得/设置 降序图标
        /// </summary>
        [Parameter]
        public string SortIconDesc { get; set; } = "fa fa-sort-desc";
 
        /// <summary>
        /// 获得/设置 默认图标
        /// </summary>
        [Parameter]
        public string SortDefault { get; set; } = "fa fa-sort";
 
        private string sortName = "";
        private SortOrder sortOrder;
 
        /// <summary>
        /// 渲染组件方法
        /// </summary>
        protected override void BuildRenderTree(RenderTreeBuilder builder)
        {
            // 渲染正常按钮
            if (Header != null)
            {
                var index = 0;
                foreach (var header in Header.Headers)
                {
                    var fieldName = header.GetFieldName();
                    var displayName = header.GetDisplayName();
                    builder.OpenElement(index++, "th");
                    builder.AddMultipleAttributes(index++, header.AdditionalAttributes);
 
                    // 如果允许排序
                    if (header.Sort)
                    {
                        builder.AddAttribute(index++, "onclick", new Action(() =>
                        {
                            if (sortName != fieldName) sortOrder = SortOrder.Asc;
                            else sortOrder = sortOrder == SortOrder.Asc ? SortOrder.Desc : SortOrder.Asc;
                            sortName = fieldName;
 
                            // 通知 Table 组件刷新数据
                            Header.OnSort?.Invoke(sortName, sortOrder);
                            StateHasChanged();
                        }));
                        builder.AddAttribute(index++, "class", "sortable");
                    }
                    builder.OpenElement(index++, "span");
                    builder.AddContent(index++, displayName);
                    builder.CloseElement();
                    if (header.Sort)
                    {
                        builder.OpenElement(index++, "i");
                        var order = sortName == fieldName ? sortOrder : SortOrder.Unset;
                        var icon = order switch
                        {
                            SortOrder.Asc => SortIconAsc,
                            SortOrder.Desc => SortIconDesc,
                            _ => SortDefault
                        };
                        builder.AddAttribute(index++, "class", icon);
                        builder.CloseElement();
                    }
                    builder.CloseElement();
                }
            }
        }
    }
 
    /// <summary>
    /// 排序顺序枚举类型
    /// </summary>
    public enum SortOrder
    {
        /// <summary>
        /// 未设置
        /// </summary>
        Unset,
        /// <summary>
        /// 升序 0-9 A-Z
        /// </summary>
        Asc,
        /// <summary>
        /// 降序 9-0 Z-A
        /// </summary>
        Desc,
    }
}