Using CJUIautocomplete to fill fields based on arithmetic

Hello everyone,

I’ve searched all over for an answer to this, but cannot find any clear examples for what I’d like to do. I have worked to provide this functionality in native java script (getDocumentiD) in the past, but I’d like to know the easiest way to do this in the Yii framework.

[b]My main issue

[/b]What I’d like to do is using the CJuiAutoComplete class, autofill particular fields by adding,dividing, or multiplaying values given from PREVIOUSLY entered fields in a form. I would like this to update dynamically as the values are changed. If anyone could outline the appropriate steps or examples, or a link to a guide I would greatly appreciate it!

[b]Something I would also like to do

[/b]I would also like to achieve something similar for one particular field in my form. What I’d like to do is given the input from a previous field e.g (country name), look up my database for that model all corresponding rows that contain that same previously entered country from the form, take the average of an integer in a column for all those rows, and autofill ANOTHER field on the form.

Thanks for any help!

What you are describing is not ‘autocomplete’.

It is simply doing manipulations using jQuery selectors.

Main issue:

Select value from field one

Multiply against value in field two

Update field 3 with result

This can all be done via clientside JS.

Other ‘issue’:

You essentially need a dependent dropdown style functionality. You pick something from a dropdown, fire an ajax request to the server to run a query with that dropdown selection as part of the query. The results then are multiplied and updated to another field.

Thanks for the clarification. However, I am still a little confused regarding how this would be implemented. Upon my research, it seems I would need to use something like this?





$('input[name="field1"]').keyup(function(){

    var val = parseInt($(this).val());


$('input[name="field2"]').keyup(function(){

    var val2 = parseInt($(this).val());


$('input[name="field3"]').val(field1 + field2);



Where should this be placed?

If this is correct, where would I place this code and how can this implemented in the context of the yii framework in terms of the view? Does the second line, textField contain the id of an input field?





<div class="row">

		<?php echo $form->labelEx($model,'comments'); ?>

		<?php echo $form->textField($model,'comments',array('size'=>60,'maxlength'=>600)); ?>

		<?php echo $form->error($model,'comments'); ?>

	</div>



Here’s what I did.

In this example I bind a function to change a field when another field changes.




<?php

Yii::app()->clientScript->registerScript('assignment',"

$('#Assignment_templateId').bind('change', function()

	{

	$('#Assignment_assignmentName').val($('#Assignment_stationId :selected').text() + ' - ' + ($('#Assignment_templateId :selected').text()));

	});

");

?>



This code should be pretty self-evident. The field #Assignment_assignmentName (look at the html source of your form, or use a class selector, etc…) changes when the templateId field changes. I concatenate two fields into one, but you could just as easily do math, etc.

My $form->textFields are the default - you do not need to assign event handlers to them since you are binding the script as I show you.

.bind() is old code. Use .on() now (see jQuery docs).

Thanks for the help! I appreciate it greatly.

I’ll give this a shot tomorrow and see how it goes.

For the record this code is crummy and it was a copy paste job when I first started learning. Spend a few minutes looking at jQuery selectors documentation and some sample code and you should be in good shape :)