3

There is a related question with great response, but the approach and my actual implementation requires different strategy.

Please view the demo sample I have done as my homework: http://jsfiddle.net/k8fNb/

I have two home works:

  1. Make select options hidden and visible based on data-filter: TODO
  2. Make the select option selected based on radio value: DONE

When you click a radio, the select value should be the same as radio value (#2), and done, but this should also filter the visibility of select options based on radios' data-filter. Data filter ["a","b","c"] on #filters should make the options with values a, b, c visible, and hide the rest. My issue is how to do with #1.

HTML:

<div id="filters">
  <input type="radio" name="filter" value="a" data-filter="["a","b","c"]"/><label>a </label>
  <input type="radio" name="filter" value="b" data-filter="["a","b","c"]"/><label>b </label>
  <input type="radio" name="filter" value="c" data-filter="["a","b","c"]"/><label>c </label>
  <input type="radio" name="filter" value="1" data-filter="["1","2","3"]"/><label>1 </label>
  <input type="radio" name="filter" value="2" data-filter="["1","2","3"]"/><label>2 </label>
  <input type="radio" name="filter" value="3" data-filter="["1","2","3"]"/><label>3 </label>
</div>

<select id="options">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>  
</select>

jQuery:

$(document).ready(function(){

  $('#filters input').each(function () {
    var v = $(this).val();
    $(this).click(function () {
      $('#options option').filter(function(){
        // 1. Make select options hidden or visible based on data-filter.
        // ? TODO ....

        // 2. FIXED. Change select value to use radio value
        return $(this).val() == v;
      }).prop("selected", true);
    });
  });
});

Any help is much appreciated.

Community
  • 1
  • 1
swan
  • 2,509
  • 3
  • 24
  • 40

1 Answers1

2

If you put "["a","b","c"]" into data-filter, it will be considered a string rather than an array.

How about approaching it like this?

HTML:

<div id="filters">
  <input type="radio" name="filter" value="a"/><label>a</label>
  <input type="radio" name="filter" value="b"/><label>b</label>
  <input type="radio" name="filter" value="c"/><label>c</label>
  <input type="radio" name="filter" value="1"/><label>1</label>
  <input type="radio" name="filter" value="2"/><label>2</label>
  <input type="radio" name="filter" value="3"/><label>3</label>
</div>

<select id="options">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>  
</select>

JAVASCRIPT:

$(function () {
    var arrays = [
        ['a', 'b', 'c'],
        ['1', '2', '3']
    ];

  $('#filters input').change(function () {
      var value = $(this).val();
      var activeArr = [];
      var index;
      for(var i = 0; i < arrays.length; i++){
          index = $.inArray(value, arrays[i]);
          if(index > -1){
              activeArr = arrays[i];
              break;
          }
      }

      $('#options').empty(); //clear options
      $.each(activeArr, function(i, e){
         $('#options').append('<option value="' + e + '">' + e + '</option>');
      });

      $('#options option').eq(index).prop('selected', true);  
  });
});

DEMO: http://jsfiddle.net/dirtyd77/JrY9R/3/


However, you could also create a new array using .split() and removing the brackets.

HTML:

<div id="filters">
  <input type="radio" name="filter" value="a" data-filter="a,b,c"/><label>a</label>
  <input type="radio" name="filter" value="b" data-filter="a,b,c"/><label>b</label>
  <input type="radio" name="filter" value="c" data-filter="a,b,c"/><label>c</label>
  <input type="radio" name="filter" value="1" data-filter="1,2,3"/><label>1</label>
  <input type="radio" name="filter" value="2" data-filter="1,2,3"/><label>2</label>
  <input type="radio" name="filter" value="3" data-filter="1,2,3"/><label>3</label>
</div>

<select id="options">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>  
</select>

JAVASCRIPT:

$(function () {
  $('#filters input').change(function () {
      var value = $(this).val();
      var activeArr = $(this).data('filter').split(',');
      var index;
      index = $.inArray(value, activeArr);
      $('#options').empty(); //clear options

      $.each(activeArr, function(i, e){
         $('#options').append('<option value="' + e + '">' + e + '</option>');
      });
      $('#options option').eq(index).prop('selected', true);  
  });
});

DEMO: http://jsfiddle.net/dirtyd77/k8fNb/2/

Dom
  • 38,906
  • 12
  • 52
  • 81
  • Thanks a lot. This is more complicated than I thought, however the last option gives me much freedom server side. I really appreciate it. – swan Jul 11 '13 at 10:39