Yii2将下拉值与kartik typeahead结合使用

时间:2017-11-23 14:02:52

标签: php yii2 typeahead.js bloodhound

我想将Country的下拉选择与ajax填充的Typeahead for State结合起来,但我正在努力。我正在为Yii2使用kartik的typeahead小部件我试图破坏实例并重新创建它,但它没有填充数据,见下文。

<?php //country field
                $datacountry = \app\helper\countries::getCountries();
                echo $form->field($client, 'country')->dropDownList($datacountry, ['prompt' => '-Choose a Country-',
                    'onchange' => '$("#county").val("");
            $.post( "' . Url::to(['/state-search?country=']) . '"+$(this).val(), function( data ) {
               $("#county").typeahead("destroy");                 
                $("#county").typeahead({local: JSON.parse(data)});

            });
        ']);
                ?>
<?php


                echo $form->field($client, 'county')->widget(kartik\typeahead\Typeahead::className(), [

                    'options' => ['placeholder' => 'Filter as you type ...','id'=>'county'],
                    'dataset' => [
                        [
                            'local' => [''],
                            'limit' => 10,

                            'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
                            'cache' => false,
                            'display' => 'value',
                            'templates' => [
'suggestion' => new JsExpression("Handlebars.compile('{$template}')")
                            ]
                        ]
                    ],                        
                ]);
                    ?>

或者我可以将js变量传递给远程api for bloodhound吗?

 'remote' => [
                                    'url' => Url::to(['county-search']) . '?q=%QUERY',
                                    'wildcard' => '%QUERY'
                                ],

e.g

 new JsExpression('$("#client-country").val()')

1 个答案:

答案 0 :(得分:0)

也许,您应该使用select2小部件?在那里你可以看到一些演示http://demos.krajee.com/widget-details/select2