mercredi 7 juin 2017

Organize checkboxes in column according to a parameter JavaScript

I'm working on an addon in Javascript. But I had an ergonomic problem.

I generate checkboxes from an array. So if the array is modified, the checkboxes are modified to. And in the following example, if my array choices[] becomes heavy, I will have a big stack of option and this would not be very nice.

So I imagined to organize the checkboxes into columns, and the number of columns will be set in an array columnNumber[0] = x . So I would like to get Checkboxes in center of their column and X checkboxes by row.

Here is my code: please update it in your answer to let me see how you proceed.

function All() {
  $("input").parent().show();
}

function OnlySelecteds() {
  $("input").parent().show();
  $("input:not(:checked)").parent('label').hide();
}


//array of options
var choices = new Array();
choices[0] = "January";
choices[1] = "February";
choices[2] = "March";
choices[3] = "April";
choices[4] = "May";
choices[5] = "June";
choices[6] = "July";
choices[7] = "August";
choices[8] = "September";
choices[9] = "October";
choices[10] = "November";
choices[11] = "December";

//array of options which has to be auto-selected
var target = new Array();
target[0] = "9";
target[1] = "8";
target[2] = "11";

//array which contains the number of column
var numberOfColumn = new Array();
numberOfColumn[0] = 3;
var columnNumber = numberOfColumn[0];

var cbh = document.getElementById('checkboxes');
var val = '';
var cap = "";

var j = "";
var t = document.getElementById('t');

// the loop is creating the checkboxes with name, value...
for (var i in choices) {
  //Name of checkboxes are their number so I convert the i into a string. 
  j = i.toString();

  val = j;
  //cap will be the value/text of choices[i]
  var cb = document.createElement('input');
  var label = document.createElement("label");

  cap = choices[i];
  var text = document.createTextNode(cap);
  cb.type = 'checkbox';
  cbh.appendChild(cb);
  cb.name = cap;
  cb.value = val;
  label.appendChild(cb);
  label.appendChild(text);
  cbh.appendChild(label);
  cb.addEventListener('click', updateText)
  if (target.indexOf(i) >= 0) {
    cb.checked = true;
  }

}
updateText();

function updateText() {
  t.value = [null, ...document.querySelectorAll('#checkboxes [type="checkbox"]')].reduce((s, el) => el && el.checked ? s = (s || '') + el.value + '$#' : s || '')
}


function updateCheckboxes(x) {
  if ($('#SearchBar').val() == '') {
    $('#checkboxes > label').show();
  } else {
    $('#checkboxes > label').hide();
    $('#checkboxes > label:contains(' + $('#SearchBar').val() + ')').show();
  }
}
* {
  box-sizing: border-box;
}

#data {
  padding: 5px;
  width: 100vw;
}

.multiselect {
  overflow: visible;
  padding: 0;
  padding-left: 1px;
  border: none;
  width: 100vw;
  white-space: normal;
  height: 75px;
}

.checkboxes {
  height: 100px;
  width: 100px;
  border: 1px solid #000;
  background-color: white;
  margin-left: -1px;
  display: inline-block;
}

label {
  display: inline-block;
  border: 1px grey solid;
  padding: 5px;
}
<span onclick="All()">All</span> | <span onclick="OnlySelecteds()">Selected</span> | <input id="SearchBar" placeholder="Search for options.." type="text" oninput="updateCheckboxes(this)" autocomplete="off">
<form>
  <div id="data">
    <div class="multiselect">
      <div id="c_b">
        <div id="checkboxes">
        </div>
      </div>
    </div>
  </div>
</form>

<textarea id="t" style="display: none;"></textarea>



Aucun commentaire:

Enregistrer un commentaire