namespace MiaoYu.Shared.Admin.Models.PagingViews;
///
/// 列头信息
///
public class TableColumnView
{
public TableColumnView()
{
}
public TableColumnView(string fieldName, string title)
{
DataIndex = fieldName;
FieldName = fieldName;
Title = title;
Show = !fieldName.StartsWith("_");
}
public TableColumnView(string fieldName, string title, int width)
{
DataIndex = fieldName;
FieldName = fieldName;
Title = title;
Show = !fieldName.StartsWith("_");
Width = width.ToString();
}
public TableColumnView(string fieldName, string title, bool show, int width)
{
DataIndex = fieldName;
FieldName = fieldName;
Title = title;
Show = show;
Width = width.ToString();
}
///
/// 字段名称
///
public string FieldName { get; set; }
///
/// 字段名称
///
[JsonProperty("dataIndex")]
public string DataIndex { get; set; }
///
/// 标题名称
///
public string Title { get; set; }
///
/// 是否显示
///
public bool Show { get; set; } = true;
///
/// 列宽度
///
public string Width { get; set; } = string.Empty;
///
/// 是否参加排序
///
public bool Sorter { get; set; } = true;
///
/// 排序
///
public int OrderById { get; set; }
///
/// 映射字段
///
/// 列显示名称
/// 是否显示列
/// 列宽
/// 是否参加排序
public void SetColumn(string title = null, bool? show = null, string width = null, bool sort = true)
{
if (!string.IsNullOrWhiteSpace(title)) Title = title;
if (show != null) Show = show.Value;
if (!string.IsNullOrWhiteSpace(width)) Width = width;
Sorter = sort;
}
///
/// 设置列信息 用于外键表列头得显示名称
///
///
///
///
///
///
///
public TableColumnView SetColumn(Expression> field, bool? show = null, string? width = null, bool sort = true)
{
// 设置列显示名称
using var scope = App.CreateScope();
var databaseTableService = scope.ServiceProvider.GetService();
if (databaseTableService == null) return this;
var genDbTableDtos = databaseTableService!.GetAllTablesByCache();
if (genDbTableDtos == null) return this;
if (genDbTableDtos.Count == 0) return this;
//自动获取名称对应的显示名
var type = typeof(TEntity);
var name = Tools.GetNameByExpression(field);
var table = genDbTableDtos?
.Where(w => w.EntityName.ToLower() == type.Name.ToLower())
.FirstOrDefault();
if (table == null) return this;
var tableInfo = table.TableInfos.FirstOrDefault(w => w.CsField.ToLower() == name.ToLower());
if (tableInfo == null) return this;
this.SetColumn(tableInfo?.DisplayName, show, width, sort);
return this;
}
}