|
| 1 | +using Microsoft.AspNetCore.Components.Rendering; |
| 2 | + |
| 3 | +namespace TDesign; |
| 4 | +/// <summary> |
| 5 | +/// 用于模块内切换内容的分页 |
| 6 | +/// </summary> |
| 7 | +[CssClass("t-pagination")] |
| 8 | +public class TPagination:BlazorComponentBase |
| 9 | +{ |
| 10 | + #region 参数 |
| 11 | + /// <summary> |
| 12 | + /// 设置分页组件的大小。 |
| 13 | + /// </summary> |
| 14 | + [Parameter][BooleanCssClass("t-size-s","t-size-m")] public bool Small { get; set; } |
| 15 | + |
| 16 | + #region Current |
| 17 | + /// <summary> |
| 18 | + /// 设置当前页码。 |
| 19 | + /// </summary> |
| 20 | + [Parameter][EditorRequired]public int Current { get; set; } |
| 21 | + /// <summary> |
| 22 | + /// 设置一个当页码变更时的回调方法。 |
| 23 | + /// </summary> |
| 24 | + [Parameter] public EventCallback<int> CurrentChanged { get; set; } |
| 25 | + #endregion |
| 26 | + |
| 27 | + #region PageSize |
| 28 | + /// <summary> |
| 29 | + /// 设置每一页的数据量。默认是 10。 |
| 30 | + /// </summary> |
| 31 | + [Parameter] [EditorRequired]public int PageSize { get; set; } = 10; |
| 32 | + /// <summary> |
| 33 | + /// 设置一个当每页数据量变更时的回调方法。 |
| 34 | + /// </summary> |
| 35 | + [Parameter]public EventCallback<int> PageSizeChanged { get; set; } |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region Total |
| 39 | + /// <summary> |
| 40 | + /// 设置分页的总数据量。必须要大于 0。 |
| 41 | + /// </summary> |
| 42 | + [Parameter][EditorRequired]public long Total { get; set; } |
| 43 | + /// <summary> |
| 44 | + /// 设置一个当总数据量变化时的回调方法。 |
| 45 | + /// </summary> |
| 46 | + [Parameter]public EventCallback<long> TotalChanged { get; set; } |
| 47 | + /// <summary> |
| 48 | + /// 设置显示总数据量的任意内容。 |
| 49 | + /// </summary> |
| 50 | + [Parameter]public RenderFragment<long>? TotalContent { get; set; } |
| 51 | + /// <summary> |
| 52 | + /// 设置一个布尔值,表示是否显示总数据量的内容。 |
| 53 | + /// </summary> |
| 54 | + [Parameter]public bool ShowTotal { get; set; } |
| 55 | + #endregion |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// 设置分页页码条的显示数量。建议范围在 5-21 之间,默认是 7。 |
| 59 | + /// </summary> |
| 60 | + [Parameter] public int PageNumber { get; set; } = 7; |
| 61 | + #endregion |
| 62 | + |
| 63 | + |
| 64 | + #region Internal Property |
| 65 | + /// <summary> |
| 66 | + /// 获取总页数。 |
| 67 | + /// </summary> |
| 68 | + long TotalPages |
| 69 | + { |
| 70 | + get |
| 71 | + { |
| 72 | + var total = Total + PageSize - 1; |
| 73 | + if ( total <= 0 ) |
| 74 | + { |
| 75 | + total = 1; |
| 76 | + } |
| 77 | + |
| 78 | + var result = total / PageSize; |
| 79 | + if ( result < 0 ) |
| 80 | + { |
| 81 | + result = 1; |
| 82 | + } |
| 83 | + return result; |
| 84 | + } |
| 85 | + } |
| 86 | + #endregion |
| 87 | + |
| 88 | + #region Method |
| 89 | + /// <inheritdoc/> |
| 90 | + protected override void OnParametersSet() |
| 91 | + { |
| 92 | + base.OnParametersSet(); |
| 93 | + |
| 94 | + if ( Current <= 0 ) |
| 95 | + { |
| 96 | + throw new ArgumentException($"{nameof(Current)} 必须大于0"); |
| 97 | + } |
| 98 | + if ( PageSize <= 0 ) |
| 99 | + { |
| 100 | + throw new ArgumentException($"{nameof(PageSize)} 必须大于0"); |
| 101 | + } |
| 102 | + if ( Total <= 0 ) |
| 103 | + { |
| 104 | + throw new ArgumentException($"{nameof(Total)} 必须大于0"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + protected override void AddContent(RenderTreeBuilder builder, int sequence) |
| 109 | + { |
| 110 | + BuildTotal(builder, sequence); |
| 111 | + BuildPrevOrNextBtn(builder, sequence + 1, true); |
| 112 | + BuildPageNumbers(builder, sequence + 2); |
| 113 | + BuildPrevOrNextBtn(builder, sequence + 3, false); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// 构建总数据量的元素。 |
| 118 | + /// </summary> |
| 119 | + void BuildTotal(RenderTreeBuilder builder, int sequence) => builder.CreateElement(sequence, "div", TotalContent?.Invoke(Total), new { @class = "t-pagination__total" }, ShowTotal); |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// 构建上一页或下一页按钮。 |
| 123 | + /// </summary> |
| 124 | + /// <param name="builder"></param> |
| 125 | + /// <param name="sequence"></param> |
| 126 | + /// <param name="prevOrNext"><c>true</c> 表示上一页,否则是下一页。</param> |
| 127 | + void BuildPrevOrNextBtn(RenderTreeBuilder builder, int sequence,bool prevOrNext) |
| 128 | + { |
| 129 | + builder.CreateElement(sequence, "div", content => |
| 130 | + { |
| 131 | + content.CreateComponent<TIcon>(0, attributes: new { Name = prevOrNext ? IconName.ChevronLeft : IconName.ChevronRight }); |
| 132 | + }, new { @class = HtmlHelper.CreateCssBuilder().Append("t-pagination__btn").Append("t-pagination__btn-prev", prevOrNext).Append("t-pagination__btn-next") }); |
| 133 | + } |
| 134 | + |
| 135 | + void BuildPageNumbers(RenderTreeBuilder builder, int sequence) |
| 136 | + { |
| 137 | + builder.CreateElement(sequence, "ul", content => |
| 138 | + { |
| 139 | + var (start, end) = ComputePageNumber(); |
| 140 | + for ( long i = start; i <=end; i++ ) |
| 141 | + { |
| 142 | + content.CreateElement((int)i, "li", i.ToString(), new { @class = "t-pagination__number" }); |
| 143 | + } |
| 144 | + |
| 145 | + }, new { @class = "t-pagination__pager" }); |
| 146 | + |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// 计算分页页码的开始和结束的页码。 |
| 150 | + /// </summary> |
| 151 | + /// <returns>开始和结束的页码。</returns> |
| 152 | + (long start, long end) ComputePageNumber() |
| 153 | + { |
| 154 | + var start = 0L; |
| 155 | + var end = 0L; |
| 156 | + |
| 157 | + var middle = PageNumber / 2; |
| 158 | + if ( Current <= middle ) |
| 159 | + { |
| 160 | + start = 1; |
| 161 | + end = PageNumber; |
| 162 | + } |
| 163 | + else if ( Current > middle ) |
| 164 | + { |
| 165 | + start = Current - middle; |
| 166 | + end = Current + middle; |
| 167 | + } |
| 168 | + if ( end > TotalPages ) |
| 169 | + { |
| 170 | + end = TotalPages; |
| 171 | + if ( start + end != PageNumber - 2 ) |
| 172 | + { |
| 173 | + start = end - PageNumber + 2 - 1; |
| 174 | + } |
| 175 | + } |
| 176 | + if ( end <= PageNumber ) |
| 177 | + { |
| 178 | + start = 1; |
| 179 | + } |
| 180 | + |
| 181 | + return (start, end); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + #endregion |
| 186 | +} |
0 commit comments