Skip to content

Commit f54df9e

Browse files
committed
Update ITableColumnExtensions.cs
1 parent a759930 commit f54df9e

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6+
using System.Collections;
67
using System.Globalization;
78

89
namespace BootstrapBlazor.Components;
@@ -174,15 +175,46 @@ public static List<IFilterAction> ToSearches(this IEnumerable<ITableColumn> colu
174175

175176
internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item) => async builder =>
176177
{
178+
// 1.增加了一个从IEnumerable转化为List<object>方法
179+
Func<IEnumerable, List<object>> GetEnumeratorDatas = (enumerableObj) =>
180+
{
181+
var enumerator = enumerableObj.GetEnumerator();
182+
var list = new List<object>();
183+
while (enumerator.MoveNext())
184+
{
185+
list.Add(enumerator.Current);
186+
}
187+
return list;
188+
};
189+
177190
var val = col.GetItemValue(item);
178191
if (col.Lookup != null && val != null)
179192
{
193+
string? content = val.ToString();
180194
// 转化 Lookup 数据源
181-
var lookupVal = col.Lookup.FirstOrDefault(l => l.Value.Equals(val.ToString(), col.LookupStringComparison));
182-
if (lookupVal != null)
195+
if ((Nullable.GetUnderlyingType(col.PropertyType) ?? col.PropertyType).IsGenericType && val is IEnumerable v)
183196
{
184-
builder.AddContent(10, col.RenderTooltip(lookupVal.Text, item));
197+
// 2.如果是 IEnumerable 类型,则转化为 List<object> 类型 然后根据 List<object> 类型进行 Lookup 匹配
198+
var enumeratorDatas = GetEnumeratorDatas(v);
199+
var lookupVals = col.Lookup.Where(l => enumeratorDatas.Any(v1 => l.Value.Equals(v1?.ToString(), col.LookupStringComparison)));
200+
if (lookupVals.Any())
201+
{
202+
content = string.Join(",", lookupVals.Select(l => l.Text));
203+
}
204+
else
205+
{
206+
content = string.Join(",", enumeratorDatas);
207+
}
208+
}
209+
else
210+
{
211+
var lookupVal = col.Lookup.FirstOrDefault(l => l.Value.Equals(val.ToString(), col.LookupStringComparison));
212+
if (lookupVal != null)
213+
{
214+
content = lookupVal.Text;
215+
}
185216
}
217+
builder.AddContent(10, col.RenderTooltip(content, item));
186218
}
187219
else if (val is bool v1)
188220
{
@@ -205,9 +237,14 @@ internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem i
205237
{
206238
content = Utility.Format(val, CultureInfo.CurrentUICulture.DateTimeFormat);
207239
}
208-
else if (val is IEnumerable<object> v)
240+
//else if(val is IEnumerable<object> v)
241+
//{
242+
// content = string.Join(",", v);
243+
//}
244+
//3.这里的判断条件调整了一下,为了支持List<int>,List<string> 等等
245+
else if ((Nullable.GetUnderlyingType(col.PropertyType) ?? col.PropertyType).IsGenericType && val is IEnumerable v)
209246
{
210-
content = string.Join(",", v);
247+
content = string.Join(",", GetEnumeratorDatas(v));
211248
}
212249
else
213250
{

0 commit comments

Comments
 (0)