html - Drill-down select options using JavaScript - Stack Overflow

I am starting to build "drill-down" select elements (select elements that filter their option

I am starting to build "drill-down" select elements (select elements that filter their options based on previous options you have selected.

Being that I have never done this before, I am looking for a "best-practice" approach to this mon situation. Could you point me to a tutorial, or provide some example code to how I should approach this?

Solution

I originally thought of hiding and showing options, but it turns out that approach is not cross-browser patible. The easiest, cross-browser method I have e across is creating a copy of the original select options and replacing the options once the user has made a selection. I wrote a little jQuery plugin that makes it a bit more reusable.

<select id="first">
    <option value="1">Fruits</option>
    <option value="2">Vegetables</option>

</select>
<select id="second">
    <option> -- Select -- </option>
    <!-- class is in the format of "parentId_parentValue" -->
    <option class="first_1" value="1">Apple</option>
    <option class="first_1" value="2">Orange</option>
    <option class="first_1" value="3">Banana</option>
    <option class="first_2" value="4">Carrot</option>
    <option class="first_2" value="5">Broccoli</option>
    <option class="first_2" value="6">Spinach</option>
</select>

<script type="text/javascript">
$.fn.drilldown = function(child) {
    var $parent = this;
    var parentId = $parent.attr('id');
    var $child = $(child);
    var childId = $child.attr('id');
    var optionIdentifier = '.' + parentId + '_';
    var selectedChildOption = $child.val();
    var $childCopy = $('<select id='+parentId+childId+' />');
    
    $childCopy.html($child.find('option')).hide().appendTo('body');
    
    var refreshOptions = function(){
        var selectedParentValue = $parent.val();
        $child.html($childCopy.find(optionIdentifier+selectedParentValue).clone());
        $child.prepend('<option value="0" selected="selected"> -- Select -- </option>');
    };
    
    refreshOptions();
    $child.val(selectedChildOption);
    
    $parent.change(function(){
        refreshOptions();
        $child.trigger('change').focus();
    });
};

$(document).ready(function(){
    $('#first').drilldown('#second');
});
</script>

Here is a jsFiddle to show that it works.

I am starting to build "drill-down" select elements (select elements that filter their options based on previous options you have selected.

Being that I have never done this before, I am looking for a "best-practice" approach to this mon situation. Could you point me to a tutorial, or provide some example code to how I should approach this?

Solution

I originally thought of hiding and showing options, but it turns out that approach is not cross-browser patible. The easiest, cross-browser method I have e across is creating a copy of the original select options and replacing the options once the user has made a selection. I wrote a little jQuery plugin that makes it a bit more reusable.

<select id="first">
    <option value="1">Fruits</option>
    <option value="2">Vegetables</option>

</select>
<select id="second">
    <option> -- Select -- </option>
    <!-- class is in the format of "parentId_parentValue" -->
    <option class="first_1" value="1">Apple</option>
    <option class="first_1" value="2">Orange</option>
    <option class="first_1" value="3">Banana</option>
    <option class="first_2" value="4">Carrot</option>
    <option class="first_2" value="5">Broccoli</option>
    <option class="first_2" value="6">Spinach</option>
</select>

<script type="text/javascript">
$.fn.drilldown = function(child) {
    var $parent = this;
    var parentId = $parent.attr('id');
    var $child = $(child);
    var childId = $child.attr('id');
    var optionIdentifier = '.' + parentId + '_';
    var selectedChildOption = $child.val();
    var $childCopy = $('<select id='+parentId+childId+' />');
    
    $childCopy.html($child.find('option')).hide().appendTo('body');
    
    var refreshOptions = function(){
        var selectedParentValue = $parent.val();
        $child.html($childCopy.find(optionIdentifier+selectedParentValue).clone());
        $child.prepend('<option value="0" selected="selected"> -- Select -- </option>');
    };
    
    refreshOptions();
    $child.val(selectedChildOption);
    
    $parent.change(function(){
        refreshOptions();
        $child.trigger('change').focus();
    });
};

$(document).ready(function(){
    $('#first').drilldown('#second');
});
</script>

Here is a jsFiddle to show that it works.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 25, 2011 at 20:33 AndrewAndrew 240k195 gold badges531 silver badges718 bronze badges 1
  • this is rather easy, I could write the code in about 15 minutes, but I believe there's someone here who has it ready and will just paste it here :) but if you don't get an answer in a few hours, I'll write it up! – Frantisek Commented Apr 25, 2011 at 20:40
Add a ment  | 

2 Answers 2

Reset to default 4

If you can use jQuery there is a nice plugin to do so: http://www.ajaxray./Examples/depend.html

it basically uses classes to define the hierarchy e.g. parent select option 1 has value of abc, child select options belonging to parent will have class sub_abc.

Nice and simple! There are many ways of doing this of course, this seems to be a very simple route.

I might do something like this:

$(document).ready(function(){
    var sv = [['apples', 'oranges'],['honda', 'plymouth']];

    $('#category').change(function(){
        $('#subcat option').remove();

        var a = sv[$('#category option:selected').val()];
        for(var n = 0; n < a.length; ++n)
        {
            $('#subcat').append($('<option></option>')
                                .attr('value', n)
                                .text(a[n]));
        }
    });
});

Fiddle here

I would only do that for small data sets however. If you have a massive amount of data that you want to toggle (i.e. dealing with geo data sets), I would most likely fetch the data via .ajax()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745185858a4615636.html

相关推荐

  • html - Drill-down select options using JavaScript - Stack Overflow

    I am starting to build "drill-down" select elements (select elements that filter their option

    4小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信