R
R
rusline2017-02-20 12:17:15
Yii
rusline, 2017-02-20 12:17:15

How to run the depdrop widget?

I can't run the depdrop widget on yii2 Did according to the documentation demos.krajee.com/widget-details/depdrop
Here is the code that I have frontend/views/site/signup.php

use kartik\depdrop\DepDrop;
use yii\helpers\ArrayHelper;
use frontend\models\Country;
use fronted\models\Region;

 <?php 
                $countryList = Country::find()->wherw('id = country_id')-all();
                $countryList = ArrayHelper::map($countryList, 'id', 'name');
                ?>
                <?= $form->field($model, 'country')->dropDownList($countryList, ["id"=>"country_id"]); ?>

                <?= $form->field($model, 'region')->widget(DepDrop::classname(), [
                'options' => ['id'=>'region_id'],
                'pluginOptions'=>[
                    'depends'=>['country_id'],
                    'placeholder' => 'Выберите страну',
                    'url' => Url::to(['/site/region'])
                    ]
                ]); ?>

And I added this code to frontend/views/controllers/SiteController.php
class SiteController extends Controller
{
    public function actionRegion() {
    $out = [];
    if (isset($_POST['depdrop_parents'])) {
        $parents = $_POST['depdrop_parents'];
        if ($parents != null) {
            $country_id = $parents[0];
            $out = self::getRegionList($country_id); 
            // the getSubCatList function will query the database based on the
            // cat_id and return an array like below:
            // [
            //    ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
            //    ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
            // ]
            echo Json::encode(['output'=>$out, 'selected'=>'']);
            return;
        }
    }
    echo Json::encode(['output'=>'', 'selected'=>'']);
    }
}

and tables from frontend/views/models/Country.php database
namespace frontend\models;

use yii\db\ActiveRecord;

class Country extends ActiveRecord
{
  
}

And similarly, the same file was created for the region, only in the class Region is specified there.
As I understand it, the file that I specified /site/region should be a grid table or not. I just did not understand how to add this widget to myself. I get this error: Calling unknown method: yii\db\ActiveQuery::wherw()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2017-02-20
@slo_nik

Good morning.
You are the text of the error, for starters, translate and correct it, then you will move on.
PS
Have you looked at the examples in the widget's documentation? Did you see grid there?
By specifying the address /site/region, you firstly refer to the SiteController controller, and secondly, to the action of this actionRegion controller.
You need to get the data of all regions from the database.
To do this, just go to the database and get the regions. From this object, widget will create a dropdown list on the form.
Explained as best I could.
PSS If you do not know the bourgeois language, then good google took care of it, there are automatic translators, the first one is at translate.google.com, the second one you can find in the google-chrome browser store .
Use either the first one or install the browser extension.
This is the first.
Second.
Open the widget's documentation and translate the text with the help of a translator, trying to figure it out.
Consider the "url" parameter
The documentation says:
Yeah, we translate with a translator
Here, we met some unknown ajax beast. And in addition json... It's not clear... Okay, let's see examples...
And in the example we find the line

echo Json::encode(['output'=>$data['out'], 'selected'=>$data['selected']]);
           return;

Here, just actionProd.
There is no file connection, only use of some unknown getProdList() method with parameters.
We read in the comment:
/**
* the getProdList function will query the database based on the
* cat_id and sub_cat_id and return an array like below:
* [
* 'out'=>[
* ['id'=>'', 'name'=>' '],
* ['id'=>'', 'name'=>'']
* ],
* 'selected'=>''
* ]
*/

We translate:
getProdList function will query the database based on
*cat_id and sub_cat_id and return an array like below:

Yeah, that's where the dog rummaged!!!
So it is necessary to write a query to my database in the method, to the table "what_it_is_called" and get the regions.
And with the help
to give the widget.
Everything.
How to write a request - think for yourself. Here is a link for you to the documentation for the trial.

M
Maxim Timofeev, 2017-02-20
@webinar

the error clearly indicates

$countryList = Country::find()->wherw('id = country_id')-all();

This is a typo not where but where

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question