Skip to content

Commit

Permalink
FritzAndFriends#163 not yet complete WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
grleachman committed Apr 18, 2020
1 parent 4ed0333 commit 5d1294b
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 339 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@
</Fixture>

@code {
void FirstTest()
{
var cut = GetComponentUnderTest();
var tableHeaders = cut.FindAll("th").ToList();
tableHeaders.Count.ShouldBe(4, "Did not render 4 TH elements");

try {
tableHeaders[0].TextContent.ShouldBe("Id");
} catch (Exception ex) {
Console.WriteLine("HTML rendered: " + cut.Markup);
throw;
}
void FirstTest()
{
var cut = GetComponentUnderTest();
System.Diagnostics.Debug.WriteLine(cut.Markup);
var tableHeaders = cut.FindAll("th").ToList();
tableHeaders.Count.ShouldBe(4, "Did not render 4 TH elements");

tableHeaders[1].TextContent.ShouldBe("Name");
tableHeaders[2].TextContent.ShouldBe("Price");
tableHeaders[3].TextContent.ShouldBe("LastUpdate");
try
{
tableHeaders[0].TextContent.ShouldBe("Id");
}

IQueryable<Widget> GetWidgets(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
catch (Exception ex)
{
totalRowCount = Widget.SimpleWidgetList.Length;
return Widget.SimpleWidgetList.AsQueryable();
Console.WriteLine("HTML rendered: " + cut.Markup);
throw;
}

tableHeaders[1].TextContent.ShouldBe("Name");
tableHeaders[2].TextContent.ShouldBe("Price");
tableHeaders[3].TextContent.ShouldBe("LastUpdate");
}

IQueryable<Widget> GetWidgets(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
{
totalRowCount = Widget.SimpleWidgetList.Length;
return Widget.SimpleWidgetList.AsQueryable();
}
}
30 changes: 15 additions & 15 deletions src/BlazorWebFormsComponents.Test/GridView/EmptyDataText.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

@code {

void FirstTest()
{
var cut = GetComponentUnderTest();
var tableHeaders = cut.FindAll("th").ToList();
tableHeaders.Count.ShouldBe(4, "Did not render 4 TH elements");
cut.FindAll("tr").Count(e => e.InnerHtml.Contains("td")).ShouldBe(1, "Did not render 1 TR elements");
cut.FindAll("td").Count().ShouldBe(1, "Did not render 0 TD elements");
cut.Find("td").TextContent.Replace("\t", string.Empty).Replace("\n", string.Empty).ShouldBe("TestText");
}
void FirstTest()
{
var cut = GetComponentUnderTest();
var tableHeaders = cut.FindAll("th").ToList();
tableHeaders.Count.ShouldBe(4, "Did not render 4 TH elements");
cut.FindAll("tr").Count(e => e.InnerHtml.Contains("td")).ShouldBe(1, "Did not render 1 TR elements");
cut.FindAll("td").Count().ShouldBe(1, "Did not render 0 TD elements");
cut.Find("td").TextContent.Replace("\t", string.Empty).Replace("\n", string.Empty).ShouldBe("TestText");
}

IQueryable<Widget> GetWidgets(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
{
totalRowCount = 0;
var widgetList = new List<Widget>();
return widgetList.AsQueryable();
}
IQueryable<Widget> GetWidgets(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
{
totalRowCount = 0;
var widgetList = new List<Widget>();
return widgetList.AsQueryable();
}
}
17 changes: 10 additions & 7 deletions src/BlazorWebFormsComponents.Test/GridView/TemplateFields.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
AutogenerateColumns="false"
ItemType="SharedSampleObjects.Models.Widget">
<Columns>
<TemplateField ItemType="Widget" HeaderText="Name">
<TemplateField HeaderText="Name">
<ItemTemplate Context="Item">
<label Text="@Item.Name"></label>
<label></label>
</ItemTemplate>
</TemplateField>
<TemplateField ItemType="Widget" HeaderText="Price">
<TemplateField HeaderText="Price">
<ItemTemplate Context="Item">
<label ID="lblPrice" Text="@Item.Price.ToString()"></label>
<button></button>
</ItemTemplate>
</TemplateField>
</Columns>
Expand All @@ -26,16 +26,19 @@
void FirstTest()
{
var cut = GetComponentUnderTest();
System.Diagnostics.Debug.Write(cut.Markup);

System.Diagnostics.Debug.WriteLine(cut.Markup);
var tableHeaders = cut.FindAll("th");
tableHeaders[0].TextContent.ShouldBe("Name");
tableHeaders[1].TextContent.ShouldBe("Price");
tableHeaders.Count.ShouldBe(2, "Did not render 2 TH elements");
cut.FindAll("tr").Count(e => e.InnerHtml.Contains("td")).ShouldBe(3, "Did not render 3 TR elements");
cut.FindAll("td label").Count().ShouldBe(6, "Did not render 6 labels");
cut.FindAll("td").Count(e => e.InnerHtml.Contains("label")).ShouldBe(3, "Did not render 3 labels");
cut.FindAll("td").Count(e => e.InnerHtml.Contains("button")).ShouldBe(3, "Did not render 3 buttons");
}

IQueryable<Widget> GetWidgets(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
IQueryable<Widget>
GetWidgets(int maxRows, int startRowIndex, string sortByExpression, out int totalRowCount)
{
totalRowCount = Widget.SimpleWidgetList.Length;
return Widget.SimpleWidgetList.AsQueryable();
Expand Down
16 changes: 1 addition & 15 deletions src/BlazorWebFormsComponents/BaseColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,10 @@

namespace BlazorWebFormsComponents
{
public abstract class BaseColumn<ItemType> : BaseWebFormsComponent, IColumn<ItemType>
public abstract class BaseColumn : BaseWebFormsComponent
{
///<inheritdoc/>
[CascadingParameter(Name = "ColumnCollection")]
public IColumnCollection<ItemType> ParentColumnsCollection { get; set; }

///<inheritdoc/>
[Parameter] public string HeaderText { get; set; }

public void Dispose()
{
ParentColumnsCollection.RemoveColumn(this);
}

///<inheritdoc/>
protected override void OnInitialized()
{
ParentColumnsCollection.AddColumn(this);
}
}
}
28 changes: 0 additions & 28 deletions src/BlazorWebFormsComponents/BaseRow.cs

This file was deleted.

58 changes: 21 additions & 37 deletions src/BlazorWebFormsComponents/GridView.razor
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
@typeparam ItemType
@inherits BaseModelBindingComponent<ItemType>
@using Interfaces
@if (ColumnList.Any())
{
<table class="@CssClass">
<thead>
<tr>
@foreach (IColumn<ItemType> column in ColumnList)
@inherits ListView<ItemType>
@typeparam ItemType
<ListView runat="server"
ItemType="ItemType"
Context="Item"
Items="@Items">
<ItemTemplate><tr></tr></ItemTemplate>
<LayoutTemplate Context="itemPlaceHolder">
<table class="table" id="gridview">
<thead>
@foreach (var h in HeaderRow())
{
<th>@column.HeaderText</th>
}
</tr>
</thead>
<tbody>
@if (Items != null && Items.Any())
{
@foreach (ItemType item in Items)
{
<CascadingValue Value="this" Name="RowCollection">
<GridViewRow DataItem="item" Columns="ColumnList" />
</CascadingValue>
}
}
else
{
<tr>
<td colspan=@ColumnList.Count>
@EmptyDataText
</td>
</tr>
<th>@h</th>
}
</tbody>
</table>
}

<CascadingValue Value="this" Name="ColumnCollection">
@Columns
</CascadingValue>
</thead>
<tbody>
@itemPlaceHolder
</tbody>
</table>
</LayoutTemplate>
</ListView>
@code{
}
67 changes: 16 additions & 51 deletions src/BlazorWebFormsComponents/GridView.razor.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
using BlazorWebFormsComponents.Interfaces;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Linq;

namespace BlazorWebFormsComponents
{
/// <summary>
/// Blazor version of WebForms GridView control
/// </summary>
/// <typeparam name="ItemType"></typeparam>
public partial class GridView<ItemType> : BaseModelBindingComponent<ItemType>, IRowCollection<ItemType>, IColumnCollection<ItemType>
{
public partial class GridView<ItemType> : ListView<ItemType>
{

/// <summary>
/// Specify if the GridView component will autogenerate its columns
/// </summary>
[Parameter] public bool AutogenerateColumns { get; set; } = true;

[Parameter] public RenderFragment Columns { get; set; }

public List<BoundField> BoundFields { get; set; } = new List<BoundField>();

public IEnumerable<string> HeaderRow()
{
foreach (var x in BoundFields)
{
yield return x.HeaderText;
}
}

/// <summary>
/// Text to show when there are no items to show
/// </summary>
Expand All @@ -32,26 +44,8 @@ public partial class GridView<ItemType> : BaseModelBindingComponent<ItemType>, I
[Parameter] public string CssClass { get; set; }

///<inheritdoc/>
public List<IColumn<ItemType>> ColumnList { get; set; } = new List<IColumn<ItemType>>();

/// <summary>
/// The Rows of the GridView
/// </summary>
public List<IRow<ItemType>> Rows { get => RowList; set => RowList = value; }

///<inheritdoc/>
public List<IRow<ItemType>> RowList { get; set; } = new List<IRow<ItemType>>();

#region Templates
/// <summary>
/// The columns template of the GridView
/// </summary>
[Parameter] public RenderFragment Columns { get; set; }

/// <summary>
/// The ChildContent of the GridView
/// </summary>
[Parameter] public RenderFragment ChildContent { get; set; }
#endregion
protected override void OnInitialized()
{
Expand All @@ -61,34 +55,5 @@ protected override void OnInitialized()
GridViewColumnGenerator.GenerateColumns(this);
}
}

///<inheritdoc/>
public void AddColumn(IColumn<ItemType> column)
{
ColumnList.Add(column);
StateHasChanged();
}

///<inheritdoc/>
public void RemoveColumn(IColumn<ItemType> column)
{
ColumnList.Remove(column);
StateHasChanged();
}

///<inheritdoc/>
public void RemoveRow(IRow<ItemType> row)
{
Rows.Remove(row);
StateHasChanged();
}

///<inheritdoc/>
public void AddRow(IRow<ItemType> row)
{
Rows.Add(row);
StateHasChanged();
}

}
}
13 changes: 7 additions & 6 deletions src/BlazorWebFormsComponents/GridViewColumnGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace BlazorWebFormsComponents
/// <summary>
/// The GridView Column Generator
/// </summary>
public static class GridViewColumnGenerator
{
public static class GridViewColumnGenerator
{
/// <summary>
/// Generate columns for a given GridView based on the properties of given Type
/// </summary>
Expand All @@ -19,12 +19,13 @@ public static void GenerateColumns<ItemType>(GridView<ItemType> gridView)
var propertiesInfo = type.GetProperties(BindingFlags.Instance | BindingFlags.Public).OrderBy(x => x.MetadataToken);
foreach (var propertyInfo in propertiesInfo)
{
var newColumn = new BoundField<ItemType> {
var newColumn = new BoundField
{
DataField = propertyInfo.Name,
ParentColumnsCollection = gridView,
HeaderText = propertyInfo.Name
HeaderText = propertyInfo.Name,
};
gridView.ColumnList.Add(newColumn);
gridView.BoundFields.Add(newColumn);

}
}
}
Expand Down
Loading

0 comments on commit 5d1294b

Please sign in to comment.