Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HELP] Toggle Not Rendering, Shows Checkbox #155

Closed
Major-Mike opened this issue Jan 5, 2024 · 5 comments
Closed

[HELP] Toggle Not Rendering, Shows Checkbox #155

Major-Mike opened this issue Jan 5, 2024 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@Major-Mike
Copy link

I am not sure if this is a bug or not, so I am asking for help to confirm.

I am on ASP.net Core 6 Razor Pages, Bootstrap v5.3.2, latest release of BS5-Toggle. Following the exact instructions provided on https://palcarazm.github.io/bootstrap5-toggle/#usage-1 resulting in this basic markup:

<div class="col-6">
	<input type="checkbox" id="isLockedOut" data-toggle="toggle" data-size="lg" data-onstyle="danger" data-onlabel="Locked Out" data-offlabel="Unlocked" data-style="quick" data-onvalue="true" data-offvalue="false" aria-required="true" @(Model.IsLockedOut ? "checked" : "") />
</div>

and the result is this:
image

can anyone see a problem or suggest a solution? thank you.

@Major-Mike Major-Mike added the help wanted Extra attention is needed label Jan 5, 2024
Copy link

github-actions bot commented Jan 5, 2024

Hi! 👋
Thanks for your issue. You are helping to improve Bootstrap 5 toggle.

@SnakeME
Copy link

SnakeME commented Jan 15, 2024

Same issue here, not found a solution yet...

@palcarazm
Copy link
Owner

Hi,

I'm not in asp.net, I can't help you.

The issue is the js is been executed when the checkbox haven't been already render by asp.net.

I don't know if we can schedule de js to be launch when asp.net had finished.

@Major-Mike
Copy link
Author

@palcarazm ASP.net is nothing special in that regard, CSS and scripts are loaded client side and is simple HTML, only the event management server side is what can be "in limbo". Unfortunately I had to meet a deadline and decided to just use the default toggle that is in Bootstrap 5. But I'd like to use this in the future so a solution would be appreciated.

Also, to add, I did a very very stripped down code for the toggle and it worked, but in the full context of Bootstrap added, it fails. I didn't think they would be relevant to each other, perhaps a CSS is overriding it? Missing an !important somewhere maybe? Not sure, just thinking out loud.

@palcarazm
Copy link
Owner

Hi @Major-Mike ans @SnakeME ,

It seems that asp.net throws the "loaded" event before all the components or sections have actually been loaded.

This assumes that the autorender included in the library is triggered prematurely (when your checkboxes are not yet in the DOM).

The solutions I can think of involve rendering by hand.

Here a simulation : https://codepen.io/palcarazm/pen/poBqzwj

Either by giving your checkbox the attributes in the HTML and launching the manual rendering from the same file where your checkbox is generated:

<div class="col-6">
	<input type="checkbox" id="my-unique-id" data-toggle="toggle" data-size="lg" data-onstyle="danger" data-onlabel="Locked Out" data-offlabel="Unlocked" data-style="quick" data-onvalue="true" data-offvalue="false" aria-required="true" @(Model.IsLockedOut ? "checked" : "") />
</div>
<script>
	 document.getElementById("my-unique-id").bootstrapToggle();.
</script>

Either by directly generating the element from JS in the file where the toggle should be:

<div class="col-6">
	<input type="checkbox" id="my-unique-id" aria-required="true" @(Model.IsLockedOut ? "checked" : "") />
</div>
<script>
	  document.getElementById("my-unique-id").bootstrapToggle({
    	      onlabel: "Locked Out",
	      offlabel: "Unlocked",
 	      onstyle: "danger",
	      onvalue: "true",
	      offvalue: "false",
	      size: "lg",
	      style: "quick"
	    });
</script>

Sorry for the late solution. I had to attend to other projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
Archived in project
Development

No branches or pull requests

3 participants