Thu Ra
Thu Ra

Reputation: 2063

yii : jquery ajax add data into label

Q : how to add a data value into the label of table's row by ajax?

status : add a new row into table of form is working. at then, when the user choose the item, the price should be show at the label(price). return from controller is fine. but the price didn't show at label(price).

This is my view

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'acc-recei-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>
   <?php
    Yii::app()->clientScript->registerScript('add-item', "
        $('#add-item').click(function(){
            //console.log(item_template);
            $('#template .template_item').clone().appendTo('#item_list');
            $('#item_list .template_item').fadeIn();
            return false;
        });

        $('#AccRecei_acc_category_id').live('change', function(){
                                                               var itemID = $(this).val(),
                    vNode  = $(this);
                    $.ajax({
                  url: 'getitem/'+itemID,
                  dataType: 'json',
                  success: function(data){
                    var target = $(vNode).parents('tr');
                    alert(target);
                    $(target).find('.price').val(data.item_price);
                  }
                });
                //alert(vNode);
            });
");


    ?>
<table id="item_list">
<tr>
    <td>name</td>
    <td>price</td>
    <td>qty</td>
    <td>row_total</td>
<tr>
</table>
<!--- other fields -->

<div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<table id="template" style="display:none;" >
<tr class="template_item">
    <td><?php
            echo $form->dropDownList($model,'acc_category_id', $accitemslist, array(
            'prompt'=>'Please select a category',
            'options'=>array("$model->acc_category_id"=>array('selected'=>'selected')),
            )
        ); 
        ?></td>
    <td><label id="price" class="price"></label></td>
    <td><input type="text" name="item_qty[]" class=".qty" /></td>
    <td><label id="row_total" class="row_total[]"></label></td>
<tr>
</table>

This is the controller (call from ajax)

public function actionGetitem()
    {
        echo CJSON::encode(array(
            'item_price'=>12, 
        ));
    }

Upvotes: 0

Views: 1972

Answers (2)

Udaya Sri
Udaya Sri

Reputation: 2452

Hi you can use jquery html attribute here .. Give some id to the label , then use below code ..

in the ajax success message , add this

$( "#label_ID" ).html( data.item_price )

Upvotes: 0

MrSoundless
MrSoundless

Reputation: 1384

a 'label' tag doesn't have a 'value'. Try using 'text()' instead.

Upvotes: 2

Related Questions