zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
commit | author | age
921de2 1 using Microsoft.AspNetCore.Components;
Z 2 using Microsoft.AspNetCore.Components.Rendering;
3 using System.Linq;
4
5 namespace Bootstrap.Admin.Pages.Components
6 {
7     /// <summary>
8     /// Table Toolbar 按钮呈现组件
9     /// </summary>
10     public class TableToolbarContent : ComponentBase
11     {
12         /// <summary>
13         /// 获得/设置 Table Toolbar 实例
14         /// </summary>
15         [CascadingParameter]
16         protected TableToolbarBase? Toolbar { get; set; }
17
18         /// <summary>
19         /// 渲染组件方法
20         /// </summary>
21         protected override void BuildRenderTree(RenderTreeBuilder builder)
22         {
23             // 渲染正常按钮
24             if (Toolbar != null && Toolbar.Buttons.Any())
25             {
26                 // 渲染 Toolbar 按钮
27                 //<div class="toolbar btn-group">
28                 //  <button type="button" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i><span>新增</span></button>
29                 //  <button type="button" class="btn btn-danger"><i class="fa fa-remove" aria-hidden="true"></i><span>删除</span></button>
30                 //  <button type="button" class="btn btn-primary"><i class="fa fa-pencil" aria-hidden="true"></i><span>编辑</span></button>
31                 //</div>
32                 var index = 0;
33                 builder.OpenElement(index++, "div");
34                 builder.AddAttribute(index++, "class", "toolbar btn-group");
35                 foreach (var button in Toolbar.Buttons)
36                 {
37                     builder.OpenElement(index++, "button");
38                     builder.AddAttribute(index++, "type", "button");
39                     builder.AddMultipleAttributes(index++, button.AdditionalAttributes);
40                     builder.AddAttribute(index++, "onclick", button.OnClick);
41
42                     // icon
43                     builder.OpenElement(index++, "i");
44
45                     // class="fa fa-plus" aria-hidden="true"
46                     builder.AddAttribute(index++, "class", button.Icon);
47                     builder.AddAttribute(index++, "aria-hidden", "true");
48                     builder.CloseElement();
49
50                     // span
51                     builder.OpenElement(index++, "span");
52                     builder.AddContent(index++, button.Title);
53                     builder.CloseElement();
54                     builder.CloseElement();
55                 }
56                 builder.CloseElement();
57
58                 // 渲染移动版按钮
59                 //<div class="gear btn-group">
60                 //  <button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" type="button"><i class="fa fa-gear"></i></button>
61                 //  <div class="dropdown-menu">
62                 //      <div class="dropdown-item" title="新增" @onclick="Add" asp-auth="add"><i class="fa fa-plus"></i></div>
63                 //      <div class="dropdown-item" title="删除" @onclick="Delete" asp-auth="del"><i class="fa fa-remove"></i></div>
64                 //      <div class="dropdown-item" title="编辑" @onclick="Edit" asp-auth="edit"><i class="fa fa-pencil"></i></div>
65                 //  </div>
66                 //</div>
67                 builder.OpenElement(index++, "div");
68                 builder.AddAttribute(index++, "class", "gear btn-group");
69
70                 builder.OpenElement(index++, "button");
71                 builder.AddAttribute(index++, "class", "btn btn-secondary dropdown-toggle");
72                 builder.AddAttribute(index++, "data-toggle", "dropdown");
73                 builder.AddAttribute(index++, "type", "button");
74
75                 // i
76                 builder.OpenElement(index++, "i");
77                 builder.AddAttribute(index++, "class", "fa fa-gear");
78                 builder.CloseElement();
79                 builder.CloseElement(); // end button
80
81                 // div dropdown-menu
82                 builder.OpenElement(index++, "div");
83                 builder.AddAttribute(index++, "class", "dropdown-menu");
84
85                 foreach (var button in Toolbar.Buttons)
86                 {
87                     builder.OpenElement(index++, "div");
88                     builder.AddAttribute(index++, "class", "dropdown-item");
89                     builder.AddAttribute(index++, "title", button.Title);
90                     builder.AddAttribute(index++, "onclick", EventCallback.Factory.Create(button, button.OnClick));
91
92                     // icon
93                     builder.OpenElement(index++, "i");
94
95                     // class="fa fa-plus" aria-hidden="true"
96                     builder.AddAttribute(index++, "class", button.Icon);
97                     builder.AddAttribute(index++, "aria-hidden", "true");
98                     builder.CloseElement(); // end i
99
100                     builder.CloseElement(); // end div
101                 }
102                 builder.CloseElement(); // end dropdown-menu
103                 builder.CloseElement(); // end div
104             }
105         }
106     }
107 }