I'm using Blazor Server 19.4 and I have a page full of radiobuttons to let the user the option to answer "yes" or "no" to a question
I don't find a simple solution to be able to click on the button and the second time to "unselect" the option leaving in this way "yes" or "no" totally empty in case the user is not sure about the answer.
The buttons are bound to a boolean value in DB where initially all the values are null
When I open the EditForm with inside all the radiobuttons, the first time all the buttons are empty and this is correct.
When I click the second time on an option, I tried to "reset" it using the "ondblclick" event as you can see below but this doen't work bacause the option is still selected.
I tried also using checkboxes where you can select and unselect an option but they can't be grouped in order to have a toogle selection between "yes" or "no" option
<EditForm EditContext="@EC">
<SfCard >
<CardContent>
<div class="form-row">
<div class="col-md-5">
<ul>
<li @ondblclick="Click">
<SfRadioButton Label="Epatite virale Si" LabelPosition="LabelPosition.Before" Name="epatite" Value=true Checked="@DatiCli.EPATITE" TChecked="bool?"></SfRadioButton>
<SfRadioButton Label="No" Name="epatite" LabelPosition="LabelPosition.Before" Value=false Checked="@DatiCli.EPATITE" TChecked="bool?"></SfRadioButton>
</li>
</ul>
</div>
</div>
private void Click()
{
DatiCli.EPATITE = null;
}
|
@using Syncfusion.Blazor.Buttons
@inject IJSRuntime JSRuntime;
<ul>
<li @ondblclick="click">
<SfRadioButton Label="Epatite virale Si" LabelPosition="LabelPosition.Before" Name="@name" Value=true TChecked="bool?"></SfRadioButton>
</li>
<li @ondblclick="click">
<SfRadioButton Label="No" Name="@name" LabelPosition="LabelPosition.Before" Value=false TChecked="bool?"></SfRadioButton>
</li>
</ul>
@code{
public string name = "epatite";
public bool? DatiCli_EPATITE = null;
public async Task click()
{
await JSRuntime.InvokeAsync<object>("registerClearCallback", name);
}
}
|
|
<script>
window.registerClearCallback = function (name) {
var rBtn = document.getElementsByName(name);
for (var i = 0; i < rBtn.length; i++) {
rBtn[i].checked = false;
}
}
</script>
|
Thanks for this solution, it's working fine
I have about 50 radiobuttons in my page, is there a way to pass to the click function the source of the dobleclick event ?
or do I have to create 50 functions like this ?
<li @ondblclick="click1">
...
<li @ondblclick="click49">
<li @ondblclick="click50">
|
<ul @ondblclick="click">
<li>
<SfRadioButton Label="Epatite virale Si" LabelPosition="LabelPosition.Before" Name="@name" Value=true TChecked="bool?"></SfRadioButton>
</li>
<li >
<SfRadioButton Label="No" Name="@name" LabelPosition="LabelPosition.Before" Value=false TChecked="bool?"></SfRadioButton>
</li>
</ul>
@code{
public string name = "epatite";
public bool? DatiCli_EPATITE = null;
public async Task click()
{
await JSRuntime.InvokeAsync<object>("registerClearCallback", name);
}
} |
I changed your last demo to show you what I need
I wrote only 2 radiobuttons but I have 50 of them in my page
I'd like to handle the doubleclick event in one method only but I need to pass also the Name value to the method
|
<ul>
@for (int i = 0; i < @data.Count; i++)
{
var radioData = data[i];
<li @ondblclick="@(e => click(@radioData.name))">
<SfRadioButton Label=@radioData.label LabelPosition=@radioData.position Name=@radioData.name Value=@radioData.value TChecked="bool?"></SfRadioButton>
</li>
}
</ul>
@code{
public string name = "epatite";
public bool? DatiCli_EPATITE = null;
public List<ListData>
data = new List<ListData>
{
new ListData{ label = "Epatite virale Si", position = LabelPosition.Before, name = "epatite" , value ="true" },
new ListData{ label = "No", position = LabelPosition.Before, name = "epatite" , value = "false" },
new ListData{ label = "Problemi psichici", position = LabelPosition.Before, name = "psichici" , value = "true" },
new ListData{ label = "No", position = LabelPosition.Before, name = "psichici" , value = "false" },
};
public class ListData
{
public string label { get; set; }
public string name { get; set; }
public string value { get; set; }
public LabelPosition position { get; set; }
}
public async Task click(string name)
{
//how can I get the "Name" value where I double clicked on ?
await JSRuntime.InvokeAsync<object>("registerClearCallback", name);
}
} |