question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatically sort table when property changed

See original GitHub issue

So let’s say I have this table:

image

On each action “Up” and “Down” I swap SortOrder property between items but I can’t find a way to refresh/re-render the table. It always stays the same.

Tried on every “Up” and “Down” to call this:

List = List.OrderBy(x => x.SortOrder).ToList();

as well as

await InvokeAsync(StateHasChanged)

Any idea?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kedzior-iocommented, Apr 21, 2021

Oh and by the way. Awesome work!

1reaction
anranruyecommented, Apr 21, 2021

@kedzior-io Well, this works for me:

@page "/demo" 
@using AntDesign
@using AntDesign.TableModels

<Table TItem="Data" DataSource="List">
    <Column Title="Name" @bind-Field="context.Name"></Column>
    <Column Title="Duration(Days)" @bind-Field="context.DurationInDays"></Column>
    <Column Title="Sort" @bind-Field="context.SortOrder"></Column>
    <ActionColumn>
        <Space>
            <SpaceItem>
                <Button Type="@ButtonType.Link" OnClick="() => { SortUp(context.Id); }">Up</Button>
            </SpaceItem>
            <SpaceItem>
                <Button Type="@ButtonType.Link" OnClick="() => { SortDown(context.Id); }">Down</Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

@code {
    public class Data
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int DurationInDays { get; set; }

        public int SortOrder { get; set; }
    }

    List<Data> List = new List<Data>();

    protected override void OnInitialized()
    {
        List.AddRange(Enumerable.Range(1, 5).Select(x => new Data
        {
            Id = x,
            DurationInDays = x * 2,
            Name = $"Item{x}",
            SortOrder = x
        }));
    }

    private void SortUp(int id)
    {
        var itemToSort = List.Single(x => x.Id == id);
        var itemIndex = List.IndexOf(itemToSort);
        if (itemIndex == 0)
        {
            return;
        }
        var newItemIndex = itemIndex - 1;
        itemToSort.SortOrder--;
        List[newItemIndex].SortOrder++;
        List = List.OrderBy(x => x.SortOrder).ToList();
    }

    private void SortDown(int id)
    {
        var itemToSort = List.Single(x => x.Id == id);
        var itemIndex = List.IndexOf(itemToSort);
        if (itemIndex == List.Count - 1)
        {
            return;
        }
        var newItemIndex = itemIndex + 1;
        itemToSort.SortOrder++;
        List[newItemIndex].SortOrder--;
        List = List.OrderBy(x => x.SortOrder).ToList();
    }
}

https://user-images.githubusercontent.com/54608128/115588404-4bfb7e80-a301-11eb-8cc0-8415640bf96b.mp4

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel: Automatically Sort When Data Changes or Added
In this video tutorial I explain how to get data to sort automatically when that data is changed or added to. 5 FREE...
Read more >
How to Automatically Sort Table Rows
1. Select your table. · 2. Go to Properties > RULES on the object inspector. · 3. To apply the rule, select the...
Read more >
JavaFX TableView sorting consistency on property update
The table can't re-sort on changes in the underlying data unless the SortedList receives information that those changes have actually occurred.
Read more >
Automatically sort rows when entering data in specific column
1. Select the columns to sort. · 2. In the ribbon, click Data > Sort. · 3. In the Sort popup window, in...
Read more >
Excel Automatically Sort When Data Changes or Added
Excel Automatically Sort When Data Changes or Added | Auto Sort Excel Formula | Auto Sort Macro · Comments45.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found