Skip to content

Commit 7be8d98

Browse files
committed
Update ITableColumnExtensions.cs
1 parent f54df9e commit 7be8d98

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,13 @@ public static List<IFilterAction> ToSearches(this IEnumerable<ITableColumn> colu
175175

176176
internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem item) => async builder =>
177177
{
178-
// 1.增加了一个从IEnumerable转化为List<object>方法
179-
Func<IEnumerable, List<object>> GetEnumeratorDatas = (enumerableObj) =>
178+
// 获取单元格数据
179+
var val = col.GetItemValue(item);
180+
// 当前类型是否为IEnumerable<>类型
181+
var isEnumerableValue = (Nullable.GetUnderlyingType(col.PropertyType) ?? col.PropertyType).IsGenericType && val is IEnumerable;
182+
183+
// 将IEnumerable<>转换为List<object>方法
184+
List<object> GetEnumeratorDatas(IEnumerable enumerableObj)
180185
{
181186
var enumerator = enumerableObj.GetEnumerator();
182187
var list = new List<object>();
@@ -185,29 +190,35 @@ internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem i
185190
list.Add(enumerator.Current);
186191
}
187192
return list;
188-
};
193+
}
189194

190-
var val = col.GetItemValue(item);
191195
if (col.Lookup != null && val != null)
192196
{
197+
// 如果存在Lookup数据转化 Lookup 数据源
193198
string? content = val.ToString();
194-
// 转化 Lookup 数据源
195-
if ((Nullable.GetUnderlyingType(col.PropertyType) ?? col.PropertyType).IsGenericType && val is IEnumerable v)
199+
200+
if (isEnumerableValue && val is IEnumerable v)
196201
{
197-
// 2.如果是 IEnumerable 类型,则转化为 List<object> 类型 然后根据 List<object> 类型进行 Lookup 匹配
202+
// 如果是 IEnumerable<> 类型数据,则先转化为 List<object> 类型
198203
var enumeratorDatas = GetEnumeratorDatas(v);
204+
205+
// 根据 List<object> 数据获取匹配数据
199206
var lookupVals = col.Lookup.Where(l => enumeratorDatas.Any(v1 => l.Value.Equals(v1?.ToString(), col.LookupStringComparison)));
207+
200208
if (lookupVals.Any())
201209
{
210+
//如果有匹配到数据,则将匹配的文本用,连接显示
202211
content = string.Join(",", lookupVals.Select(l => l.Text));
203212
}
204213
else
205214
{
215+
//如果未匹配到数据,则直接将原数据用,连接显示
206216
content = string.Join(",", enumeratorDatas);
207217
}
208218
}
209219
else
210220
{
221+
// 非IEnumerable<>类型数据直接通过ToString进行匹配
211222
var lookupVal = col.Lookup.FirstOrDefault(l => l.Value.Equals(val.ToString(), col.LookupStringComparison));
212223
if (lookupVal != null)
213224
{
@@ -237,13 +248,9 @@ internal static RenderFragment RenderValue<TItem>(this ITableColumn col, TItem i
237248
{
238249
content = Utility.Format(val, CultureInfo.CurrentUICulture.DateTimeFormat);
239250
}
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)
251+
else if (isEnumerableValue && val is IEnumerable v)
246252
{
253+
// IEnumerable<>类型数据用,连接展示
247254
content = string.Join(",", GetEnumeratorDatas(v));
248255
}
249256
else

0 commit comments

Comments
 (0)