Reputation: 91
I'm working on server-side Blazor Project using RadzenPickList component to display a list of data from my People table. I want to filter the data based on CompanyId and TypeId before displaying it in the PickList. This is my code below
<RadzenFormField Text="Class Type">
<ChildContent>
<RadzenTextBox @bind-Value="@classTypeName" Disabled="true" />
</ChildContent>
<Helper>
<RadzenRequiredValidator Component="selectedClassType" Text="Class Type is required" />
</Helper>
</RadzenFormField>
<RadzenFormField Text="Lecturer">
<ChildContent>
<RadzenTextBox @bind-Value="lecturerName" Disabled="true" />
</ChildContent>
<Helper>
<RadzenRequiredValidator Component="selectedClassType" Text="Class Type is required" />
</Helper>
</RadzenFormField>
<RadzenCard>
<RadzenPickList @bind-Source="@Source" @bind-Target="@Target" Style="height:300px; width:100%;" Orientation="Orientation.Horizontal"
TextProperty="@nameof(ClassMembers.Name)" AllowFiltering="true" Multiple="true" ShowHeader="true" Disabled="false"
ButtonGap="12px" ButtonJustifyContent="JustifyContent.Center" ButtonStyle="ButtonStyle.Secondary" ButtonSize="ButtonSize.Medium"
ButtonShade="Shade.Default" ButtonVariant="Variant.Flat">
<SourceHeader>
Member:
</SourceHeader>
<TargetHeader>
Selected Member:
</TargetHeader>
<Template>
@context.Name
</Template>
</RadzenPickList>
</RadzenCard>
@code
{
private int selectedCompany;
private int selectedPeopleType = 1;
private IEnumerable<People> Peoples;
private IEnumerable<People> _source;
private IEnumerable<People> Source
{
get
{
return _source;
}
set
{
if (_source != value)
{
_source = value;
// Console.WriteLine("Source updated);
}
}
}
private IEnumerable<People> _target;
private IEnumerable<People> Target
{
get
{
return _target;
}
set
{
if (_target != value)
{
_target = value;
}
}
}
protected override async Task OnInitializedAsync()
{
Peoples = await DbContext.People
.Select(pl => new People
{
Id = pl.Id,
Name = pl.Name,
CompanyId = pl.CompanyId,
TypeId = pl.TypeId
}).ToListAsync() ?? new List<People>();
Source = Peoples.Where(pl => pl.CompanyId == selectedCompany && pl.TypeId == selectedPeopleType).ToList();
await base.OnInitializedAsync();
}
}
I want to filter the Peoples collection based on CompanyId and TypeId. However, the filtered data is not showing up. I've tried to fetch the data without filter and it showed up just fine. But when I add the filtering, no data is fetched. Does anyone know what might be wrong here?
UPDATE
I've realized that what might be a cause is because the default value of selectedCompany and selectedPeopleType is 0. before we chose the People data, we chose for the selectedCompany and selectedPeopleType first. but it turns out that the value doesn't change which cause no data people fetch, and i still cant solve it yet.
Upvotes: -1
Views: 127
Reputation: 51295
Convert the comment that resolved the Post Owner's question into the answer.
Implement the filter criteria with the condition that bypasses the filter when the value to be filtered is the default value.
Would suggest writing a separate function to update the Source
array to avoid redundancy. This function will be called during OnInitializedAsync
and the change event by the drop-down.
@code {
protected override async Task OnInitializedAsync()
{
Peoples = await DbContext.People
.Select(pl => new People
{
Id = pl.Id,
Name = pl.Name,
CompanyId = pl.CompanyId,
TypeId = pl.TypeId
}).ToListAsync() ?? new List<People>();
FilterSource();
await base.OnInitializedAsync();
}
private void FilterSource()
{
Source = Peoples.Where(pl => (selectedCompany == 0 || pl.CompanyId == selectedCompany)
&& (selectedPeopleType == 0 || pl.TypeId == selectedPeopleType))
.ToList();
}
}
Work with Radzen Dropdown's Change
event to reflect the (filtered) Source
data when the selectedCompany
/selectedPeopleType
value is changed.
<RadzenDropDown TValue="int" @bind-Value=@selectedCompany Data=@Companies
TextProperty="Name" ValueProperty="Id"
Change="@FilterSource" Name="SelectCompany" />
<RadzenDropDown TValue="int" @bind-Value=@selectedPeopleType Data=@Peoples
TextProperty="Name" ValueProperty="Id"
Change="@FilterSource" Name="SelectPeopleType" />
Upvotes: 0