Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create 3 state checkbox button with bootstrap 4

I'm creating a filter form, I want to filter out objects with the property high=true, or high=false or ignore that property.
All I'm getting is the value "on" so far.
Also on the third value (null or whatever) I want the styling to be different, like disabled.
Anyone know an easy way to do this?

function readValue() {
  var highValue = $('#high').val();
  $('#result').html(highValue);
  if (highValue === null) {
    //don't filter
  } else {
    //filter
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<div class="btn-group-toggle" data-toggle="buttons">
  <label onclick="readValue()" class="btn btn-secondary active">
    <input id="high" type="checkbox" checked autocomplete="off"> High
  </label>
</div>
Result:<div id='result'></div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
like image 646
Cold_Class Avatar asked Nov 16 '25 05:11

Cold_Class


2 Answers

I figured out a way of having as many states as wanted to be toggled with one button while the button indicates the state. This is what I was looking for - most of the code is proudly found and put together from different parts of the internet!
If you have a suggestion how to make this even shorter AND easier to read, please comment or directly edit it :)

var $radios = $('input[type="radio"][name="high"]');
$('#result').html($radios.filter(':checked').val());

$('#toggler').click(function() {
  var colorClasses = ["btn-danger", "btn-success", "btn-secondary"];
  var $checked = $radios.filter(':checked');
  var $next = $radios.eq($radios.index($checked) + 1);
  if (!$next.length) {
    $next = $radios.first();
  }
  $next.prop("checked", true);
  var newValue = $radios.filter(':checked').val();
  $(this)
    .removeClass(colorClasses.join(" "))
    .addClass(colorClasses[newValue]);
  $('#result').html(newValue);
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<div class="btn-group-toggle" data-toggle="buttons">
  <input type="radio" name="high" value="2" checked hidden>
  <input type="radio" name="high" value="1" hidden>
  <input type="radio" name="high" value="0" hidden>
  <button type="button" id="toggler" class="btn btn-secondary">High</button>
</div>
Result:
<div id='result'></div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
like image 172
Cold_Class Avatar answered Nov 17 '25 19:11

Cold_Class


Similar question had been asked and some people suggested to use :indeterminate pseudo class to represent the NULL state - where that filter property is ignored.

What is :indeterminate?

Bootstrap 4 custom checkboxes utilize the :indeterminate pseudo class only when manually set via JavaScript: http://getbootstrap.com/docs/4.1/components/forms/#custom-forms

:indeterminate is pretty new and not all browsers render it correctly so I would just go for simpler approaches:

  • A checkbox next to the filter property to represent whether we need to filter by that property or not
  • A checkbox or radio button to present true/yes or false/no state.

On page load

The filter is unselected, and its available values are hidden.

enter image description here

Filter is selected/engaged

If the filter is selected, its available values are shown.

enter image description here

On submit

On form submit, you can always pass 2 values in:

  • Whether the filter property is engaged or not, i.e., $().is(":checked")
  • The filter property value, i.e., yes/no

Fiddle: http://jsfiddle.net/aq9Laaew/72593/

jQuery plugin

A plugin for null-able checkbox: http://vanderlee.github.io/tristate/

like image 35
David Liang Avatar answered Nov 17 '25 18:11

David Liang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!