Yii Framework Forum: Dropdownlist Populating Other Textfiled - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dropdownlist Populating Other Textfiled Rate Topic: -----

#1 User is offline   PaoloVIP 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 11-July 11

Posted 20 January 2013 - 10:22 AM

Hello to all, I hope this was the right section for my question.

I write a drop down list populated from values of another model

<?php echo $form->dropDownList($model,'idcausale_uscita', CHtml::listData(CausaleUscita::model()->findAll(), 'idcausale_uscita', 'uscita')); ?>


Is it possible to change the value of another textfield, in the same form, accordling the value of dropdownlist chosen? I can't figure out... maybe with onchange event?

$model,'idcausale_uscita' is the first model and refer to current form. The second 'idcausale_uscita' (not a good idea give it the same name...), 'uscita' are the values populating dropdownlist of another model.

The table with data to select are very simple:

id – description – nvalue

0 – aaa – 10
1 – bbb – 12
2 – ccc – 5

… and so on

I need to select from description field (and this work), and put in another textfield the corrisponding “nvalue”. And this I can't figure out.

Thank you in advance
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 20 January 2013 - 12:57 PM

/* moved from Miscellaneous Posted Image */
"Less noise - more signal"
0

#3 User is offline   PaoloVIP 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 11-July 11

Posted 10 February 2013 - 06:58 AM

To clarify what I try to do.

I want to update a textField in a CActiveForm when I choose a value from a Dropdownlist. The model is linked to database. But some steps is not so clear for me.

This is the relevant part in _form.php

	<div class="row">
		<?php echo $form->labelEx($model,'idcausale_uscita'); ?>
		<?php
		//Dropdowlist 
		$dati = CHtml::listData(CausaleUscita::model()->findAll(), 'idcausale_uscita', 'uscita');
		$fupdate = array( 'empty'=>' Selezionare una voce ',
       				'ajax'=>array(
        					'type'=>'POST',
        	  				'url'=>CController::createUrl('Spesa/update_prezzo'),
        	  				'replace'=>'#Spesa_importo', // With "update" doesn't work
						)
				);
		echo $form->dropDownList($model,'idcausale_uscita', $dati, $fupdate);
		?>

	<div class="row">
		<?php echo $form->labelEx($model,'importo'); ?>
		<?php 

			echo $form->textField($model,'importo', array('size'=>10,'maxlength'=>10));
		?>
		<?php echo $form->error($model,'importo'); ?>
	</div>

This is SpesaController.php

    public function actionUpdate_prezzo()
   {
	
	//Only work with "replace" function in Dropdownlist
	echo CHtml::textField('Spesa[importo]','123', array('id'=>'Spesa_importo','size'=>10,'maxlength'=>10)); //"123" Is arbitrary value, just to try if it work.

   }


First question. The Dropdownlist in the form works fine, read data from database and it is corrected populated. The controller, in this way, works too, but I'm not shure this is the more logical way to do this work. But with "update" option, instead "replace", I can't save data in database. Is there a way to simply set the 'importo' value in CActiveForm with controller function?

Second question. Can you put me in the right direction to substitute the manual value "123" with a variable pupulated by a field from a record selected by Dropdownlist?

Thank you in advance.
0

#4 User is offline   Coksnuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 14-May 09

Posted 10 February 2013 - 01:26 PM

View PostPaoloVIP, on 10 February 2013 - 06:58 AM, said:

First question. The Dropdownlist in the form works fine, read data from database and it is corrected populated. The controller, in this way, works too, but I'm not shure this is the more logical way to do this work. But with "update" option, instead "replace", I can't save data in database. Is there a way to simply set the 'importo' value in CActiveForm with controller function?


This functionality is not natively provided by the framework. Anyway, its very easy to achieve that once you look at the source code of CHtml::ajax()

_form.php
[...]
$fupdate = array(
	'empty' => ' Selezionare una voce ',
	'ajax' => array(
		'type'    => 'POST',
		'url'     => $this->createUrl('Spesa/update_prezzo'),
		'success' => new CJavaScriptExpression('function(html){jQuery("#'.CHtml::activeId($model, 'importo').'").attr("value", html)}'),
	)
);
[...]


SpesaController.php
public function actionUpdate_prezzo()
{
	echo 'This text will show up as the textbox value';
}


View PostPaoloVIP, on 10 February 2013 - 06:58 AM, said:

Second question. Can you put me in the right direction to substitute the manual value "123" with a variable pupulated by a field from a record selected by Dropdownlist?

Thank you in advance.


Once again: Checking the source code of CHtml::ajax() reveals this:
if(!isset($options['data']) && isset($options['type']))
        $options['data']=new CJavaScriptExpression('jQuery(this).parents("form").serialize()');


So you have full access to the user entered values.

SpesaController.php
public function actionUpdate_prezzo()
{
	echo Spesa::model()->findByPk($_POST['Spesa']['id']))->importo;
}

0

#5 User is offline   PaoloVIP 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 11-July 11

Posted 10 February 2013 - 05:35 PM

Thank you for your kindly reply.

You fully answered at my first question, and your solution works fine. But I will lie if I tell you that all is clear now. This code

 'success' => new CJavaScriptExpression('function(html){jQuery("#'.CHtml::activeId($model, 'importo').'").attr("value", html)}'), 


is still criptic for me and the source code of CHtml::ajax() help me only to feel me more stupid than ever. :-) If you can point me to some guide, book or manual that can be useful (jquery?, ajax?) to learn this, it will be very appreciate.

The solution for second answer doesn't work, and I will investigate. (Parenthesis error, and after deleted one parenthesis "Undefined index: id" error.)

Thank you again for your time and for your precious help.
0

#6 User is offline   Coksnuss 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 142
  • Joined: 14-May 09

Posted 10 February 2013 - 05:46 PM

I do not know about a good jQuery tutorial, but I guess there a dozen's of it in the WWW. I can just refer you to the offical documentation for the jQuery ajax function: http://docs.jquery.c...ry.ajax#options

About the "Undefined index" error: Unless i know your fomular i can only guess which data is submitted on the ajax request... Just use var_dump($_POST) to find out - or view the source code of your formular.
0

#7 User is offline   PaoloVIP 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 11-July 11

Posted 10 February 2013 - 06:58 PM

The second solution for me works too

public function actionUpdate_prezzo()
{
        echo CausaleUscita::model()->findByPk($_POST['Spesa']['idcausale_uscita'])->importo;
}


Thank you again
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users