Laravel,jquery相互依赖的选择列表完整代码

时间:2014-04-06 17:11:12

标签: jquery laravel

在最后一行代码中,我无法将其加载到View的Select列表中。我无法填写第二个选择列表(国家地区城市),但我很狡猾,因为这一切对我来说都很清楚,我不明白为什么它不能。在firebug中,我能够(在HTML选项卡上)加载区域的选择列表,但不能将其发送到View。您可能认为只是将数组从控制器发送到视图,但这不会起作用,因为它会在View中使用variable undefined进行投诉。所以你不能有任何变量在那里等待任何值,如果它是以ajax方式生成的,所以我可以在控制器(或在模型中)生成它,但它只能在Firebug中查看。我非常感激,因为我已经达到了我的能力极限。我让它在Codeigniter工作,但不能在Laravel中重现它。这是东西:

THE JQUERY CODE

<script type="text/javascript">
    jQuery(document).ready(function(){

      //When the first select list changes, then the function activates itself

       $('#country').change(cargarProvincias); //load regions when countries change

    });

    function cargarProvincias() {

        // collects the selected value

       var id_pais = $('#country').val();

        // sends the parameter to the controller. It is definitively a correct url as I can see the result
        $regionsurl = '{{URL::route('getregionslist')}}';

//I tried also with $.post, same results as with get. Had to change the route accordingly, of course.

        $.get( $regionsurl, {'id_pais':id_pais} ) ,function(resp){

            //then the controller gets that value and sends it to the Model
            // and the Model does retrieve the correct result.
            // I put that result into a string and that should be picked up by the
            // html function here below (inside the resp parameter) and fill up the select lists at the View

        $('#regions').empty().html(resp);

        };

        }



     </script> 

这是控制者:

public function getregionslist(){

        $id_pais = Input::get('id_pais');               
        (new region)->getregion($id_pais);

    }

It does get the correct id from the View and also gets (if I try it, the correct feedback from the Model)

HERE IS THE MODEL (it does get correct results) but it does not get echoed at the View


 public function getregion($id_pais) {


        $regions = DB::table('regions')->where('id_pais', $id_pais)->get();

         $cadena = ""; 
         foreach ($regions as $region){
         $cadena .= "<option value ='$region->id_region'>$region->nombre_region</option>";          
       }

        echo $cadena;

        return $cadena;

    }

例如,如果我看看Firebug,加德纳为加拿大带来了什么,它会带来这个,这是正确的:

<option value ='2055'>Alberta</option><option value ='2056'>British Columbia</option><option value ='2053'>Manitoba</option><option value ='2050'>New Brunswick</option><option value ='2047'>Newfoundland</option><option value ='2058'>Northwest Territories</option><option value ='2048'>Nova Scotia</option><option value ='2057'>Nunavut</option><option value ='2052'>Ontario</option><option value ='2049'>Prince Edward Island</option><option value ='2051'>Quebec</option><option value ='2054'>Saskatchewan</option><option value ='2059'>Yukon Territory</option>

和观点是:

私人土地

    <select name ="regions" id ="regions" class="form-control">

            </select>

是的,我们希望我们可以从控制器发送数组,然后在View中执行@foreach $regions ->$region,但这会失败,因为它总会返回variable $regions" undefined,所以我必须在模型(或控制器)上进行,并且没有任何变量在视图中等待值。

1 个答案:

答案 0 :(得分:0)

而不是使用以下代码:

$id_pais = Input::get('id_pais');               
(new region)->getregion($id_pais);

在控制器中使用以下代码:

$region = Region::whereId(Input::get('id_pais'))->lists('id_region', 'nombre_region');
return Response::json($region);

然后在客户端使用jQuery构建下拉列表/选择,如下所示:

$.getJSON($regionsurl, {'id_pais':id_pais}, function(resp) {
    $('#regions').empty();
    $.each(resp, function(key, value){
        var option = $('<option/>', {'id':key, 'text':value});
        $('#regions').append(option);
    });
});

还要确保声明Region类/模型如下:

class Region Extends Eloquent {
    protected $table = 'regions';
    protected $primaryKey = 'id_pais'; // if id_pais is your primary key
    // code...
}

P / S:您需要从the documentation学习/阅读更多内容。