Proper way to bind to checkboxes for many options
I have a page for options. To keep it simple I've cut it down to checkboxes which enables/disables fruits. One can select between several fruits and the number of fruits can vary over time. Also one other options, which quick filters fruits, exists. The settings are stored to and retrieved from LocalStorage with help of https://github.com/cloudcrate/BlazorStorage
As I understand, bind can only bind to a variable or property so I can't do:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxChanged(fruit.Name)>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
This wont work either since all checkboxes will toggle the same property:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxEnabled>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
What I ended up doing was creating a component which has properties Name and Enabled and an event which indicate state change:
@{foreach (var fruit in AllFruits) {
<Fruit Name=@fruit.Name
Enabled=@fruit.Enabled
OnSettingsChanged=@((Action<string, bool>) FruitClicked)></Fruit>
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
I also have a button which saves the current settings and an handler to the event OnSettingsChanged:
<BlazorButton Color="Color.Primary" OnClick=@SaveFruitSettings>Save Changes</BlazorButton>
protected void FruitClicked(string name, bool enabled) {
_fruits.FirstOrDefault(b => b.Name == name).Enabled = enabled;
}
The single checkbox disabling/enabling yellow fruits is used often and should give effect directly. So the Checked property accesses local storage in get/set. The individual fruits are not changed often and should only be saved to local storage after save is pressed.
This setup worked fine in Blaxor 0.5.0 but after upgrade to 0.6.0 it doesn't. When the yellow fruits checkbox is clicked all the individual fruits OnSettingsChanged are fired after being set with the saved values from LocalStorage. The foreach loop accessing AllFruits is run indicating that the whole page is rerendered or similar.
I feel that I'm missing the big picture on how to do this. How do I show checkboxes for a number of options without creating individual bool properties for each?
c# blazor
add a comment |
I have a page for options. To keep it simple I've cut it down to checkboxes which enables/disables fruits. One can select between several fruits and the number of fruits can vary over time. Also one other options, which quick filters fruits, exists. The settings are stored to and retrieved from LocalStorage with help of https://github.com/cloudcrate/BlazorStorage
As I understand, bind can only bind to a variable or property so I can't do:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxChanged(fruit.Name)>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
This wont work either since all checkboxes will toggle the same property:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxEnabled>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
What I ended up doing was creating a component which has properties Name and Enabled and an event which indicate state change:
@{foreach (var fruit in AllFruits) {
<Fruit Name=@fruit.Name
Enabled=@fruit.Enabled
OnSettingsChanged=@((Action<string, bool>) FruitClicked)></Fruit>
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
I also have a button which saves the current settings and an handler to the event OnSettingsChanged:
<BlazorButton Color="Color.Primary" OnClick=@SaveFruitSettings>Save Changes</BlazorButton>
protected void FruitClicked(string name, bool enabled) {
_fruits.FirstOrDefault(b => b.Name == name).Enabled = enabled;
}
The single checkbox disabling/enabling yellow fruits is used often and should give effect directly. So the Checked property accesses local storage in get/set. The individual fruits are not changed often and should only be saved to local storage after save is pressed.
This setup worked fine in Blaxor 0.5.0 but after upgrade to 0.6.0 it doesn't. When the yellow fruits checkbox is clicked all the individual fruits OnSettingsChanged are fired after being set with the saved values from LocalStorage. The foreach loop accessing AllFruits is run indicating that the whole page is rerendered or similar.
I feel that I'm missing the big picture on how to do this. How do I show checkboxes for a number of options without creating individual bool properties for each?
c# blazor
Maybe you are running into this bug: github.com/aspnet/Blazor/issues/1490 and need to wait for the 0.7 release?
– Flores
Nov 15 '18 at 8:37
add a comment |
I have a page for options. To keep it simple I've cut it down to checkboxes which enables/disables fruits. One can select between several fruits and the number of fruits can vary over time. Also one other options, which quick filters fruits, exists. The settings are stored to and retrieved from LocalStorage with help of https://github.com/cloudcrate/BlazorStorage
As I understand, bind can only bind to a variable or property so I can't do:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxChanged(fruit.Name)>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
This wont work either since all checkboxes will toggle the same property:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxEnabled>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
What I ended up doing was creating a component which has properties Name and Enabled and an event which indicate state change:
@{foreach (var fruit in AllFruits) {
<Fruit Name=@fruit.Name
Enabled=@fruit.Enabled
OnSettingsChanged=@((Action<string, bool>) FruitClicked)></Fruit>
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
I also have a button which saves the current settings and an handler to the event OnSettingsChanged:
<BlazorButton Color="Color.Primary" OnClick=@SaveFruitSettings>Save Changes</BlazorButton>
protected void FruitClicked(string name, bool enabled) {
_fruits.FirstOrDefault(b => b.Name == name).Enabled = enabled;
}
The single checkbox disabling/enabling yellow fruits is used often and should give effect directly. So the Checked property accesses local storage in get/set. The individual fruits are not changed often and should only be saved to local storage after save is pressed.
This setup worked fine in Blaxor 0.5.0 but after upgrade to 0.6.0 it doesn't. When the yellow fruits checkbox is clicked all the individual fruits OnSettingsChanged are fired after being set with the saved values from LocalStorage. The foreach loop accessing AllFruits is run indicating that the whole page is rerendered or similar.
I feel that I'm missing the big picture on how to do this. How do I show checkboxes for a number of options without creating individual bool properties for each?
c# blazor
I have a page for options. To keep it simple I've cut it down to checkboxes which enables/disables fruits. One can select between several fruits and the number of fruits can vary over time. Also one other options, which quick filters fruits, exists. The settings are stored to and retrieved from LocalStorage with help of https://github.com/cloudcrate/BlazorStorage
As I understand, bind can only bind to a variable or property so I can't do:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxChanged(fruit.Name)>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
This wont work either since all checkboxes will toggle the same property:
@{foreach (var fruit in AllFruits) {
<input type="checkbox" bind=@CheckBoxEnabled>@fruit.Name
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
What I ended up doing was creating a component which has properties Name and Enabled and an event which indicate state change:
@{foreach (var fruit in AllFruits) {
<Fruit Name=@fruit.Name
Enabled=@fruit.Enabled
OnSettingsChanged=@((Action<string, bool>) FruitClicked)></Fruit>
}
}
<input type="checkbox" bind=@Checked />Show all yellow fruits
I also have a button which saves the current settings and an handler to the event OnSettingsChanged:
<BlazorButton Color="Color.Primary" OnClick=@SaveFruitSettings>Save Changes</BlazorButton>
protected void FruitClicked(string name, bool enabled) {
_fruits.FirstOrDefault(b => b.Name == name).Enabled = enabled;
}
The single checkbox disabling/enabling yellow fruits is used often and should give effect directly. So the Checked property accesses local storage in get/set. The individual fruits are not changed often and should only be saved to local storage after save is pressed.
This setup worked fine in Blaxor 0.5.0 but after upgrade to 0.6.0 it doesn't. When the yellow fruits checkbox is clicked all the individual fruits OnSettingsChanged are fired after being set with the saved values from LocalStorage. The foreach loop accessing AllFruits is run indicating that the whole page is rerendered or similar.
I feel that I'm missing the big picture on how to do this. How do I show checkboxes for a number of options without creating individual bool properties for each?
c# blazor
c# blazor
asked Nov 15 '18 at 7:32
Lars CardonLars Cardon
62
62
Maybe you are running into this bug: github.com/aspnet/Blazor/issues/1490 and need to wait for the 0.7 release?
– Flores
Nov 15 '18 at 8:37
add a comment |
Maybe you are running into this bug: github.com/aspnet/Blazor/issues/1490 and need to wait for the 0.7 release?
– Flores
Nov 15 '18 at 8:37
Maybe you are running into this bug: github.com/aspnet/Blazor/issues/1490 and need to wait for the 0.7 release?
– Flores
Nov 15 '18 at 8:37
Maybe you are running into this bug: github.com/aspnet/Blazor/issues/1490 and need to wait for the 0.7 release?
– Flores
Nov 15 '18 at 8:37
add a comment |
1 Answer
1
active
oldest
votes
I've hardly read a couple of lines in your code, as I was in a hurry. Hope my answer may have
some value for you...
@{foreach (var fruit in AllFruits) {
// This must be done... otherwise all your checkboxes will contain the same value
var fruitName = fruit.Name;
// Trigger an event handler for the click event passing the name of the fruit to the event handler
// You can pass the checked property instead.
// Note: If you don't set the value property, it should contain the values on or off,
// at least when form data is passed to the server.
// Additionally you may **bind** to the **value** property
<input type="checkbox" id="@fruitName" name="MyFruits" value="@fruitName" onclick=@(() => CheckBoxChanged(fruitName))>
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314423%2fproper-way-to-bind-to-checkboxes-for-many-options%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've hardly read a couple of lines in your code, as I was in a hurry. Hope my answer may have
some value for you...
@{foreach (var fruit in AllFruits) {
// This must be done... otherwise all your checkboxes will contain the same value
var fruitName = fruit.Name;
// Trigger an event handler for the click event passing the name of the fruit to the event handler
// You can pass the checked property instead.
// Note: If you don't set the value property, it should contain the values on or off,
// at least when form data is passed to the server.
// Additionally you may **bind** to the **value** property
<input type="checkbox" id="@fruitName" name="MyFruits" value="@fruitName" onclick=@(() => CheckBoxChanged(fruitName))>
}
add a comment |
I've hardly read a couple of lines in your code, as I was in a hurry. Hope my answer may have
some value for you...
@{foreach (var fruit in AllFruits) {
// This must be done... otherwise all your checkboxes will contain the same value
var fruitName = fruit.Name;
// Trigger an event handler for the click event passing the name of the fruit to the event handler
// You can pass the checked property instead.
// Note: If you don't set the value property, it should contain the values on or off,
// at least when form data is passed to the server.
// Additionally you may **bind** to the **value** property
<input type="checkbox" id="@fruitName" name="MyFruits" value="@fruitName" onclick=@(() => CheckBoxChanged(fruitName))>
}
add a comment |
I've hardly read a couple of lines in your code, as I was in a hurry. Hope my answer may have
some value for you...
@{foreach (var fruit in AllFruits) {
// This must be done... otherwise all your checkboxes will contain the same value
var fruitName = fruit.Name;
// Trigger an event handler for the click event passing the name of the fruit to the event handler
// You can pass the checked property instead.
// Note: If you don't set the value property, it should contain the values on or off,
// at least when form data is passed to the server.
// Additionally you may **bind** to the **value** property
<input type="checkbox" id="@fruitName" name="MyFruits" value="@fruitName" onclick=@(() => CheckBoxChanged(fruitName))>
}
I've hardly read a couple of lines in your code, as I was in a hurry. Hope my answer may have
some value for you...
@{foreach (var fruit in AllFruits) {
// This must be done... otherwise all your checkboxes will contain the same value
var fruitName = fruit.Name;
// Trigger an event handler for the click event passing the name of the fruit to the event handler
// You can pass the checked property instead.
// Note: If you don't set the value property, it should contain the values on or off,
// at least when form data is passed to the server.
// Additionally you may **bind** to the **value** property
<input type="checkbox" id="@fruitName" name="MyFruits" value="@fruitName" onclick=@(() => CheckBoxChanged(fruitName))>
}
answered Nov 15 '18 at 9:51
IssacIssac
1,8301312
1,8301312
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314423%2fproper-way-to-bind-to-checkboxes-for-many-options%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe you are running into this bug: github.com/aspnet/Blazor/issues/1490 and need to wait for the 0.7 release?
– Flores
Nov 15 '18 at 8:37