【问题标题】:Dynamically load second bootstrap selectpicker based on user input of first selectpicker in codeigniter根据 codeigniter 中第一个 selectpicker 的用户输入动态加载第二个 bootstrap selectpicker
【发布时间】:2021-02-24 18:28:38
【问题描述】:

我想根据第一个 selectpicker 的输入 id 将数据从数据库加载到第二个 selectpicker。在第一个 selectpicker 的数据库 id 中是第二个 selectpicker 的外键。

查看文件

第一个选择器

<select name="depot_select" id="depot_select" class="selectpicker" data-live-search="true"  value="<?php echo set_value('depot_select');?>">
                            <option value="">No Depot Selected</option>
                            <?php echo $depots['depot'] ?>
</select>

第二个选择器

<select class="selectpicker" data-live-search="true" name="route_select" id="route_select" value="<?php echo set_value('route_select');?>">
                            <option value="">No Route Selected</option>
                            <?php echo $routes['route']?>
</select>

阿贾克斯

function fetchDepot(){

        $.ajax({
                    type    :'POST',
                    url     : '<?php echo base_url('transporter_dashboard/bus/get_depots'); ?>',
                    dataType: 'json',
                    success : function(data)
                    {
                         $('#depot_select').empty();               
                         var example =  $('#depot_select').selectpicker('val', data['data']);
                         console.log("depot_select"  + example);

                          if(!$.isEmptyObject(data))
                          {

                             $.each(data, function(i,o)
                            {
                                 var depot = $('#depot_select').append('<option value="'+ o['dpt_id'] +'">'+o['dpt_name'] +' </option>');


                            });


                          }
                       $('#depot_select').selectpicker('refresh');
                       fetchRoute();
                        
                    },
                    error: function()
                    {
                        alert('error occour..........!');
                    }
            });


    }


    function fetchRoute()
      { 

        $.ajax(
        {   
            type    :'POST',
            url     :'<?php echo base_url('transporter_dashboard/bus/get_routes'); ?>',
            data    : { dpt_id : $('#depot_select option:selected').val()},
            dataType: 'json',
            success : function(data)
            {


                $('#route_select').empty();
                 $('#route_select').selectpicker('val', data['data']);

                if(!$.isEmptyObject(data))
                {

                    $.each(data, function(i,o)
                    {
                        var route = $('#route_select').append('<option value="'+ o['rot_id'] +'">'+ o['rot_name'] +'</option>');

        

                    });                      

                }
                
                $('#route_select').selectpicker('refresh');

            },
             error: function(data){

                alert('error occour..........!');

            }

        });
    }

控制器

function get_depots()
{

    echo json_encode($this->bus_model->get_depots());

}

function get_routes()
{
    if ($this->input->post('dpt_id')) 
        {
            echo json_encode($this->bus_model->get_routes($this->input->post('dpt_id')));
        }


}

型号

function get_depots()
    {
        $this->db->select('*');
        $data = $this->db->get('depots')->result_array();
        return $data;
    }

function get_routes($id)
{
     $data = array();
        
        $this->db->select('*');
        $this->db->where('dpt_id',$id);
        $data = $this->db->get('routes')->result_array();

        return $data;

}

根据上面的代码,不管选择了哪个选项,每次只有第一个选项的id去后端。然后数据库只加载那些属于第一个选项的 id 的数据。

【问题讨论】:

  • 你试过这样$('#depot_select').val() 吗?

标签: javascript php jquery ajax codeigniter


【解决方案1】:

终于,我找到了这个问题的答案。我们需要在$( document ).ready()内部调用fetchRoute()函数

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 2021-12-20
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多