dependent dropdown yii2. How to do? - yii2

can I create a dependent dropdown in yii2?
I have two tables:
'id','name_country"
'id','name_city','country_id'
and have two methods in my model:
public function getCountryList()
{
$models = NetCountry::find()->asArray()->all();
return ArrayHelper::map($models, 'id', 'country_name');
}
and
public function getCityList($parent_id) {
$models = \common\models\City::find()->where(['parent_id' => $country_id])->asArray()->all();
return ArrayHelper::map($models, 'id', 'name_city','country_id');
}
I have the first field:
<?= $form->field($model, 'country')->dropDownList($model->countryList),['id'=>'parent_id'];
and the second
<?= $form->field($model, 'city')->dropDownList($model->cityList);
I need to 'transmit' parent_id to controller and return city_list by AJAX (with JSON).
How can I do this? I saw an example in Yii1, but what about Yii2?

use the krajee extension for dependent drop down
Details is here Krejee dependent dropdown for yii2
or follow following instructions:
Install the extension via composer:
$ php composer.phar require kartik-v/dependent-dropdown "dev-master"
In your view :
use kartik\widgets\DepDrop;
// Normal parent select
echo $form->field($model, 'cat')->dropDownList($catList, ['id' => 'cat-id']);
// Dependent Dropdown
echo $form->field($model, 'subcat')->widget(DepDrop::classname(), [
'options' => ['id' => 'subcat-id'],
'pluginOptions' => [
'depends' => ['cat-id'],
'placeholder' => 'Select...',
'url' => Url::to(['/site/subcat'])
]
]);
// THE CONTROLLER
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getSubCatList($cat_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'=>'']);
}

creating dependent dropdown in yii2 without using any third party libraries is
quite as simple as yii1. you have to try following code written below as per your requirements.
use gii to create models,views, controller for respective tables.
suppose there r two table like country, city as u written.
then write the following code into views file for one controller(like country):
<?php
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
?>
<div>
<?php
$dataCountry=ArrayHelper::map(\app\models\Country::find()->
asArray()->all(),'id', 'name');
$form = ActiveForm::begin();
echo $form->field($model, 'id')->dropDownList($dataCountry,
['prompt'=>'-Choose a Name-',
'class'=>'adjust',
'onchange'=>'
$.post("'.Yii::$app->urlManager->createUrl('city/lists?id=').
'"+$(this).val(),function( data )
{
$( "select#city" ).html( data );
});
']);
$dataPost=ArrayHelper::map(\app\models\City::find()->
asArray()->all(), 'id', 'city');
echo $form->field($model, 'id')
->dropDownList(
$dataPost,
['id'=>'city',
'class'=>'adjust'
]
);
ActiveForm::end();
?>
</div>
and after this in another controller for city write following code as:
<?php
namespace app\controllers;
class CityController extends \yii\web\Controller
{
public function actionLists($id)
{
//echo "<pre>";print_r($id);die;
$countPosts = \app\models\City::find()
->where(['country_id' => $id])
->count();
$posts = \app\models\City::find()
->where(['country_id' => $id])
->orderBy('id DESC')
->all();
if($countPosts>0){
foreach($posts as $post){
echo "<option value='".$post->id."'>".$post->city."</option>";
}
}
else{
echo "<option>-</option>";
}
}
}
then run into url it works!
edit: fixed url construction. http requests will now work.

You can do it without any widget manually:
make your activeform as follows:
<?= $form->field($model, 'nameofyourmodel')->dropDownList(
ArrayHelper::map(\app\models\nameofyourmodel::find()->all(), 'id', 'name'),
[
'prompt'=>'smth',
'onchange' => '
$.post(
"' . Url::toRoute('getoperations') . '",
{id: $(this).val()},
function(res){
$("#requester").html(res);
}
);
',
]
); ?>
and here the second form which receives the id from the first model:
<?= $form->field($model,'nameofyourmodel')->dropDownList(
[],
[
'prompt' => 'smth',
'id' => 'requester'
]
); ?>
and the last action is to make a functionality in controller to match 2 ids and send them to your model:
public function actionGetoperations()
{
if ($id = Yii::$app->request->post('id')) {
$operationPosts = \app\models\firstmodel::find()
->where(['id' => $id])
->count();
if ($operationPosts > 0) {
$operations = \app\models\secondmodel::find()
->where(['firstmodelid' => $id])
->all();
foreach ($operations as $operation)
echo "<option value='" . $operation->firstmodelid. "'>" . $operation->name . "</option>";
} else
echo "<option>-</option>";
}
}

The above code is not working properly. There is an error in the line
$.post("'.Yii::$app->urlManager->createUrl('city/lists&id=').'"+$(this).val(),function( data )
console shows the error :
Not Found (#404): Unable to resolve the request: subcategory/lists&id=54
is there any solution for this
my controller looks like below
public function actionLists($id)
{
$posts = SubCategory::find()
->where(['category_id' => $id])
->orderBy('id DESC')
->all();
if($posts){
foreach($posts as $post){
echo "<option value='".$post->id."'>".$post->name."</option>";
}
}
else{
echo "<option>-</option>";
}
}
when i remove the id from the url and hard coded it in to controller it works properly.
I have find a solution for this
please change your view as follows
<?= $form->field($model, 'category_id')->dropDownList($data,['prompt'=>'-Choose a Category-',
'onchange'=>'
$.get( "'.Url::toRoute('product/catlists').'", { id: $(this).val() } )
.done(function( data )
{
$( "select#product-sub_categoryid" ).html( data );
});
']); ?>
and controller like this
public function actionCatlists($id)
{
$mymodel = new Product ();
$size = $mymodel->modelGetCategory ( 'product_sub_category',$id );
if($size){
echo '<option value="">Choose Sub category</option>';
foreach($size as $post){
echo "<option value='".$post['id']."'>".$post['name']."</option>";
}
}
else{
echo '<option value="0">Not Specified</option>';
}
}
don't forget to include this on your view
use yii\helpers\Url;

Related

Yii2 DepDrop Kartik widget

I am trying to use the Kartik Depdrop widget. When I select the area field, the firebug console shows the data correctly like:
{"output":[{"id":"6310100000","name":"Харьков"},{"id":"6310400000","name":"Изюм"}],"selected":""}
_form.php
<?= $form->field($model, 'reg')->dropDownList(TReg::getOptions(), ['id' => 'cat-id', 'class'=>'input-large form-control']); ?>
<?= $form->field($model, 'area')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id', 'class'=>'input-large form-control'],
'pluginOptions'=>[
'depends'=>['cat-id'],
'placeholder'=>'Select...',
'url'=>Url::to(['/pt/subcat'])
]
]); ?>
Model
TReg.php
public static function getOptions()
{
$data=static::find()->all();
$value=(count($data)==0)? [''=>'']: \yii\helpers\ArrayHelper::map($data, 'reg_id','reg_name');
return $value;
}
Model TArea.php
public static function getAreaList($reg_id)
{
$data= static::find()->where(['reg_id' => $reg_id])->select(['area_id AS id' , 'area_name AS name'])->asArray()->all();;
$value=(count($data)==0)? [''=>'']: $data;
return $value;
}
Controller
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$reg_id = $parents[0];
$out = \common\models\TArea::getAreaList($reg_id);
echo Json::encode(['output' => $out , 'selected' => '']);
return;
}
}
echo Json::encode(['output' => '', 'selected' => '']);
}
What is my mistake? Thanks.

How to grab the value of a dropDownList in Yii2?

I am not grabbing the selected value ($model->attribute) on a dropDownList in Yii2, what could be wrong? Thanks
This is the code that is located on a View:
$command = $connection->createCommand('SELECT att1
FROM table
ORDER By table_id ASC');
$rows = $command->queryAll();
$command->execute();
$listData = ArrayHelper::index($rows, null, 'table_id');
Then on the same View I call to $listData
<div class="row">
<div class="col-lg-9">
<?php Pjax::begin(['enablePushState' => false]); ?>
<?php $form = ActiveForm::begin(['id' => 'test-form', 'options' => ['data-pjax' => true]
]); ?>
<?= $form->field($model, 'attribute')->dropDownList($listData, ['prompt'=>'List of attributes.']); ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'test-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
</div>
This is the Controller:
public function actionTest()
{
$model = new TestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->insertTest();
return $this->renderAjax('test', ['model' => $model]);
} else {
return $this->renderAjax('test', ['model' => $model]);
}
}
The Model has the definition of $attribute and insertTest() is a function that use the value of $attribute to query onto a DB.
Thinking of you should use
ArrayHelper::map($listData, 'table_id', 'table_id');
Because you need a one-dimensional array.
And it's better to use the ActiveRecord for queries to the database.
ArrayHelper Docs
You are selecting only att1 from table so you should map this column
$listData = ArrayHelper::index($rows, 'att1');
for debug try modify your actionTest commenting $model->insertTest(); and using $model->save();
if the value is saved in db then you should check inside you $model->insertTest() function
public function actionTest()
{
$model = new TestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// $model->insertTest();
$model->save();
return $this->renderAjax('test', ['model' => $model]);
} else {
return $this->renderAjax('test', ['model' => $model]);
}
}
you should use map in ArrayHelper
$listdata = ArrayHelper::map(CategoryModel::find()->orderBy(['table_id' => SORT_ASC])->all(), 'table_id', 'table_text');
Note: 'table_text' is table attribute that will appear in dropdown label.
I found the solution to that issue.
By doing this way the form was not able to save the submitted information.
$command = $connection->createCommand('SELECT att1
FROM table
ORDER By table_id ASC');
$rows = $command->queryAll();
$command->execute();
$listData = ArrayHelper::index($rows, null, 'table_id');
However, by this way the form is able to grab the value selected and put it into the variable.
$rows = table::find()->all();
$listData = ArrayHelper::map($rows,'table_id','att1');

font awesome use in kartik activeform - yii2

I have using kartik active form in my Yii2 development so i want to add icons into the form field. I was followed this LINK and tried below coding
//registered font awesome
rmrevin\yii\fontawesome\AssetBundle::register($this);
<?php $form = ActiveForm::begin();
<?= $form->field($model, 'Bags_count',[
'feedbackIcon' => [
'default' => 'sort-numeric-desc',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput()->hint(' Example: 4'); ?>
<?php ActiveForm::end(); ?>
I have found this unwanted coding by using chrome inspect element feature. hope this will helpful for the answer.
You can change the widget function to use font-awesome-class
Kartik-v Active Form Widget
protected function renderFeedbackIcon()
{
if (!$this->_hasFeedback) {
return '';
}
$config = $this->feedbackIcon;
$type = ArrayHelper::getValue($config, 'type', 'icon');
$prefix = ArrayHelper::getValue($config, 'prefix', 'fa fa-');
$id = Html::getInputId($this->model, $this->attribute);
return $this->getFeedbackIcon($config, 'default', $type, $prefix, $id) .
$this->getFeedbackIcon($config, 'success', $type, $prefix, $id) .
$this->getFeedbackIcon($config, 'error', $type, $prefix, $id);
}

Yii2 Post to controller and get back data

This is my code in view.
<?= $form->field($model, 'projectTitle')->dropDownList($fullprojectlist, [
'prompt'=>'-Choose a Course-',
'id' => 'projectList',
'type'=> 'POST',
'onchange'=>' var value = $("#projectList :selected").val();
//$("#draftproposalform-supervisor").val(value);'
$.post("'.Yii::$app->urlManager->createUrl(["/draftproposalform.php", "id" => value).'", function(data) {
//set value for text box
}
]);?>
<?= $form->field($model, 'supervisor')->textInput(['readonly'=> 'true']); ?>
I am trying to pass the selected value to the controller so that I can query the database to look for the relevant information. Then send back that information to the view to settext.
I know how to get data from the database. I just don't know how I can pass the selected value to controller and get back a value to settext while maintaining the selected value in view.
public function actionDraftproposalform() {
$model = new DraftProposalForm();
if($model->load(Yii::$app->request->post()) && $model->validate())
{
return $this->refresh();
}
else {
$query = new Query;
$query->select(['User.name', 'Project.title', 'Project.project_ID'])
->from('Project')
->innerJoin('Supervisor', '`Project`.`supervisor_ID` = `Supervisor`.`supervisor_ID`')
->innerJoin('User', '`Supervisor`.`user_ID` = `User`.`user_ID`')
->where(['Project.project_type_ID'=>1, 'Project.approval_ID'=>2]);
$projectlist = $query->all();
$fullprojectlist = ArrayHelper::map($projectlist, 'name', 'title', 'project_ID');
return $this->render('draftproposalform', [
'model'=>$model,
'fullprojectlist' => $fullprojectlist]);
}
}
Sorry if it's messy. Truthfully, I don't even know if passing the data back to here is the correct choice.
Edited Codes
View
<?php
$this->registerJs(' $("#projectList").change(function() {
var value = $("#projectList option:selected").val();
alert(value);
$.post(
"'.Yii::$app->urlManager->createUrl(["/draftproposalform.php"]).'",
{id:value},
function(data) {
alert("Test");
$("input[name=\'supervisor\']").val(data);
}); });');
?>
<?= $form->field($model, 'projectTitle')->dropDownList($projectlist, [
'prompt'=>'-Choose a Course-',
'id' => 'projectList',
'type'=> 'POST'
]);
?>
<?= $form->field($model, 'supervisor')->textInput(['readonly'=> 'true']); ?>
Controller
public function actionDraftproposalform() {
$model = new DraftProposalForm();
if(Yii::$app->request->isPost)
{
$id = Yii::$app->request->post("id");
$super = DbProject::findOne(["project_ID"=>$id]);
$supervisor = DbSupervisor::findOne(["supervisor_ID"=>$super->supervisor_ID]);
$user = DbUser::findOne(["user_ID"=>$supervisor->user_ID]);
//$super = DbSupervisor::findOne(["supervisor_ID"=>$id]);
echo $user->name;
exit;
}
else {
$projectlist = ArrayHelper::map(DbProject::find()->where(['project_type_ID' => 1, 'approval_ID' => 2])->asArray()->all(), 'project_ID', 'title');
return $this->render('draftproposalform', [
'model'=>$model,
'projectlist' => $projectlist]);
}
}
Can you test this.
//Controller
<?
public function actionDraftproposalform() {
$model = new DraftProposalForm();
if(Yii::$app->request->isPost)
{
$id=Yii::$app->request->post("id");
$super=Supervisor::findOne(["supervisor_ID"=>$id]);
if($super) echo $super->name;else echo "Not found";exit;
}
else {
$query = new Query;
$query->select(['User.name', 'Project.title', 'Project.project_ID'])
->from('Project')
->innerJoin('Supervisor', '`Project`.`supervisor_ID` = `Supervisor`.`supervisor_ID`')
->innerJoin('User', '`Supervisor`.`user_ID` = `User`.`user_ID`')
->where(['Project.project_type_ID'=>1, 'Project.approval_ID'=>2]);
$projectlist = $query->all();
$fullprojectlist = ArrayHelper::map($projectlist, 'name', 'title', 'project_ID');
return $this->render('draftproposalform', [
'model'=>$model,
'fullprojectlist' => $fullprojectlist]);
}
}
//view register js: put this in your view
<?
$this->registerJs(' $("#projectList").change(function(){
var value = $("#projectList option:selected").val();
$.post(
"'.Yii::$app->urlManager->createUrl(["/draftproposalform"]).'",
{ id:value },
function(data) {
$( "#draftproposalform-supervisor").val(data);
}); });');
?>

Dependent Dropdown in Yii2 making values reset while updating the Active form

Here my problem is i have a form . In that i have dependent dropdown.
for example if i select company name it automatically selects the dependent company email and company phone number. This is working perfectly while creating. but the problem is while am updating the same form, the dependent value getting reset. so that makes me to select the company name for every time but i don't to be like that. once if i select the value while creating the value shouldn't change while updating also.
_form.php
<?= $form->field($model, 'employee_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Employeedetails::find()->all(),'id','employeecode'),
'language' => 'en',
'options' => [
'placeholder' => 'Select a employeecode ...',
'onchange' => '
$.post( "index.php?r=employeedetails/lists2&id='.'"+$(this).val().split("-")[0], function( data ) {
$( "select#claimprocess-claim_for" ).html( data );
}),
$.post( "index.php?r=employeedetails/lists3&id='.'"+$(this).val().split("-")[0], function( data ) {
$( "select#claimprocess-email" ).html( data );
}),
$.post( "index.php?r=employeedetails/lists1&id='.'"+$(this).val(), function( data ) {
$( "select#claimprocess-employee_name" ).html( data );
});',
],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
This is Controller code
controller.php
public function actionLists($id)
{
$countEmployeedetails = Employeedetails::find()
->where(['company_id' => $id])
->count();
$employeedetails = Employeedetails::find()
->where(['company_id' => $id])
->all();
if($countEmployeedetails>0){
foreach($employeedetails as $employee){
echo "<option value='".$employee->id."'>".$employee->employeecode."</option>";
}
}
else{
echo "<option>-</option>";
}
}
public function actionLists1($id)
{
$countEmployeedetails = Employeedetails::find()
->where(['id' => $id])
->count();
$employeedetails = Employeedetails::find()
->where(['id' => $id])
->all();
if($countEmployeedetails >= 0)
{
foreach($employeedetails as $employee)
{
echo "<option value='".$employee->id."'>".$employee->name."</option>";
}
}
else{
echo "<option>-</option>";
}
}
public function actionLists2($id)
{
$countEmployeedetails = Employeedetails::find()
->where(['id' => $id])
->count();
$employeedetails = Employeedetails::find()
->where(['id' => $id])
->all();
if($countEmployeedetails >= 0)
{
foreach($employeedetails as $employee)
{
// $arr["id"] . '-' . $arr['designation']
echo "<option value='".$employee->id. '-' .$employee->name. "'>".$employee->RelationName."</option>";
}
}
else{
echo "<option>-</option>";
}
}
Finally i found answer, actually mistake was mine, because here am using getting records based on company_id but here i have used approvaldetails table id so that is the mistake, after i changed it to company_id, now its working good
public function actionLists2($id)
{
$countEmployeedetails = Employeedetails::find()
->where(['company_id' => $id])
->count();
$employeedetails = Employeedetails::find()
->where(['company_id' => $id])
->all();
if($countEmployeedetails >= 0)
{
foreach($employeedetails as $employee)
{
// $arr["id"] . '-' . $arr['designation']
echo "<option value='".$employee->id. '-' .$employee->name. "'>".$employee->RelationName."</option>";
}
}
else{
echo "<option>-</option>";
}
}