Welcome Guest, Not a member yet? Register   Sign In
n/a Change values in one list box based on selection in another
#7

[eluser]cideveloper[/eluser]
ok so some modifications have been made but I think you can figure it out after this. What happens is add_new gets all brands but only the categories for brand_id = 1. The javascript looks for a change event and sends a get request to get_cat which gets the categories for the specific id changed to. Its not perfect but works well.

Controller
Code:
function add_new(){
        
        $brand_id = 1;
        $data["title"]="Add Product";
        $data["brandList"]=$this->brand_model->get_data();
        $data["categoryList"]=$this->category_model->get_category($brand_id);
        $data["main_content"]="product/add";
        $this->load->view("ci176981",$data);
    }

    function get_cat($brand_id=null) {
        $data["categoryList"] = $this->category_model->get_category($brand_id)->result();
        echo json_encode($data);
    }
Category_model
Code:
function get_category($brand_id=null){
        if($brand_id) {
            $this->db->where('brand_id',$brand_id);
        }
        return $this->db->get('tbl_category');
    }

Brand_model
Code:
function get_data(){
        $query=$this->db->get('tbl_brand');
        return $query;
    }

View
Code:
<!DOCTYPE HTML>
&lt;html lang="en"&gt;
    &lt;head&gt;
    &lt;meta charset="utf-8" /&gt;
    &lt;title&gt;&lt;?=$title;?&gt;&lt;/title&gt;
    &lt;link rel="stylesheet" href="&lt;?=base_url()?&gt;assets/css/site.css" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <div class="field">
            <label for="brand_id">Brand Available</label>
            <select name="brand_id" id="brand_id" class="medium">
            &lt;?php foreach($brandList->result() as $row): ?&gt;
            <option value="&lt;?=$row->id;?&gt;">&lt;?=$row->name;?&gt;</option>
            &lt;?php endforeach; ?&gt;  
            </select>
        </div>
        <div class="field">
            <label for="category_id">Category Available</label>
            <select name="category_id" id="category_id" class="medium">
            &lt;?php foreach($categoryList->result() as $row): ?&gt;
            <option value="&lt;?=$row->id;?&gt;">&lt;?=$row->name;?&gt;</option>
            &lt;?php endforeach; ?&gt;
            </select>
        </div>
        [removed][removed]
        [removed][removed]
    &lt;/body&gt;
&lt;/html&gt;
Javascript
Code:
$(function(){

$('#brand_id').change(function(e){

    $.get('get_cat/'+$(this).val(), function(data) {
        var new_cat_list = ""
        for (var i=0; i<data.categoryList.length; i++)
        {
            new_cat_list += '<option value="'+data.categoryList[i].id+'">'+data.categoryList[i].name+'</option>';
        }
        $('#category_id').html(new_cat_list);
    }, 'json');

});

});


Messages In This Thread
n/a Change values in one list box based on selection in another - by El Forum - 12-31-2010, 12:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB