Доброго времени суток. Помогите разобраться. Поставил DopDrop demos.krajee.com/widget-details/depdrop по третьему сценарию. Но не могу заставить второе и третье поле загружать данные из бд. Постоянно висит надпись Загрузка…
И почему то второе поле стоит активным, его нужно сделать не активным как и третье. скрины ниже.
_form.php:
<?php
use app\modules\brand\models\AccBrand;
use app\modules\device_type\models\AccDeviceType;
use app\modules\model_of_device\models\AccModelOfDevice;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use kartik\form\ActiveForm;
use kartik\widgets\DepDrop;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model app\modules\service\models\AccService */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
if ($model->isNewRecord) {
?>
<?= $form->field($model, 'brand')->widget(DepDrop::classname(), [
'id'=>'brand-id',
'data'=> ArrayHelper::map(AccBrand::find()->
asArray()->
all(), 'id', 'brand_name'),
'options' => [
'placeholder' => 'Select ...'/*,
'multiple' => true*/
],
'type' => DepDrop::TYPE_SELECT2,
'select2Options'=>['pluginOptions'=>['allowClear'=>true]],
'pluginOptions'=>[
'depends'=>['brand-id'],
'url' => Url::to(['/service/child-brand']),
'loadingText' => 'Загрузка...',
]
]);
?>
<?= $form->field($model, 'device_type')->widget(DepDrop::classname(), [
'data'=> ArrayHelper::map(AccDeviceType::find()->
asArray()->
all(), 'id', 'device_type'),
'options' => ['placeholder' => 'Select ...'],
'type' => DepDrop::TYPE_SELECT2,
'select2Options'=>['pluginOptions'=>['allowClear'=>true]],
'pluginOptions'=>[
'depends'=>['accservice-brand'],
'url' => Url::to(['/service/child-account']),
'loadingText' => 'Загрузка...',
]
]);
?>
<!-- --><?php
/* $brand = $this->brand;
$device_type = $this->device_type;
*/?>
<?= $form->field($model, 'model')->widget(DepDrop::classname(), [
'data'=> ArrayHelper::map(AccModelOfDevice::find()->
where('id_brand_name = :id_brand_name', [':id_brand_name' => $brand])->
andWhere('id_device_type = :id_device_type', [':id_device_type' => $device_type])->
asArray()->
all(), 'id', 'model_name'),
'options' => ['placeholder' => 'Select ...'],
'type' => DepDrop::TYPE_SELECT2,
'select2Options'=>['pluginOptions'=>['allowClear'=>true]],
'pluginOptions'=>[
'depends'=>['accservice-device_type'],
'url' => Url::to(['/service/child-account']),
'loadingText' => 'Загрузка...',
]
]);
?>
<?php
}
?>
DefaultController.php:
<?php
namespace app\modules\service\controllers;
use app\modules\brand\models\AccBrand;
use app\modules\device_type\models\AccDeviceType;
use app\modules\model_of_device\models\AccModelOfDevice;
use Yii;
use app\modules\service\models\AccService;
use app\modules\service\models\AccServiceSearch;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
- DefaultController implements the CRUD actions for AccService model.
*/
class DefaultController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all AccService models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new AccServiceSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single AccService model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new AccService model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new AccService();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['update', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionChildBrand() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$id = end($_POST['depdrop_parents']);
$list = AccDeviceType::find()->asArray()->all();
$selected = null;
if ($id != null && count($list) > 0) {
$selected = '';
foreach ($list as $i => $model) {
$out[] = ['id' => $model['id'], 'name' => $model['device_type']];
if ($i == 0) {
$selected = $model['id'];
}
}
// Shows how you can preselect a value
echo Json::encode(['output' => $out, 'selected'=>$selected]);
return;
}
}
echo Json::encode(['output' => '', 'selected'=>'']);
}
public function actionChildAccount() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$id = end($_POST['depdrop_parents']);
$list = AccModelOfDevice::find()->
andWhere(['id_brand_name'=>$brand])->
andWhere(['id_device_type'=>$device_type])->
asArray()->all();
$selected = null;
if ($id != null && count($list) > 0) {
$selected = '';
foreach ($list as $i => $account) {
$out[] = ['id' => $account['id'], 'name' => $account['name']];
if ($i == 0) {
$selected = $account['id'];
}
}
// Shows how you can preselect a value
echo Json::encode(['output' => $out, 'selected'=>$selected]);
return;
}
}
echo Json::encode(['output' => '', 'selected'=>'']);
}
/**
* Updates an existing AccService model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing AccService model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the AccService model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return AccService the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = AccService::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}