TelerikGrid 异步数据源

huangapple go评论71阅读模式
英文:

TelerikGrid async datasource

问题

我试图从数据库异步获取数据来设置网格的数据,但它不起作用。可能我漏掉了一些东西。

@page "/Test"
@using System.Dynamic;

<div class="col-md-12">
    <TelerikButton OnClick="ButtonClick">点击我以查看数据</TelerikButton>
    <TelerikGrid Class="grid"
                 Data=@GridFilter
                 Sortable="true"
                 FilterMode="GridFilterMode.FilterMenu"
                 Resizable="true"
                 SelectionMode="GridSelectionMode.Multiple"
                 Pageable="true"
                 PageSize=20
                 Navigable="true">

        <GridColumns>
            <GridColumn Field="Field1" Title="Field1" />
            <GridColumn Field="Field2" Width="300px" Title="Field2" TextAlign="@ColumnTextAlign.Center" />
        </GridColumns>
    </TelerikGrid>
</div>

@code {
    private List<dynamic> GridFilter;

    private async Task ButtonClick()
    {
        GridFilter = await this.GetData();
    }

    private async Task<List<dynamic>> GetData()
    {
        Task<List<dynamic>> b = new(() => {
            dynamic A = new ExpandoObject();
            A.Field1 = "Field 1";
            A.Field2 = "Field 2";
            List<dynamic> grid = new();
            grid.Add(A);
            return grid;
        });
        return await b;
    }
}

希望这有助于您解决问题。

英文:

I'm trying to set data of grid by fetching data asynchronously from DB but it is not working. Probably I'm missing something.

@page &quot;/Test&quot;
@using System.Dynamic;

&lt;div class=&quot;col-md-12&quot;&gt;
    &lt;TelerikButton OnClick=&quot;ButtonClick&quot;&gt;Click Me to see data&lt;/TelerikButton&gt;
    &lt;TelerikGrid Class=&quot;grid&quot;
                 Data=@GridFilter
                 Sortable=&quot;true&quot;
                 FilterMode=&quot;GridFilterMode.FilterMenu&quot;
                 Resizable=&quot;true&quot;
                 SelectionMode=&quot;GridSelectionMode.Multiple&quot;
                 Pageable=&quot;true&quot;
                 PageSize=20
                 Navigable=&quot;true&quot;&gt;

        &lt;GridColumns&gt;
            &lt;GridColumn Field=&quot;Field1&quot; Title=&quot;Field1&quot; /&gt;
            &lt;GridColumn Field=&quot;Field2&quot; Width=&quot;300px&quot; Title=&quot;Field2&quot; TextAlign=&quot;@ColumnTextAlign.Center&quot; /&gt;
        &lt;/GridColumns&gt;
    &lt;/TelerikGrid&gt;
&lt;/div&gt;

}
@code {
    private List&lt;dynamic&gt; GridFilter;

    private async Task ButtonClick()
    {
        GridFilter = await this.GetData();
    }

    private async Task&lt;List&lt;dynamic&gt;&gt; GetData()
    {
        Task&lt;List&lt;dynamic&gt;&gt; b = new(() =&gt; {
            dynamic A = new ExpandoObject();
            A.Field1 = &quot;Field 1&quot;;
            A.Field2 = &quot;Field 2&quot;;
            List&lt;dynamic&gt; grid = new();
            grid.Add(A);
            return grid;
        });
        return await b;
    }

}

答案1

得分: 1

根据此文档,如果您希望在更新列表时使网格自动更新,您必须将集合声明为ObservableCollection。您可能还需要初始化一个空集合,以便网格可以订阅其事件:

private ObservableCollection<dynamic> GridFilter = new ObservableCollection<dynamic>();

但这意味着您不能用新的集合实例替换GridFilter,就像您在这里所做的一样:

GridFilter = await this.GetData();

您必须将每个项目添加到现有集合中。您可以在ButtonClick()事件中执行此操作,或者直接在GetData()中将项目添加到GridFilter中。

不幸的是,ObservableCollection不支持AddRange(),所以您必须一个一个添加。或者,您可以使用某人编写的ObservableRangeCollection类,使其支持AddRange

另一种方法是在更改集合后调用网格上的Rebind方法,可能看起来像这样(未经测试,可能需要一些调整):

<TelerikGrid Class="grid"
             Data=@GridFilter
             Sortable="true"
             FilterMode="GridFilterMode.FilterMenu"
             Resizable="true"
             SelectionMode="GridSelectionMode.Multiple"
             Pageable="true"
             PageSize=20
             Navigable="true"
             @ref="@GridRef">
private async Task ButtonClick()
{
    GridFilter = await this.GetData();
    GridRef.Rebind();
}
英文:

Based on this documentation, if you want the grid to automatically update when you update the list, you have to declare the collection as an ObservableCollection. You'll likely also have to initialize an empty collection so that the grid can subscribe to its events:

private ObservableCollection&lt;dynamic&gt; GridFilter = new ObservableCollection&lt;dynamic&gt;();

But that means you cannot replace GridFilter with a new instance of a collection, like you're doing here:

GridFilter = await this.GetData();

You'll have to add each item to the existing collection. You can either do that in your ButtonClick() event, or add the items directly to GridFilter in GetData().

ObservableCollection doesn't support AddRange() unfortunately, so you'll have to add one at a time. Or, you can use this ObservableRangeCollection class someone wrote to make it support AddRange.

The other way is to call the Rebind method on the grid after you change the collection, which might look something like this (untested, so it might need some tweaks):

    &lt;TelerikGrid Class=&quot;grid&quot;
                 Data=@GridFilter
                 Sortable=&quot;true&quot;
                 FilterMode=&quot;GridFilterMode.FilterMenu&quot;
                 Resizable=&quot;true&quot;
                 SelectionMode=&quot;GridSelectionMode.Multiple&quot;
                 Pageable=&quot;true&quot;
                 PageSize=20
                 Navigable=&quot;true&quot;
				 @ref=&quot;@GridRef&quot;&gt;
    private async Task ButtonClick()
    {
        GridFilter = await this.GetData();
		GridRef.Rebind();
    }

huangapple
  • 本文由 发表于 2023年3月31日 20:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定