如何将json数据填充到下拉列表
这是json链接:http://localhost/data
这是json格式:
[{"inst_id":"1","inst_code":"001","inst_name":"HARVARD"},{"inst_id":"2","inst_code":"002","inst_name":"UCLA"}]
这个观点:
<?= $form->field($model, 'institusi')->dropDownList(); ?>
来自&#34; inst_id&#34;的下拉值来自&#34; inst_name&#34;
的文本我有代码,但它不是活动形式
$url = 'http://localhost/data';
$content = file_get_contents($url);
$json = json_decode($content, true);
$inst=array();
echo "<select>";
foreach($json as $item) {
echo "<option value='".$item['inst_id']."'>".$item['inst_name']."</option>";
}
echo "</select>";
所以如何将json数据填充到Activeform Dropdownlist
答案 0 :(得分:0)
最简单的方法是在它之前为dropDownList准备数组
<?php
$url = 'http://localhost/data';
$content = file_get_contents($url);
$json = json_decode($content, true);
$instArray = ArrayHelper::map($json,'inst_id','inst_name');
echo $form->field($model, 'institusi')->dropDownList($instArray);
?>
您也可以从控制器准备相同的数组,并将其传递给render
方法查看。
其他选项是创建组件或向模型添加静态方法。两者都应该返回json数据的数组格式。