可见列
TreeList允许您以编程方式隐藏它的一些列。
在本文中:
基础知识
要隐藏TreeList列,请设置其可见
参数假
.要根据特定条件隐藏列,您可以传递,例如,三元运算符或返回的方法保龄球
-应用程序可以根据其逻辑(如屏幕大小)提供表达式。
从TreeList中隐藏一列。基本的例子。
@*通过设置Visible参数为false隐藏Id TreeList列*@ @code {public List Data {get;设置;}公共类雇员{公共列表<雇员> DirectReports {get;设置;}公共int Id{获取;设置;}公共字符串名称{获取;设置;}公共字符串EmailAddress{获取; set; } public DateTime HireDate { get; set; } } public int LastId { get; set; } = 1; protected override async Task OnInitializedAsync() { Data = await GetTreeListData(); } async Task> GetTreeListData() { List data = new List(); for (int i = 1; i < 15; i++) { Employee root = new Employee { Id = LastId, Name = $"root: {i}", EmailAddress = $"{i}@example.com", HireDate = DateTime.Now.AddYears(-i), DirectReports = new List() }; data.Add(root); LastId++; for (int j = 1; j < 4; j++) { int currId = LastId; Employee firstLevelChild = new Employee { Id = currId, Name = $"first level child {j} of {i}", EmailAddress = $"{currId}@example.com", HireDate = DateTime.Now.AddDays(-currId), DirectReports = new List(), }; root.DirectReports.Add(firstLevelChild); LastId++; for (int k = 1; k < 3; k++) { int nestedId = LastId; // populate the parent's collection firstLevelChild.DirectReports.Add(new Employee { Id = LastId, Name = $"second level child {k} of {j} and {i}", EmailAddress = $"{nestedId}@example.com", HireDate = DateTime.Now.AddMinutes(-nestedId) }); ; LastId++; } } } return await Task.FromResult(data); } }
上面代码片段的结果- ID列没有被呈现
笔记
不可见的列(可见= " false "
)会有以下行为:
例子
在本节中,你会看到以下例子:
在按钮单击上切换列的可见性
的值可见
参数,这将切换列。
切换一个列的可见性将保持其在树列表中的原始顺序。*@ 切换Id列的可见性 @code {public bool isVisible {get;设置;} = true;public List Data {get;设置;}公共类雇员{公共列表<雇员> DirectReports {get;设置; } public int Id { get; set; } public string Name { get; set; } public string EmailAddress { get; set; } public DateTime HireDate { get; set; } } public int LastId { get; set; } = 1; protected override async Task OnInitializedAsync() { Data = await GetTreeListData(); } async Task> GetTreeListData() { List data = new List(); for (int i = 1; i < 15; i++) { Employee root = new Employee { Id = LastId, Name = $"root: {i}", EmailAddress = $"{i}@example.com", HireDate = DateTime.Now.AddYears(-i), DirectReports = new List() }; data.Add(root); LastId++; for (int j = 1; j < 4; j++) { int currId = LastId; Employee firstLevelChild = new Employee { Id = currId, Name = $"first level child {j} of {i}", EmailAddress = $"{currId}@example.com", HireDate = DateTime.Now.AddDays(-currId), DirectReports = new List(), }; root.DirectReports.Add(firstLevelChild); LastId++; for (int k = 1; k < 3; k++) { int nestedId = LastId; // populate the parent's collection firstLevelChild.DirectReports.Add(new Employee { Id = LastId, Name = $"second level child {k} of {j} and {i}", EmailAddress = $"{nestedId}@example.com", HireDate = DateTime.Now.AddMinutes(-nestedId) }); ; LastId++; } } } return await Task.FromResult(data); } }
上面代码片段的结果
带有模板的隐藏树列表列
当使用特定于单元格的模板时,它们根本不会被呈现。但是,如果您正在使用RowTemplate,请确保在那里也处理列的可见性。
@* Name列的模板将不会被渲染*@ @{Employee Employee = context as Employee;@employee.HireDate.ToShortDateString ();} @code {public List Data {get;设置;}公共类雇员{公共列表<雇员> DirectReports {get;设置;}公共int Id{获取;设置;}公共字符串名称{获取; set; } public string EmailAddress { get; set; } public DateTime HireDate { get; set; } } public int LastId { get; set; } = 1; protected override async Task OnInitializedAsync() { Data = await GetTreeListData(); } async Task> GetTreeListData() { List data = new List(); for (int i = 1; i < 15; i++) { Employee root = new Employee { Id = LastId, Name = $"root: {i}", EmailAddress = $"{i}@example.com", HireDate = DateTime.Now.AddYears(-i), DirectReports = new List() }; data.Add(root); LastId++; for (int j = 1; j < 4; j++) { int currId = LastId; Employee firstLevelChild = new Employee { Id = currId, Name = $"first level child {j} of {i}", EmailAddress = $"{currId}@example.com", HireDate = DateTime.Now.AddDays(-currId), DirectReports = new List(), }; root.DirectReports.Add(firstLevelChild); LastId++; for (int k = 1; k < 3; k++) { int nestedId = LastId; // populate the parent's collection firstLevelChild.DirectReports.Add(new Employee { Id = LastId, Name = $"second level child {k} of {j} and {i}", EmailAddress = $"{nestedId}@example.com", HireDate = DateTime.Now.AddMinutes(-nestedId) }); ; LastId++; } } } return await Task.FromResult(data); } }
上面代码片段的结果——HireDate列没有被呈现
隐藏一个基于条件的树列表列
这个例子展示了根据数据中的简单条件隐藏列。您可以将其更改为使用其他视图模型数据—例如屏幕尺寸、存储的用户首选项或任何其他逻辑。
@* Id列被隐藏,因为TreeList的数据包含1 *@ @code {public List Data {get;设置;}公共类雇员{公共列表<雇员> DirectReports {get;设置;}公共int Id{获取;设置;}公共字符串名称{获取; set; } public string EmailAddress { get; set; } public DateTime HireDate { get; set; } } public int LastId { get; set; } = 1; protected override async Task OnInitializedAsync() { Data = await GetTreeListData(); } async Task> GetTreeListData() { List data = new List(); for (int i = 1; i < 15; i++) { Employee root = new Employee { Id = LastId, Name = $"root: {i}", EmailAddress = $"{i}@example.com", HireDate = DateTime.Now.AddYears(-i), DirectReports = new List() }; data.Add(root); LastId++; for (int j = 1; j < 4; j++) { int currId = LastId; Employee firstLevelChild = new Employee { Id = currId, Name = $"first level child {j} of {i}", EmailAddress = $"{currId}@example.com", HireDate = DateTime.Now.AddDays(-currId), DirectReports = new List(), }; root.DirectReports.Add(firstLevelChild); LastId++; for (int k = 1; k < 3; k++) { int nestedId = LastId; // populate the parent's collection firstLevelChild.DirectReports.Add(new Employee { Id = LastId, Name = $"second level child {k} of {j} and {i}", EmailAddress = $"{nestedId}@example.com", HireDate = DateTime.Now.AddMinutes(-nestedId) }); ; LastId++; } } } return await Task.FromResult(data); } }
上面代码片段的结果