Changes
Title
unchanged
Creating a Dependent Dropdown From Scratch in Yii2
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
yii2, dependent dropdown
Content
changed
I have read http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown/ (Dependen Dropdown Yii1), but I can
't implement
ation it in Yii2 because Yii2
notdoes not have built-in AJAX functionality hem.. I
am search
ed about it and
get this statemencame across this post
> qiangxue commented on Aug 28, 2013
In 2.0 we removed most in-page js code. You should write explicit js code in external js files to achieve similar result as in 1.1. The code is very trivial though.
So we must create
our own code hehe oh no we can use great extension http://demos.krajee.com/widget-details/depdrop, this right but some one dislike use third party too much.. Oke I try to explain You about this classic case :)
### Create Dropdown
First, You should know how to make dropdown in Yii2, I use activefield[...]
```php
public function actionLists($id)
{
$countPosts = \common\models\Post::find()
->where(['category_id' => $id])
->count();
$posts = \common\models\Post::find()
->where(['category_id' => $id])[...]
->all();
if($countP (!empty($posts
>0))) {
foreach($posts as $post)
{
echo "<option value='".$post->id."'>".$post->title."</option>";
}
}
else
{
echo "<option>-</option>";
}
}
```[...]