BlazorTable
BlazorTable copied to clipboard
[Feat] Allow dynamic DataSource type to AddColumn
hi , it is a nice project .
in the runtime AddColumn() mode, we must access the ITable< DataSource > instance, but in many context, we even dit not have the 'DataSource' type definition in code. it is just an ITable,
@code {
private ITable table;
...
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);
if (firstRender)
{
var col = new Column()
{
Title = "some desc",
//Field = f => f.Summary,
FieldName = "Summary", //pls allow string field selector rather than lambda field here :)
Sortable = true,
Filterable = true,
Width = "10%"
};
this.table.AddColumn(col); //for now, ITable dit not have AddColumn api :(
}
}
I know this is an older question but perhaps it can still help someone.
I also ran into this. But i used a simple ExpressionTree
to build this dynamically too.
I made a little method that you can try using.
private Expression<Func<T, object>> GetFieldExpression<T>(string fieldName)
{
var parameter = Expression.Parameter(typeof(T), "t");
var propertyExpression = Expression.PropertyOrField(parameter, fieldName);
return Expression.Lambda<Func<T, object>>(Expression.Convert(propertyExpression, typeof(object)), parameter);
}
In your example you can use it as follows:
var col = new Column<ColumnType>()
{
Title ="some desc",
Field = GetFieldExpression<ColumnType>("Summary")
};
this.table.AddColumn(col);
This works pretty well for me to load columns dynamically using reflection. Just make sure to change ColumnType
to the type you are displaying in your table (ie your TableItem
).