Dependent Dropdown in Yii2 lost values on Update - yii2

I created a form with a dropdownlist dependent on another (without extensions) and it ran fast.
But when you try to change the same form, the value of the second drop-down list is lost (type_id). Basically the Type field depends on the selected Area. See my code:
_form.php
<?= $form->field($model, 'area_id')->dropDownList(ArrayHelper::map(Department::find()->where(['helpdesk' => 1])->all(),'id','name'),['prompt'=>'Selecione a Área',
'onchange' => '
$.post("index.php?r=helpdesk/solicitation/lists&id=' . '"+$(this).val(),function(data){
$("select#solicitation-type_id").html(data);
});']) ?>
<?php
echo $form->field($model, 'type_id')->dropDownList(['prompt'=>'Selecione a Área']);
?>
Controller
public function actionLists($id)
{
$countType = Type::find()
->where(['area_id' => $id])
->count();
$types = Type::find()
->where(['area_id' => $id])
->orderBy(['name' => SORT_ASC])
->all();
if($countType > 0 )
{
foreach($types as $type ){
echo "<option value='".$type->id."'>".$type->name."</option>";
}
}
else{
echo "<option> - </option>";
}
}
I think it's the same problem at this topic, but I did not understand how it worked.

you need to provide the default set of options from which it should select the saved option whereas you are not providing any data
<?php
$types = Type::find()
->where(['area_id' => $model->area_id])
->orderBy(['name' => SORT_ASC])
->all();
echo $form->field($model, 'type_id')->dropDownList(ArrayHelper::map($types,'id','name'),['prompt'=>'Selecione a Área']);
?>
Now when you will use findOne() and select the model the saved value for the type_id will be automatically selected.
You can move the above query in the controller/action and pass the $types to the view.
also you can make you method actionLists D.R.Y by using count($types) and remove the first query
public function actionLists($id)
{
$types = Type::find()
->where(['area_id' => $id])
->orderBy(['name' => SORT_ASC])
->all();
if(count($types) > 0 )
{
foreach($types as $type ){
echo "<option value='".$type->id."'>".$type->name."</option>";
}
}
else{
echo "<option> - </option>";
}
}

Related

Dependend dropdowns not refreshing properly in Yii2

I have a problem that's driving me mad. I have two Select2-Dropdowns showing employees, one for dayshift, the other for nightshift. The employee selected for dayshift should be removed from the nightshift dropdown and vice versa. I managed to refresh the nightshift dropdown when an employee is selected in the dayshift dropdown, but when I select an employee in the nightshift dropdown he isn't removed from the dayshift dropdown...
here's the snippet from the _form.php:
<?= $form->field($model, 'customer_id')->widget(Select2::classname(), [
'options'=>['id'=>'customer-id','class'=>'customer-id shift-name'],
'disabled' => !$create_jn,
'data' => ArrayHelper::map(customer::find()->where(['active_yn' => 1])->orderBy(['name'=>SORT_ASC])->all(),'customer_id','name')
]); ?>
<?=
$form->field($model, 'site_id')->widget(DepDrop::classname(), [
'options'=>['id'=>'site-id','class'=>'site-id shift-name'],
'disabled' => $disable_yn,
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions'=>[
'depends'=>['cutomer-id'],
'placeholder'=>'Please choose',
'url'=>Url::to(['/site/customer-site']),
// 'initialize'=>true,
]]);
?>
<?= $form->field($model, 'site_employee_id')->widget(DepDrop::classname(), [
'options'=>['id'=>'site-employee-id'],
'disabled' => $disable_yn,
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions'=>[
'depends'=>['customer-id'],
'placeholder'=>'Please choose',
'url'=>Url::to(['/site/customer']),
'params' => ['site-employee-night-id'],
// 'initialize'=>true,
]
]); ?>
<?= $form->field($model, 'bst_leiter_nacht_id')->widget(DepDrop::classname(), [
'options'=>['id'=>'site-employee-night-id'],
'disabled' => $disable_yn,
'type' => DepDrop::TYPE_SELECT2,
'pluginOptions'=>[
'depends'=>['customer-id','site-employee-id'],
'placeholder'=>'Please choose',
'url'=>Url::to(['/site/customer-night']),
'params' => ['site-employee-id'],
'initialize'=>true,
]
]); ?>
and here's the controller:
public function actionCustomer() {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out=array();
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if (isset($_POST['depdrop_params'])) {
$params = $_POST['depdrop_params'];
if ($parents != null) {
$customer_id = $parents[0];
if($params != null) {
$site_employee_night_id = $params[0];
$out= \frontend\models\Employee::find()
->where(['customer_id'=>$customer_id])
->andWhere(['<>','employee_id', $site_employee_night_id])
->select(['employee_id as id','CONCAT(firstname," ",lastname) as name'])
->orderBy('lastname')->asArray()->all();
return ['output'=>$out, 'selected'=>''];
}
}
}
}
return ['output'=>'', 'selected'=>''];
}
public function actionCustomerNight() {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out=array();
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if (isset($_POST['depdrop_params'])) {
$params = $_POST['depdrop_params'];
if ($parents != null) {
$customer_id = $parents[0];
if($params != null) {
$site_employee_id = $params[0];
$out= \frontend\models\Employee::find()
->where(['customer_id'=>$customer_id])
->andWhere(['<>','employee_id', $site_employee_id])
->select(['employee_id as id','CONCAT(firstname," ",lastname) as name'])
->orderBy('lastname')->asArray()->all();
return ['output'=>$out, 'selected'=>''];
}
}
}
}
return ['output'=>'', 'selected'=>''];
}
Any help, tips, hints would be highly appreciated.
The problem might be that night shift dropdown depends on dayshift dropdown and gets refreshed when dayshift one is changed. Since dayshift dropdown depends only on customer-id it won't be refreshed (function in controller wont refresh data) when you change night shift dropdown.
On nightshift add onchange event and trigger refresh manually

How to update field in user table yii2

I have modified user table to add one column, the column's name is id_periode, I have advanced template, so from backend controler i want update that column value. I create controller like this
public function actionPindahPeriode($id)
{
$model2 = $this->findModel2($id);
if ($model2->load(Yii::$app->request->post()) ) {
$model2->save();
return $this->render('view',
'model2' => $this->findModel2($id),
]);
}
date_default_timezone_set('Asia/Makassar');
$jam_sekarang = date('h:i:s', time());
$tgl_sekarang=date('Y-m-d');
$model_periode = Periode::find()
->andWhere(['>','mulai_daftar_ulang',$tgl_sekarang ])
->asArray()
->all();
return $this->renderAjax('pindah_periode', [
'model_periode' => $model_periode,
'model2' => $this->findModel2($id),
]);
}
The findModel2 function is like this
protected function findModel2($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
I render that model into a form
<?php $form = ActiveForm::begin(); ?>
<?php $listData=ArrayHelper::map($model_periode,'id',function($model_periode){
return $model_periode['nama_periode'].' Tahun '.$model_periode['tahun'];});?>
<?= $form->field($model2, 'id_periode')->dropDownList($listData, ['prompt' => '']) ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-danger']) ?>
</div>
<?php ActiveForm::end(); ?>
The form is working but i can not update the value id_periode column in table user. There is not error showing, any suggestion?
Make sure your new attribute id_periode has a rule defined in the public function rules(){} function, this way $model2->load(Yii::$app->request->post()) will assign this value from the data submitted.
$model->save() returns a bool value whether the record was saved or not. You can use this to your advantage and check whether there are any validation errors.ie:
if($model2->save()) {
return $this->render('view',
'model2' => $this->findModel2($id),
]);
} else {
return $this->renderAjax('pindah_periode', [
'model_periode' => $model_periode,
'model2' => $this->findModel2($id),
]);
}
Option 1 : Check your column fields in User model's validation rules. You need to shift unwanted column fields from required attribute to safe attribute.
Option 2 : try $model2->save(false);. false will override your model rules.

How to make dependent dropdownlist in Yii2?

I'm trying to make a dependend dropDownList in Yii2. I'm trying to use DepDrop Widget, but I can't understand how to edit the code according to my situation. I have 1 model and inside of it I need to make category dropdown list and according to the category_id, the next dropDownList should be Item. (F.e If I select category 1, the Item should be Item1 and so on).
I guess that extension only can do dropdowns of the same model? I'm new to Yii2, so.
My view file:
<div class="site-create">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'code') ?>
<?= $form->field($category, 'id')->dropDownList($category, ['id'=>'category-id']); ?>
<?= $form->field($item, 'subcat')->widget(DepDrop::Item(), [
'options'=>['id'=>'item-id'],
'pluginOptions'=>[
'depends'=>['category-id'],
'placeholder'=>'Select...',
'url'=>Url::to(['/site/subcat'])
]
]); ?>
My $model is different model from $category and $item. I set those variables to use a different model in the action
Here is my action:
public function actionSubcat() {
$category = new Category();
$item = new Item();
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getSubCatList($cat_id);
echo Json::encode(['output'=>$out, 'selected'=>'']);
return $this->render('create', [
'category' => $category,
'item' => $item,
]);
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
}
Now I'm getting the error message that the $category variable is undefined. Could someone explain me what I'm doing wrong?
Actually you dont need any Widgets or Plugins. In yii2 you can easily do Dependent Dropdown list here is example:
echo $form->field($search,'category')->dropDownList(
ArrayHelper::map(
\app\models\Category::find()->all(),
'id','name'
),
[
'prompt' => Yii::t('app','choose_city'),
'onchange'=>'
$.get( "'.Yii::$app->urlManager->createUrl('site/dropdown?id=').'"+$(this).val(), function( data ) {
$( "select#subcat" ).html( data );
})'
]
);
echo $form->field($search, 'sub_category', ['inputOptions'=>['id'=>'subcat',]])->dropDownList([]);
after that you need to create action for returning action. Example:
public function actionDropdown($id)
{
$countPosts = \app\models\SubCategory::find()
->where(['category_id' => $id])
->count();
$posts = \app\models\SubCategory::find()
->where(['category_id' => $id])
->orderBy('name ASC')
->all();
echo "<option value=''>-</option>";
if($countPosts>0){
foreach($posts as $post){
echo "<option value='".$post->id."'>".Yii::t('app',$post->name)."</option>";
}
}
}
thats all.
i suggest using knockoutjs for doing this. i answered a similar question here
knockout example is here (category > product)
html:
<select data-bind="options: data, value: selected, optionsText: 'title', optionsCaption: '---'"></select>
<select data-bind="options: dependent"></select>
javascript:
<script>
function viewmodel() {
var self = this;
this.data = [
{ title: 'title a', items: ['a1', 'a2', 'a3'] },
{ title: 'title b', items: ['b1', 'b2', 'b3'] },
{ title: 'title c', items: ['c1', 'c2', 'c3'] }
];
this.selected = ko.observable();
this.dependent = ko.computed(function() {
return ((self.selected() || {}).items || []);
});
}
$(document).ready(function() {
ko.applyBindings(new viewmodel());
});
</script>
you can still use $form->field, just add 'data-bind' attribute as inputOption.
if you want to use the field's default values, Json::encode those key-value-pairs and create the viewmodel like this:
ko.applyBindings(new viewmodel(<?= Json::encode($data) ?>));
using those parameters is javascript manual labour
<?= $form->field(**$category**, 'id')->dropDownList(**$category_dropdown**, ['id'=>'category-id']); ?>
Both the names are not same:
$category = your model
$category_dropdown = arrayhelper variable in your action
like below
$transactioncategory = ArrayHelper::map(Transactioncategory::find()->all(), 'id', 'transaction_category');

Drop down does not show the option selected in codeigniter

I used a drop down button to select an option from the given options. But when I selected an option it does not display that in the box. And I it gives chance only once to select an option. For the drop down I retrieve data from the database.
This is the view code (Its inside Assign_Inquiries)
<select id = "counsellorname" name="counsellornamename" class="btn btn-default dropdown-toggle">
<option value = "0"> Select Category Name</option>
<?php
foreach($result as $row){
echo "<option value = ".$row['email'].">".$row['fname']." ".$row['lname']."</option>";
}
?>
</select>
Its really great if someone can help me. Thanks inadvance
This is the controller code
<?php
class Assign_Inquiries_Controller extends CI_Controller{
function index(){
$this->load->model('Assign_Inquiries_Model');
$data['result'] = $this->Assign_Inquiries_Model->index();
//print_r($data);
$this->load->view('Assign_Inquiries',$data);
}
}
?>
This is the model code
<?php
class Assign_Inquiries_Model extends CI_Model{
function index(){
$this->db->select('first_name,last_name,email');
$where = "status =3";
$this->db->where($where);
$query = $this->db->get('user');
foreach ($query -> result() as $row) {
$data[] = array(
'fname' => $row->first_name,
'lname' => $row->last_name,
'email' => $row->email
);
}
return $data;
}
}
?>
Change your model code as below: use result_array() which returns the result in array format.
function index(){
$this->db->select('first_name,last_name,email');
$this->db->where('status',3);
$query = $this->db->get('user');
return $query ->result_array();
}
In view you loop must be like this....
foreach($result as $row){
echo "<option value = ".$row['email'].">".$row['first_name']." ".$row['last_name']."</option>";
}

where am i going wrong in my extraction of value from mysql database?

where am i going wrong in my extraction of country name from mysql database? (codeigniter: activerecord enabled)
Its printing out "default country" instead of expected "Australia".
Pages Controller
public function view($page)
{
$page = strtoupper($page); // Capitalize the first letter
$data = array(
'title' => 'My Title',
'country' => 'default country'
);
$this->getCountryName($page, $data);
if(!is_null($data['country'])){
$page = "country";
}
$this->load->helper('url');
$this->load->helper('utility');
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
if(!is_null($data['country'])){
$this->load->view('templates/infobox', $data);
}
$this->load->view('templates/footer', $data);
}
function getCountryName(&$page, &$data){
$this->db->select('name')->from('pv_country')->where('code', $page);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data['country'] = $row;
echo $row;
}
}
}
Country View
<div style =" position:absolute;top:90%; background: red;">
<?php echo $country; ?></div> // prints default country.
</div>
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: pages/country.php
Line Number: 25
so i put in some more debugging and found the correct syntax/method to call to get the result,
which then returns an object with attributes that need to be accessed in the view.
function getCountryName(&$page, &$data){
$this->db->select('name')->from('pv_country')->where('code', $page);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$data['country'] = $query->row();
}
}
<?php echo $country->name; ?>