|
| 1 | +<div class="container"> |
| 2 | + <h1 class="title is-size-1 has-text-centered">Shared Lists</h1> |
| 3 | + <p class="mb-4">Shared lists are lists where items can be dragged from one list to the other and vice-versa. Providing the same "Group" string name for both lists is what links them together. Note that when an item is dragged into a different list, it assumes the visual style of that list. This is because Blazor controls the rendering of the list items.</p> |
| 4 | + <Tabs CodeContent="@codeContent"> |
| 5 | + <ExampleContent> |
| 6 | + <div class="columns"> |
| 7 | + <div class="column"> |
| 8 | + <SortableList Group="sharedLists" Items="items1" Context="item" OnRemove="ListOneRemove"> |
| 9 | + <SortableItemTemplate> |
| 10 | + <div class=" card has-border has-background-white"> |
| 11 | + <p class="is-size-4 p-2 ml-4">@item.Name</p> |
| 12 | + </div> |
| 13 | + </SortableItemTemplate> |
| 14 | + </SortableList> |
| 15 | + </div> |
| 16 | + <div class="column"> |
| 17 | + <SortableList Group="sharedLists" OnRemove="ListTwoRemove" Items="items2" Context="item"> |
| 18 | + <SortableItemTemplate> |
| 19 | + <div class="card has-background-white has-border"> |
| 20 | + <p class="is-size-4 p-2 ml-4">@item.Name</p> |
| 21 | + </div> |
| 22 | + </SortableItemTemplate> |
| 23 | + </SortableList> |
| 24 | + </div> |
| 25 | + </div> |
| 26 | + </ExampleContent> |
| 27 | + </Tabs> |
| 28 | +</div> |
| 29 | + |
| 30 | +@code { |
| 31 | + |
| 32 | + private string codeContent = $@" |
| 33 | + <SortableList Group=""sharedLists"" Items=""items1"" Context=""item"" OnRemove=""ListOneRemove""> |
| 34 | + <SortableItemTemplate> |
| 35 | + <div class="" card has-border has-background-white""> |
| 36 | + <p class=""is-size-4 p-2 ml-4"">@item.Name</p> |
| 37 | + </div> |
| 38 | + </SortableItemTemplate> |
| 39 | + </SortableList> |
| 40 | + <SortableList Group=""sharedLists"" OnRemove=""ListTwoRemove"" Items=""items2"" Context=""item""> |
| 41 | + <SortableItemTemplate> |
| 42 | + <div class=""card has-background-white has-border""> |
| 43 | + <p class=""is-size-4 p-2 ml-4"">@item.Name</p> |
| 44 | + </div> |
| 45 | + </SortableItemTemplate> |
| 46 | + </SortableList> |
| 47 | +
|
| 48 | + @code {{ |
| 49 | + private void ListOneRemove((int oldIndex, int newIndex) indices) |
| 50 | + {{ |
| 51 | + // get the item at the old index in list 1 |
| 52 | + var item = items1[indices.oldIndex]; |
| 53 | +
|
| 54 | + // add it to the new index in list 2 |
| 55 | + items2.Insert(indices.newIndex, item); |
| 56 | +
|
| 57 | + // remove the item from the old index in list 1 |
| 58 | + items1.Remove(items1[indices.oldIndex]); |
| 59 | + }} |
| 60 | +
|
| 61 | + private void ListTwoRemove((int oldIndex, int newIndex) indices) |
| 62 | + {{ |
| 63 | + // get the item at the old index in list 2 |
| 64 | + var item = items2[indices.oldIndex]; |
| 65 | +
|
| 66 | + // add it to the new index in list 1 |
| 67 | + items1.Insert(indices.newIndex, item); |
| 68 | +
|
| 69 | + // remove the item from the old index in list 2 |
| 70 | + items2.Remove(items2[indices.oldIndex]); |
| 71 | + }} |
| 72 | + }} |
| 73 | + "; |
| 74 | + public class Item |
| 75 | + { |
| 76 | + public int Id { get; set; } |
| 77 | + public string Name { get; set; } = ""; |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + public List<Item> items1 = Enumerable.Range(1, 10).Select(i => new Item { Id = i, Name = $"Item {i}" }).ToList(); |
| 82 | + |
| 83 | + public List<Item> items2 = Enumerable.Range(11, 10).Select(i => new Item { Id = i, Name = $"Item {i}" }).ToList(); |
| 84 | + |
| 85 | + private void ListOneRemove((int oldIndex, int newIndex) indices) |
| 86 | + { |
| 87 | + // get the item at the old index in list 1 |
| 88 | + var item = items1[indices.oldIndex]; |
| 89 | + |
| 90 | + // add it to the new index in list 2 |
| 91 | + items2.Insert(indices.newIndex, item); |
| 92 | + |
| 93 | + // remove the item from the old index in list 1 |
| 94 | + items1.Remove(items1[indices.oldIndex]); |
| 95 | + } |
| 96 | + |
| 97 | + private void ListTwoRemove((int oldIndex, int newIndex) indices) |
| 98 | + { |
| 99 | + // get the item at the old index in list 2 |
| 100 | + var item = items2[indices.oldIndex]; |
| 101 | + |
| 102 | + // add it to the new index in list 1 |
| 103 | + items1.Insert(indices.newIndex, item); |
| 104 | + |
| 105 | + // remove the item from the old index in list 2 |
| 106 | + items2.Remove(items2[indices.oldIndex]); |
| 107 | + } |
| 108 | +} |
0 commit comments