1

So, for example I have three select boxes.

<select name="cat-1" id="cat-1">
  <option value="0">A</option>
  <option value="1">B</option
</select>
<select name="cat-2" id="cat-2"> 
  <option value="0">AA</option>
  <option value="1">BB</option
</select>
<select name="cat-3" id="cat-3">
  <option value="0">AAA</option>
  <option value="1">BBB</option
</select>
<input type="submit" value="Add">

If I choose options, for example A BB AAA and press ADD, i want this combination to appear as text under add button as: A BB AAA (X) x - to cancel such combination. I would like to add up to 3 combinations with ability to cancel any combination. After submitting form, I want to pass such combinations values in this case I guess array [0, 1, 0] would be fine. Options values cannot be submitted separately, because cat-2 and cat-3 are populated dynamically using ajax - cat-3 depends on cat-2 and cat-2 depends on cat-1 so many combinations are possible.

I am using select2 (http://select2.github.io/examples.html). It would be perfect If I could return combinations in style similar to 'Multiple select boxes' (see link above).

Any idea where do I start? I have no idea how to implement this the right way with ability in future, for example, on click on combination to set select boxes to combination values and etc.

I guess final input to database will be generated array of arrays like [[0,1,0], [0,0,0], [1,0,3], ...] in hidden field?

Dancia
  • 762
  • 1
  • 18
  • 32
  • Did you try anything? Community will not write entire solution for you – Michał Jun 27 '15 at 22:38
  • I don't need entire solution. I need guidelines. The problem is I don't know how do do this the right way. I saw other unrelated topics, when people encode into hidden field json value then decode it in php, also some two dimensional arrays, but those were on totally unrelated topics. I want to hear some more approaches/general guidelines how could I deal with this problem. – Dancia Jun 27 '15 at 22:45

1 Answers1

0

I have written my own piece of code to handle this problem. Here is working example: http://jsfiddle.net/s2arvdck/13/

<select name="cat_1" id="cat_1">
    <option value="0">I</option>
    <option value="1">II</option>
    <option value="2">III</option>
</select>
<select name="cat_2" id="cat_2">
    <option value="0">A</option>
    <option value="1">B</option>
    <option value="2">C</option>
</select>
<select name="cat_3" id="cat_3">
    <option value="0">a</option>
    <option value="1">b</option>
    <option value="2">c</option>
</select>
<input type="button" id="addButton" value="Add">
<br />Combinations: <span id="combContainer"></span>

<br />Expected hidden: <span id="expected"></span>

$('#addButton').on('click', addButtonClicked);

function addButtonClicked(event) {

    var cat1 = $('#cat_1 option:selected').val();
    var cat2 = $('#cat_2 option:selected').val();
    var cat3 = $('#cat_3 option:selected').val();

    if ($('#combContainer:contains("[' + cat1 + ', ' + cat2 + ', ' + cat3 + ']")').length == 0) {
        $('#combContainer').append('<div><span class="comb">[' + cat1 + ', ' + cat2 + ', ' + cat3 + ']</span> <span id="combDel">remove</span></div>');
    }

    var arr = [];
    $('.comb').each(function (index, elem) {
        arr.push($(this).text());
        $('#expected').text(arr.join(''));
    });

    $("#combContainer").on("click", "#combDel", function () {
        var text = $(this).closest("div").children(".comb").text();
        if ($('#combContainer:contains("[' + text + ']")').length == 0) {
            $(this).closest("div").remove();
            var expectedText = $("#expected").text();
            var newExpectedText = expectedText.replace(text, '');
            $("#expected").text(newExpectedText);
        }
    });

}
Dancia
  • 762
  • 1
  • 18
  • 32