Sorting and Adjustment
Files Referenced in this Document
IMyEntityNameAppService.csMyEntityNameAppService.cs
Table of Contents
Introduction
In many business applications, users need to define a specific order for displaying items, independent of creation time or alphabetical order. The SortAsync method provides a manual ordering capability, allowing users to drag and drop items in the UI and persist this order to the backend.
Sorting Logic (SortAsync)
Method Signature: Task SortAsync(List<Guid> ids)
Description: Updates the display order of entities based on the sequence of IDs provided in the list.
Process:
- Receives an ordered list of entity IDs (
List<Guid>). - Iterates through the list, assigning a sequence number (index) to each entity.
- Updates the
SortIndex(or equivalent) field of each entity in the database. - Saves changes.
Implementation Strategy
The typical implementation involves fetching the entities (or updating them directly) and setting a sort order field.
public async Task SortAsync(List<Guid> ids)
{
// Simplified logic
for (int i = 0; i < ids.Count; i++)
{
var id = ids[i];
var entity = await Repository.GetAsync(id);
entity.SortIndex = i; // Assuming SortIndex property exists
await Repository.UpdateAsync(entity);
}
}
Note: The actual implementation might be optimized to use batch updates or direct SQL execution for better performance.
Usage Scenarios
- Menu Ordering: Reordering menu items in a navigation bar.
- Task Prioritization: Moving tasks up or down in a todo list.
- Display Configuration: Customizing the order of widgets on a dashboard.
Request:
POST /api/app/my-entity-name/sort
[
"first-item-id",
"second-item-id",
"third-item-id"
]
The backend will update "first-item-id" to have index 0, "second-item-id" index 1, etc.