How to repopulate list items in code
I have a class containing multiple ObservableCollection lists. When I change the contents of a list I would like the associated dropdown to repopulate its list with new values. However, when I attempt to repopulate the list, the dropdown remains unchanged from its initial values.
@using System.Collections.ObjectModel;
<SfDropDownList TValue="int"
TItem="Quantity"
<DropDownListTemplates TItem="Quantity">
<ItemTemplate>
<span><span>@((context as Quantity).NumericValue)</span> <span>@((context as Quantity).Symbol)</span></span>
</ItemTemplate>
</DropDownListTemplates>
<DropDownListFieldSettings Text="Symbol" Value="UnitValue"></DropDownListFieldSettings>
<DropDownListEvents TValue="string" ValueChange="OnPreferredUnitChange"></DropDownListEvents>
</SfDropDownList>
<p>Current value: @currentValue</p>
<button class="btn btn-primary" @onclick="IncrementCount">Increment</button>
@code {
int currentValue = 12;
public QuantityLists Lists = new QuantityLists();
protected override void OnInitialized()
{
// Build lists
Lists.AQuantityList.Add(new Quantity { NumericValue = currentValue.ToString(), UnitValue = 0, Symbol = "%P" });
Lists.AQuantityList.Add(new Quantity { NumericValue = "1.000", UnitValue = 1, Symbol = "SG" });
}
void IncrementCount()
{
currentValue++;
Lists.AQuantityList.Clear();
Lists.AQuantityList.Add(new Quantity() { NumericValue = currentValue.ToString(), UnitValue = 0, Symbol = "%P" });
Lists.AQuantityList.Add(new Quantity() { NumericValue = "2.000", UnitValue = 1, Symbol = "SG" });
}
void OnPreferredUnitChange(Syncfusion.Blazor.DropDowns.ChangeEventArgs<string> args)
{
//handle input [UnitValue]
}
public class QuantityLists
{
public ObservableCollection<Quantity> AQuantityList { get; set; }
// More lists...
public QuantityLists()
{
AQuantityList = new ObservableCollection<Quantity>();
}
}
public class Quantity
{
public string NumericValue { get; set; }
public int UnitValue { get; set; }
public string Symbol { get; set; }
}
}
SIGN IN To post a reply.
3 Replies
1 reply marked as answer
PO
Prince Oliver
Syncfusion Team
September 2, 2020 06:26 PM UTC
Hi Dave,
Thank you for contacting us.
We have investigated the reported case “when I attempt to repopulate the list, the dropdown remains unchanged from its initial values.” in our end. This occurs due to calling the clear method in the observable collection. So instead of calling the clear method, we recommend re-initializing the observable collection with a new instance. Please refer to the below code changes
Your current code
|
void IncrementCount()
{
currentValue++;
Lists.AQuantityList.Clear();
Lists.AQuantityList.Add(new Quantity() { NumericValue = currentValue.ToString(), UnitValue = 0, Symbol = "%P" });
Lists.AQuantityList.Add(new Quantity() { NumericValue = "2.000", UnitValue = 1, Symbol = "SG" });
}
|
Updated code
|
void IncrementCount()
{
currentValue++;
Lists.AQuantityList = new ObservableCollection<Quantity>();
Lists.AQuantityList.Add(new Quantity() { NumericValue = currentValue.ToString(), UnitValue = 0, Symbol = "%P" });
Lists.AQuantityList.Add(new Quantity() { NumericValue = "2.000", UnitValue = 1, Symbol = "SG" });
}
|
Let us know if you need any further assistance on this.
Regards,
Prince
Marked as answer
DA
Dave
September 2, 2020 07:36 PM UTC
Perfect, thanks Prince. Sorry to trouble you with my dotnet misunderstanding.
SP
Sureshkumar P
Syncfusion Team
September 3, 2020 05:57 AM UTC
Hi Dave,
Thanks for the update.
Regards,
Sureshkumar P
SIGN IN To post a reply.