Skip to content

Commit

Permalink
bug fixes for JingetDropDownTreeList
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Aug 5, 2024
1 parent acf70db commit 0a49eea
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,49 @@

public class JingetDropDownListTreeElement : JingetDropDownListElementBase
{
/// <summary>
/// Use this method whenever the Id property has int type
/// </summary>
public async Task<List<JingetDropDownTreeItemModel>> BindAsync<T>(Func<Task<List<T>>> GetData)
where T : BaseTypeTreeModel
{
return await BindAsync<T, int>(GetData).ConfigureAwait(false);
}

public async Task<List<JingetDropDownTreeItemModel>> BindAsync<T, TCode>(Func<Task<List<T>>> GetData)
where T : BaseTypeTreeModel<TCode>
/// <summary>
/// Use this method whenever the Id property has type of struct
/// </summary>
public async Task<List<JingetDropDownTreeItemModel>> BindAsync<TModel, TId>(Func<Task<List<TModel>>> GetData)
where TModel : BaseTypeTreeModel<TId>
where TId : struct
{
List<JingetDropDownTreeItemModel> result = [];

List<T> data = await GetData.Invoke().ConfigureAwait(false);
List<TModel> data = await GetData.Invoke().ConfigureAwait(false);

if (data != null)
{
foreach (T item in data)
foreach (TModel item in data)
{
result.Add(new(item.Id, item.ParentId, item.Title));
}
}
return result;
}

/// <summary>
/// Use this method whenever the Id property has string type
/// </summary>
public async Task<List<JingetDropDownTreeItemModel>> BindStringAsync<TModel>(Func<Task<List<TModel>>> GetData)
where TModel : BaseTypeStringTreeModel
{
List<JingetDropDownTreeItemModel> result = [];

List<TModel> data = await GetData.Invoke().ConfigureAwait(false);

if (data != null)
{
foreach (TModel item in data)
{
result.Add(new(item.Id, item.ParentId, item.Title));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
<option value="---" data-parent="" data-level=1>@DefaultText</option>
@foreach (var item in Items)
{
bool selected = item.Value != null && Value != null && item.Value.ToString().Equals(Value.ToString());
if (selected)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_selectItem", Id, Value).GetAwaiter().GetResult();
});
}
@* bool selected = item.Value != null && Value != null && item.Value.ToString().Equals(Value.ToString()); *@
@* if (selected)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_selectItem", Id, Value).GetAwaiter().GetResult();
});
} *@
@if (item.ParentValue == null)
{
<option value="@item.Value" data-parent="" data-level="@item.Padding">@item.Text</option>
Expand All @@ -29,16 +29,16 @@
{
<option value="@item.Value" data-parent="@item.ParentValue" data-level="@item.Padding">@item.Text</option>
}
@if (Value == null)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_clear", Id).GetAwaiter().GetResult();
});
}
}
@* @if (Value == null)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_clear", Id).GetAwaiter().GetResult();
});
} *@
</select>
@if (IsRequired && (SelectedItem == null || string.IsNullOrWhiteSpace(SelectedItem.Value?.ToString())))
@if (!HasSelectedValue())
{
<label style="color:red">@RequiredError</label>
}
Expand All @@ -49,12 +49,17 @@
{
if (_initialized)
{
// if (firstRender)
// {
//initialize drop down list by calling initJingetDropDownListTree. this functionality is mainly powered by select2.js library.
await InitComponentAsync("initJingetDropDownListTree");
await base.OnAfterRenderAsync(firstRender);
//}
//initialize drop down list by calling initJingetDropDownListTree. this functionality is mainly powered by select2.js library.
await InitComponentAsync("initJingetDropDownListTree");
await base.OnAfterRenderAsync(firstRender);
}
if (Value == null)
{
await JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_clear", Id);
}
else
{
await JS.InvokeVoidAsync("jinget_blazor_dropdownlist_tree_selectItem", Id, Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@

@if (connected)
{
<select id="@Id"
class="@CssClass"
disabled="@IsDisabled"
required="@IsRequired">
<option value="">@DefaultText</option>
<select id="@Id"
class="@CssClass"
disabled="@IsDisabled"
required="@IsRequired">
<option value="">@DefaultText</option>
@foreach (var item in Items)
{
bool selected = item.Value != null && Value != null && item.Value.ToString().Equals(Value.ToString());
if (selected)
@* if (selected)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_selectItem", Id, Value).GetAwaiter().GetResult();
});
}
} *@
<option value="@item.Value" selected="@selected">@item.Text</option>
@* <option value="@item.Value">@item.Text</option> *@
}
@if (Value == null)
@* @if (Value == null)
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_clear", Id).GetAwaiter().GetResult();
});
}
</select>
@if (IsRequired && (SelectedItem == null || string.IsNullOrWhiteSpace(SelectedItem.Value?.ToString())))
else
{
Task.Run(() =>
{
JS.InvokeVoidAsync("jinget_blazor_dropdownlist_selectItem", Id, Value).GetAwaiter().GetResult();
});
} *@
</select>
@if (!HasSelectedValue())
{
<label style="color:red">@RequiredError</label>
<label style="color:red">@RequiredError</label>
}
}
@code {
Expand All @@ -44,5 +52,13 @@
await InitComponentAsync("initJingetDropDownList");
await base.OnAfterRenderAsync(firstRender);
}
if (Value == null)
{
await JS.InvokeVoidAsync("jinget_blazor_dropdownlist_clear", Id);
}
else
{
await JS.InvokeVoidAsync("jinget_blazor_dropdownlist_selectItem", Id, Value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,17 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
await base.OnAfterRenderAsync(firstRender);
}

bool HasValue(object? value) => value != null && value.ToString() != "---";

/// <summary>
/// Set <seealso cref="SelectedItem"/> using item value(<seealso cref="JingetDropDownItemModel.Value"/>) in <seealso cref="Items"/>
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public async Task SetSelectedItemAsync(object? value)
{
Value = value;
await OnSelectedItemChangedAsync(value);
Value = HasValue(value) ? value : null;
await OnSelectedItemChangedAsync(Value);
}

/// <summary>
Expand All @@ -136,23 +138,18 @@ public async Task SetSelectedItemAsync(object? value)
/// This method is being invoked by jinget.custom.js. whenever searchable dropdownlist's selected item changed
/// </summary>
[JSInvokable]
public void OnJSDropDownListSelectedItemChanged(object? e) => OnSelectedItemChangedAsync(e);
public void OnJSDropDownListSelectedItemChanged(object? e) => SetSelectedItemAsync(e);

protected async Task OnSelectedItemChangedAsync(object? e)
{
Value = e;

if (e == null)
{
SelectedItem = null;
}
else
{
SelectedItem = Items.FirstOrDefault(x => x.Value?.ToString() == e.ToString());
}
SelectedItem = e == null ? null : Items.FirstOrDefault(x => x.Value?.ToString() == e.ToString());
StateHasChanged();
await OnChange.InvokeAsync(new ChangeEventArgs { Value = e });
}

protected internal bool HasSelectedValue() =>
IsRequired &&
(SelectedItem == null || string.IsNullOrWhiteSpace(SelectedItem.Value?.ToString()));
//public abstract Task DataBindAsync();
}
11 changes: 7 additions & 4 deletions 04-Presentation/Jinget.Blazor/Models/BaseTypeModel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
namespace Jinget.Blazor.Models;

public class BaseTypeModel : BaseTypeModel<byte> { }
public class BaseTypeModel<TId>
{
public virtual TId Id { get; set; }

public virtual string? Title { get; set; }
}
public class BaseTypeModel : BaseTypeModel<byte> { }

public class BaseTypeTreeModel<TId> : BaseTypeModel<TId>
public class BaseTypeTreeModel : BaseTypeTreeModel<int> { }
public class BaseTypeTreeModel<TId> : BaseTypeModel<TId> where TId : struct
{
public virtual TId? ParentId { get; set; }
}
public class BaseTypeTreeModel : BaseTypeTreeModel<int> { }
public class BaseTypeStringTreeModel : BaseTypeModel<string>
{
public virtual string? ParentId { get; set; }
}
6 changes: 3 additions & 3 deletions 04-Presentation/Jinget.Blazor/wwwroot/js/jinget.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2ext.js',
url: '_content/Jinget.Blazor/js/jinget.jalali.picker.date.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.jalali.picker.date.js',
url: '_content/Jinget.Blazor/js/jinget.custom.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.custom.js',
url: '_content/Jinget.Blazor/js/jinget.select2ext.js',
callback: loadScript({
url: '_content/Jinget.Blazor/js/jinget.select2tree.js'
})
Expand Down
1 change: 1 addition & 0 deletions 04-Presentation/Jinget.Blazor/wwwroot/js/jinget.custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ window.initJingetDropDownListTree = (params = {
} = {}) => {
var element = $('#' + params.id).jinget_select2tree(
{
id: params.id,
dir: params.isRtl ? 'rtl' : 'ltr',
closeOnSelect: true,
theme: 'outlined',
Expand Down
Loading

0 comments on commit 0a49eea

Please sign in to comment.