我想将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()')