Bootstrap 5 accordion with an inline checkbox? - html

I'm listing a bunch of programs that each have a bunch of files. Each program title is in an accordion header, which you can click to open the list of files.
I want to have a checkbox next to the program title, so you can select/unselect all files listed for each program.
The issue I'm running into is that I can't put the input inside of the button. See this: Bootstrap 5 collapse with Checkbox, stopPropagation() not working
And when I try putting the input outside of the button it won't stay on the same line as the accordion header. I've tried using d-inline and d-inline-block but couldn't get it to work.
Here is my HTML:
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<div class="accordion-header" id="heading6">
<input type="checkbox" class="form-check-input program_checkbox" id="program6" value="6" checked>
<a class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse6" aria-expanded="true" aria-controls="collapse6">Maine Ecological Reserves Program</a>
</div>
</div>
<div id="collapse6" style="margin-left:24px;" class="accordion-collapse collapse show ml-4" aria-labelledby="heading6" data-bs-parent="#accordionExample">
<div class="form-group form-check pl-4">
<input type="checkbox" class="form-check-input file_checkbox program6" id="package243" value="program_data/6/packages/1646756076.zip" checked="">
<label class="form-check-label" for="package243">1646756076.zip</label>
</div>
<div class="form-group form-check pl-4">
<input type="checkbox" class="form-check-input file_checkbox program6" id="scriptundefined" value="program_data/6/scripts/MEER_munger.py" checked="">
<label class="form-check-label" for="scriptundefined">MEER_munger.py</label>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" id="heading9">
<input type="checkbox" class="form-check-input program_checkbox" id="program9" value="9">
<a class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse9" aria-expanded="true" aria-controls="collapse9">Vermont State Lands Continuous Forest Inventory</a>
</div>
</div>
<div id="collapse9" style="margin-left:24px;" class="accordion-collapse collapse show ml-4" aria-labelledby="heading9" data-bs-parent="#accordionExample">No files for this project
</div>
</div>

To align items in one row you can use flex. Take a look at CSS section of Code Snippet.
.accordion--custom .accordion-header {
display: flex; /* make flex element */
align-items: center; /* aligning child items */
column-gap: 1rem; /* adding gap between items in row */
padding-left: 1rem;
}
/* small udjustments */
.accordion--custom .accordion-header .accordion-button {
padding-left: 0;
background: none;
}
.accordion--custom .accordion-button:not(.collapsed) {
box-shadow: none;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<!-- "accordion--custom" - custom class for updating default accordion, like example -->
<div class="accordion accordion--custom" id="accordionExample">
<div class="accordion-item">
<div class="accordion-header" id="heading6">
<input type="checkbox" class="form-check-input program_checkbox" id="program6" value="6" checked>
<a class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse6" aria-expanded="true" aria-controls="collapse6">Maine Ecological Reserves Program</a>
</div>
</div>
<div id="collapse6" style="margin-left:24px;" class="accordion-collapse collapse show ml-4" aria-labelledby="heading6" data-bs-parent="#accordionExample">
<div class="form-group form-check pl-4">
<input type="checkbox" class="form-check-input file_checkbox program6" id="package243" value="program_data/6/packages/1646756076.zip" checked="">
<label class="form-check-label" for="package243">1646756076.zip</label>
</div>
<div class="form-group form-check pl-4">
<input type="checkbox" class="form-check-input file_checkbox program6" id="scriptundefined" value="program_data/6/scripts/MEER_munger.py" checked="">
<label class="form-check-label" for="scriptundefined">MEER_munger.py</label>
</div>
</div>
<div class="accordion-item">
<div class="accordion-header" id="heading9">
<input type="checkbox" class="form-check-input program_checkbox" id="program9" value="9">
<a class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse9" aria-expanded="true" aria-controls="collapse9">Vermont State Lands Continuous Forest Inventory</a>
</div>
</div>
<div id="collapse9" style="margin-left:24px;" class="accordion-collapse collapse show ml-4" aria-labelledby="heading9" data-bs-parent="#accordionExample">No files for this project
</div>
</div>

Related

Hide DIV when both switches are not checked

I have two collapsible switch buttons that each one collide on/off one checkbox in my form. Above the checkboxes I have a little paragraph, and I want this paragraph to hide (collide) only when both checkboxes are hidden (or main switches are off), and I don't know how to make it work. I'm developing this frontend with Bootstrap.
<label class="switch mt-2">
<input class="form-check-input" type="checkbox" role="switch" id="ds-activate" data-bs-toggle='collapse' data-bs-target='#dsForm' aria-expanded="false" aria-controls="dsForm" checked>
<div class="slider round">
</div>
</label>
<label class="switch mt-2">
<input class="fo rm-check-input" type="checkbox" role="switch" id="email-activate" data-bs-toggle='collapse' data-bs-target='#emailForm' aria-expanded="false">
<div class="slider round">
</div>
</label>
<div id="hideWhenBothSwitchOff" aria-expanded="false" class="collapse show">
<label class="form-label">MORE OPTIONSs</label>
<p>Explanation of the next options.</p>
</div>
<div id="dsForm" aria-expanded="false" class="collapse show">
<div class="form-check mt-2 mb-2">
<input class="form-check-input" type="checkbox" value="" id="all-documents">
<label class="form-check-label" for="allDocuments">
Checkbox 1 label
</label>
</div>
</div>
<div id="emailForm" aria-expanded="false" class="collapse">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="all-emails">
<label class="form-check-label" for="allEmails">
Checkbox 2 label
</label>
</div>
</div>
We can write a little JavaScript based on Bootstrap's Collapse component to achieve your goal.
// Create a collapsible from the element that should
// be shown/collapsed depending on two checkboxes.
const collapsible = new bootstrap.Collapse('#hideWhenBothSwitchOff', {
toggle: false,
});
// The checkboxes to check.
const checkboxes = [
document.getElementById('ds-activate'),
document.getElementById('email-activate'),
];
// Attach event listener to each checkbox, so we know when they
// are checked or unchecked.
checkboxes.forEach((checkbox) => {
checkbox.addEventListener('change', () => {
// If at least one checkbox is checked, show the collapsible,
// otherwise hide it.
const operation = checkboxes.some(({ checked }) => checked)
? 'show'
: 'hide';
collapsible[operation]();
});
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-A3rJD856KowSb7dwlZdYEkO39Gagi7vIsF0jrRAoQmDKKtQBHUuLZ9AsSv4jD4Xa" crossorigin="anonymous"></script>
<label class="switch mt-2">
<input class="form-check-input" type="checkbox" role="switch" id="ds-activate" data-bs-toggle='collapse' data-bs-target='#dsForm' aria-expanded="false" aria-controls="dsForm" checked>
<div class="slider round">
</div>
</label>
<label class="switch mt-2">
<input class="fo rm-check-input" type="checkbox" role="switch" id="email-activate" data-bs-toggle='collapse' data-bs-target='#emailForm' aria-expanded="false">
<div class="slider round">
</div>
</label>
<div id="hideWhenBothSwitchOff" aria-expanded="false" class="collapse show">
<label class="form-label">MORE OPTIONSs</label>
<p>Explanation of the next options.</p>
</div>
<div id="dsForm" aria-expanded="false" class="collapse show">
<div class="form-check mt-2 mb-2">
<input class="form-check-input" type="checkbox" value="" id="all-documents">
<label class="form-check-label" for="allDocuments">
Checkbox 1 label
</label>
</div>
</div>
<div id="emailForm" aria-expanded="false" class="collapse">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="all-emails">
<label class="form-check-label" for="allEmails">
Checkbox 2 label
</label>
</div>
</div>

Select radio button when clicked outside input

I'm using a standard Bootstrap 4 accordion with a radio button inside of it. Here is my code:
<div class="card">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#tm-stripe-div" aria-expanded="true" aria-controls="tm-stripe-div">
<div class="d-flex justify-content-between">
<div class="stripe-payment-div">
<input class="form-check-input" type="radio" name="paymentRadio" id="stripePayment" value="">
<label class="form-check-label" for="stripePayment"> Credit Card (Stripe) </label>
</div>
</div>
</button>
</h2>
</div>
</div
When i click outside the input field the radio button is not selected. I know the best way would be to use jquery, but is there a way to do it with css only?
I'd like the radio button selected when the use clicks anywhere inside the .card class.
Thanks
I'd recommend using JavaScript but if you really want to do it without, you can use the trick that a lot of custom form controls use.
Basically wrap it in a label and use the for attribute to point to your radio.
Had to also convert your button to an a because the clicks events weren't registering.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="card">
<label for="stripePayment" class="mb-0">
<div class="card-header" id="headingOne">
<h2 class="mb-0">
<a class="btn btn-link btn-block text-left" data-toggle="collapse" data-target="#tm-stripe-div" aria-expanded="true" aria-controls="tm-stripe-div">
<div class="d-flex justify-content-between">
<div class="stripe-payment-div">
<input class="form-check-input" type="radio" name="paymentRadio" id="stripePayment" value="">
<span class="form-check-label" for="stripePayment"> Credit Card (Stripe) </span>
</div>
</div>
</a>
</h2>
</div>
</label>
</div>

Vertical center align for a button in panel header with Bootstrap

I'm working on a Bootstrap 4 HTML page. I put a button in a panel header:
I need to put the button (and the title) in the middle of the panel header. How can I achieve this goal?
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container p-4 ">
<div class="card">
<div class="card-header">
<p class="float-left display-4">Administrator</p>
<a href="#" class="btn btn-primary float-right" style="align-items: center" routerLink="../list" role="button">
<i class="fa fa-arrow-left"></i> Indietro</a>
</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="form-group">
<label for="displayName">Display name:</label>
<input type="displayName" class="form-control" id="displayName">
</div>
<button type="submit" class="btn btn-primary float-right">Salva</button>
</form>
</div>
</div>
</div>
Thanks
Try using display: flex; for your card header which is available in bootstrap and also instead of using float-right you can use ml-auto to move the button to the right (With the use of flex there is no need to use float properties). Hope this code helps
<!--
Bootstrap docs: https://getbootstrap.com/docs
-->
<div class="container p-4 ">
<div class="card">
<div class="card-header d-flex flex-row align-items-center">
<p class="float-left display-4">Administrator</p>
<a href="#" class="btn btn-primary ml-auto" style="align-items: center" routerLink="../list" role="button">
<i class="fa fa-arrow-left"></i> Indietro</a></div>
<div class="card-body">
<form>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
<div class="form-group">
<label for="displayName">Display name:</label>
<input type="displayName" class="form-control" id="displayName">
</div>
<button type="submit" class="btn btn-primary float-right">Salva</button>
</form>
</div>
</div>
</div>
Just add display flex to your card-header class, then align-items: center
.card-header {
padding: .75rem 1.25rem;
margin-bottom: 0;
background-color: rgba(0,0,0,.03);
border-bottom: 1px solid rgba(0,0,0,.125);
display: flex;
justify-content: space-between;
align-items: center;
}

Border appears on hover

I have a list group in a card. When you hover over the topmost list group item, a border appears under it (no border when not hovering). When I set :hover override in Google Chrome's Dev Tools, the border doesn't appear. Why does this happen?
https://jsfiddle.net/williamqin123/0fkwsemn/
<div class="container">
<div class="shadow-sm card my-5">
<div class="list-group list-group-flush">
<div class="text-center list-group-item">
<h1 class="text-left d-inline-block mb-0">
Submit
<small class="text-muted">Create your ad in less than 1 minute</small>
</h1>
</div>
<form action="/submit" method="POST" class="mb-0">
<div class="form-group list-group-item">
<label for="site">Site</label>
<div class="input-group" id="site-menu">
<select class="form-control text-center" name="site" id="site" required>
</select>
<div class="btn-group-toggle input-group-append" data-toggle="buttons">
<label class="btn btn-outline-primary">
<input type="checkbox" name="meta" value='checked'> Meta
</label>
</div>
</div>
</div>
<div class="form-group list-group-item">
<label for="post-id">Post ID</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">#</span>
</div>
<input class="form-control text-center" type="number" min="1" step="1" name="id" id="post-id" required>
</div>
</div>
<div class="form-group text-center list-group-item">
<input class="btn btn-primary btn-lg" type="submit" value="Proceed">
</div>
</form>
</div>
</div>
</div>
There is a border on the element:
.list-group-item {
position: relative;
margin-bottom: -1px;
border: 1px solid rgba(0,0,0,.125);
}
You can't see it initially because the negative margin makes the next element shift upwards (by 1px) and overlap it.
This code...
.list-group-item:hover {
z-index: 1;
}
...adds a new stacking context, making it appear in front, and allows you to see the border.

show a small inline delete button upon button press in ng-bootstrap

My Current implementation of what I want is done using ng-bootstrap for Angular4.
Image
As you can see I used the Check box button to display a response from the backend. If you click on the button the x button will be not be shown and it will also hide some other html elements below.
Upon clicked (checked)
I am actually looking to improve the layout, where I wish that the x button would somehow be spanned together for a smooth feeling.
I do not know of any components from Bootstrap4.x or ng-bootstrap itself that could do something.
Any design suggestions or improvements is what I am looking for.
code
<!-- Search Keywords go here -->
<div class="container" *ngIf="Output.length">
<div class="row">
<div class="col-sm-2">
<label class="col-form-label-lg"><b> Search History </b></label>
</div>
<div class="col-sm-10">
<div class="btn-group btn-group-toggle" *ngFor="let keyword of Output; let cbIndex = index">
<label class="btn-primary" ngbButtonLabel>
<input type="checkbox"
ngbButton
#kwCheck
(change)="cbInput= kwCheck.checked; hideKW(cbIndex)"
> {{keyword.kw}}
</label>
<span><button class="btn btn-xs btn-danger"
(click)="deleteKW(keyword.kw); $event.stopPropagation()"
[disabled]=kwCheck.checked
[hidden]="kwCheck.checked">×</button>
</span>
</div>
</div>
</div>
</div>
Output.length is an array of JSON responses from the backend.
There are, at least, two ways that you can do so.
Use btn-group. It is pretty well semantic also, except that you use btn class for the input container which is a not button.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<div class="btn-group" role="group" aria-label="Basic example">
<label class="btn d-flex align-items-center bg-primary px-3 mb-0">
<input type="checkbox" class="" id="customCheck1">
<span class="px-1 mb-0" for="customCheck1">Check</span>
</label>
<button type="button" class="btn btn-danger">×</button>
</div>
Use input-group. I prefer this approach because all of the classes have meaning.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<div class="input-group w-auto">
<label class="d-flex align-items-center w-auto bg-primary px-3 mb-0">
<input type="checkbox" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
<span class="pl-2">Check</span>
</label>
<div class="input-group-prepend">
<button class="btn btn-danger" type="button">×</button>
</div>
</div>
Update
In order to hide the checkbox's tick mark, use btn-group-toggle. It is the same method that bootstrap does.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />
<div class="input-group w-auto ">
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-primary" style="border-radius: 0;">
<input type="checkbox" autocomplete="off"> Check
</label>
</div>
<div class="input-group-prepend">
<button class="btn-danger btn" id="basic-addon1">×</button>
</div>
</div>
As far as i know, there is no way to reset the border to zero. So, you have to use custom CSS.*
https://codepen.io/anon/pen/qKOdJv?editors=1000