Skip to content

Commit

Permalink
Adds SortGroup new prefered method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbomhold3 committed Mar 7, 2021
1 parent 9b9516a commit 032e365
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 110 deletions.
13 changes: 5 additions & 8 deletions src/BlazorSortableJS.DemoCSB/DemoCSB.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.*" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorSortableJS.DemoCore\DemoCore.csproj" />
</ItemGroup>


</Project>
11 changes: 7 additions & 4 deletions src/BlazorSortableJS.DemoCore/DemoCore.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.4" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.*" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorSortableJS\BlazorSortableJS.csproj" />
Expand Down
172 changes: 114 additions & 58 deletions src/BlazorSortableJS.DemoCore/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -1,81 +1,137 @@
@page "/"
Sortable List
<button @onclick="New">Add</button>
<SortableGroup Class="list-group" TItem="string" @ref="MyGroup">
@foreach (var item in @MyGroup.Sortable.GetRaw())
{
<SortableItem Class="list-group-item" Item="item" TItem="string">@item.Data</SortableItem>
}
</SortableGroup>
Same Group
<SortableGroup Class="list-group" TItem="string" @ref="My2Group">
@foreach (var item in @My2Group.Sortable.GetRaw())
{
<SortableItem Class="list-group-item" Item="item" TItem="string">@item.Data</SortableItem>
}
</SortableGroup>
Resulting Data
@using System.Text.Json
<button @onclick="Remove">Remove One</button>

<div class="row">
<div class="col-6">Sortable List
<SortGroup Items="items" Id="@Id1" Class="list-group" TemplateClass="list-group-item" @ref="MyGroup" TItem="Items" GroupName="test" OnAdd="OnAdd" OnSort="OnSort" OnRemove="OnRemove">
<Template Context="test">
@test.Data.Text - @test.Data.Id
</Template>
</SortGroup>
Same Group
<SortGroup Items="items2" Id="@Id2" Class="list-group" TemplateClass="list-group-item" OnAdd="OnAdd" OnSort="OnSort" OnRemove="OnRemove" TItem="Items" @ref="My2Group" GroupName="test">
<Template Context="test">
@test.Data.Text - @test.Data.Id
</Template>
</SortGroup>
</div>
<div class="col-6">

List 1 Raw Contents
<ul class="list-group">
@foreach (var item in items.OrderBy(q => q.Order))
{
<li class="list-group-item">@item.Order - @item.Text - @item.Id</li>
}
</ul>
List 2 Raw Contents
<ul class="list-group">
@foreach (var item in items2.OrderBy(q => q.Order))
{
<li class="list-group-item">@item.Order -@item.Text - @item.Id</li>
}
</ul>
</div>
</div>
Resulting For Gorup 2
<ul class="list-group">
@foreach (var item in resultsList)
@foreach (var item in ResultsList)
{
<li class="list-group-item">@item</li>
<li class="list-group-item">@item.Text - @item.Order - @item.Id</li>
}
</ul>

@code
{
SortableGroup<string> MyGroup;
SortableGroup<string> My2Group;
List<string> items { get; set; } = new List<string> { "T1", "T2", "T3" };
List<string> items2 { get; set; } = new List<string> { "T4", "T5", "T6" };
List<string> resultsList { get; set; } = new List<string>();
string Id1 = Guid.NewGuid().ToString();
string Id2 = Guid.NewGuid().ToString();
int Order = 0;
SortGroup<Items> MyGroup;
SortGroup<Items> My2Group;
List<Items> items { get; set; } = new List<Items>
{
new Items("T1"),
new Items("T2"),
new Items("T3"),
};
List<Items> items2 { get; set; } = new List<Items>
{
new Items("T4"),
new Items("T5"),
new Items("T6"),
};
List<Items> ResultsList { get; set; } = new List<Items>();

public async Task New()
public async Task Remove()
{
var items = new List<string> { "T1", "T2", "T3", "T4", "T5" };
await MyGroup.Sortable.ClearAsync();
await MyGroup.Sortable.CreateAsync(MyGroup.Id, new SortableJsOptions
// This is just a shortcut for the demo
try
{
Group = "test",
Animation = 100,
// OnSort = async (e) => { resultsList = await MyGroup.Sortable.GetData(); StateHasChanged(); }
});

MyGroup.Sortable.SetData(items);
StateHasChanged();
items.Remove(items.First());
}
catch{}
// Inform Group of direct changes.
await MyGroup.UpdateAsync();
ResultsList = await My2Group.GetOrderListAsync("Order");
}
protected override Task OnInitializedAsync()

public async Task OnSort(SortableEvent<Items> e)
{
resultsList = items;
return base.OnInitializedAsync();
ResultsList = await My2Group.GetOrderListAsync("Order");
}
public void Test(SortableJSEvent e)
public async Task OnAdd(SortableEvent<Items> e)
{
Console.WriteLine(e);
// Changes are already tracked do not use UpdateAsync()
if (e.Sender.Id == Id1)
{
items.Add(e.Data);
}
else
{
items2.Add(e.Data);
}
_= await MyGroup.GetOrderListAsync("Order");
ResultsList = await My2Group.GetOrderListAsync("Order");
}
protected async override Task OnAfterRenderAsync(bool firstrun)
public async Task OnRemove(SortableEvent<Items> e)
{
if (firstrun)
// Changes are already tracked do not use UpdateAsync()
if (e.Sender.Id == Id1)
{
MyGroup.Sortable.SetData(items);
await MyGroup.Sortable.CreateAsync(MyGroup.Id, new SortableJsOptions
{
Group = "test",
Animation = 100,
OnClone = Test
// OnSort = async (e) => { resultsList = await MyGroup.Sortable.GetData(); StateHasChanged(); }
});
var item = items.FirstOrDefault(q => q.Id == e.Data.Id);
if (item != null)
items.Remove(item);
}
else
{
var item = items2.FirstOrDefault(q => q.Id == e.Data.Id);
if (item != null)
items2.Remove(item);
}
_ = await MyGroup.GetOrderListAsync("Order");
ResultsList = await My2Group.GetOrderListAsync("Order");
}

My2Group.Sortable.SetData(items2);
await My2Group.Sortable.CreateAsync(My2Group.Id, new SortableJsOptions
{
Group = "test",
Animation = 100,
OnClone = Test,
OnSort = async (e) => { resultsList = await My2Group.Sortable.GetDataAsync(); StateHasChanged(); }
});
protected override Task OnInitializedAsync()
{
return base.OnInitializedAsync();
}


public class Items
{
public int Order { get; set; } = 0;
public string Text { get; set; }
public string Id { get; set; } = Guid.NewGuid().ToString();

StateHasChanged();
public Items()
{

}
public Items(string text)
{
Text = text;
}
}
}
2 changes: 1 addition & 1 deletion src/BlazorSortableJS.DemoSSB/DemoSSB.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFrameworks>netcoreapp3.0;net5.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

Expand Down
13 changes: 8 additions & 5 deletions src/BlazorSortableJS/BlazorSortableJS.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
<RazorLangVersion>3.0</RazorLangVersion>
<PackageId>BlazorSortableJs</PackageId>
<Version>1.0.4</Version>
<Version>1.0.5</Version>
<Authors>John J Bomhold</Authors>
<Title>Blazor SortableJs Wrapper</Title>
<Description>
Expand All @@ -18,9 +18,12 @@
<Copyright>John J Bomhold</Copyright>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.*" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\js\" />
Expand Down
3 changes: 0 additions & 3 deletions src/BlazorSortableJS/Components/DynamicComponentBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

namespace BlazorSortableJS.Components
{
Expand Down
28 changes: 28 additions & 0 deletions src/BlazorSortableJS/Components/SortGroup.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@typeparam TItem
<CascadingValue Value="@Sortable">
@if(!ForceReDraw)
{
if (IsDiv)
{
<div class="@TemplateClass" @onclick="@OnClick" id="@Id" data-refId="@Sortable.RefId">
@foreach (var item in Sortable.GetRaw())
{
<div @key="item" @onclick="@TemplateOnClick" data-id="@item.DataId" Id="@(Guid.NewGuid())" class="@TemplateClass">
@Template(item)
</div>
}
</div>
}
else
{
<ul class="@Class" @onclick="@OnClick" id="@Id" data-refId="@Sortable.RefId">
@foreach (var item in Sortable.GetRaw())
{
<li @key="item" @onclick="@TemplateOnClick" data-id="@item.DataId" Id="@(Guid.NewGuid())" class="@TemplateClass">
@Template(item)
</li>
}
</ul>
}
}
</CascadingValue>
Loading

0 comments on commit 032e365

Please sign in to comment.