No term selection option in TaxonomyField (#8667)

* Added no term selection option when taxonomy field is not required and is set to be shown as a radio button set (single choice without autocomplete)

* Correctly checked the "no selection" option when SingleTermId == 0.
This commit is contained in:
Andrea Piovanelli 2023-04-21 08:53:55 +02:00 committed by GitHub
parent 10e5078956
commit 55ec9e2335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,8 @@
int termIndex = 0; int termIndex = 0;
var settings = Model.Settings; var settings = Model.Settings;
var noSelectionId = Guid.NewGuid();
} }
<fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)"> <fieldset class="taxonomy-wrapper" data-name-prefix="@Html.FieldNameFor(m => m)" data-id-prefix="@Html.FieldIdFor(m => m)">
@ -21,26 +23,31 @@
<span class="hint">@Model.Settings.Hint</span> <span class="hint">@Model.Settings.Hint</span>
} }
<ul class="terms"> <ul class="terms">
@foreach (var entry in Model.Terms) { @if (Model.Settings.SingleChoice && !settings.Required) {
var ti = termIndex; <li>
<li> <input type="radio" value="0" @if (Model.SingleTermId == 0) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@noSelectionId" data-term="@T("No selection")" />
@* Tabs for levels *@ @for (var i = 1; i <= entry.GetLevels(); i++) { <span class="gap">&nbsp;</span> } <label class="forcheckbox" for="@noSelectionId">@T("No selection")</label>
@{ </li>
var disabled = !entry.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(entry.Path + entry.Id))); }
if (Model.Settings.SingleChoice) { @foreach (var entry in Model.Terms) {
<input @if (disabled) { <text> disabled="disabled" </text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" /> var ti = termIndex;
} <li>
else { @* Tabs for levels *@ @for (var i = 1; i <= entry.GetLevels(); i++) {<span class="gap">&nbsp;</span>}
<input @if (disabled) { <text> disabled="disabled" </text> } type="checkbox" value="true" @if (entry.IsChecked) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" /> @{
} var disabled = !entry.Selectable || (Model.Settings.LeavesOnly && Model.Terms.Any(t => t.Path.Contains(entry.Path + entry.Id)));
if (Model.Settings.SingleChoice) {
<input @if (disabled) { <text> disabled="disabled" </text> } type="radio" value="@Model.Terms[ti].Id" @if (entry.Id == Model.SingleTermId) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.SingleTermId)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" />
} else {
<input @if (disabled) { <text> disabled="disabled" </text> } type="checkbox" value="true" @if (entry.IsChecked) { <text> checked="checked" </text> } name="@Html.FieldNameFor(m => m.Terms[ti].IsChecked)" id="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)" data-term="@entry.Name.ToLower()" />
} }
@Html.HiddenFor(m => m.Terms[ti].Id) }
<label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name</label> @Html.HiddenFor(m => m.Terms[ti].Id)
</li> <label class="forcheckbox" for="@Html.FieldIdFor(m => m.Terms[ti].IsChecked)">@entry.Name</label>
termIndex++; </li>
} termIndex++;
</ul> }
</ul>
@if (Model.TaxonomyId == 0) { @if (Model.TaxonomyId == 0) {
<p>@T("You haven't specified a taxonomy for {0}", Model.DisplayName)</p> <p>@T("You haven't specified a taxonomy for {0}", Model.DisplayName)</p>