yii:如何获取发布数据?

时间:2012-08-30 03:23:46

标签: php jquery ajax yii

状态:通过ajax创建包含项目列表的表单。将新项目添加到表单中工作正常。计算工作正常。但无法在控制器(创建)中获取添加的项目。

问:如何从视图的帖子中获取控制器上的数据?

这是我的观点。

<div class="form">

<?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;
        });

        $('.item-name').live('change', function(){
            var itemID = $(this).val(),
                vNode  = $(this);
            $.ajax({
                url: 'getitem/'+itemID,
                dataType: 'json',
                success: function(data){
                    var target = $(vNode).parents('tr');
                    //console.log('target tr: ' + target);
                    $(target).find('.price').text(data.item_price);
                }
            });

        });

        $('.qty').live('keyup', function(){
          var vNode  = $(this);
          var vQty = $(this).val();
          var target = $(vNode).parents('tr');
          var vPrice = $(target).find('.price').text();

          //console.log($(target).find('.price').text());

          var vrowtotal = vQty * vPrice;
          $(target).find('.row_total').text('$ ' + vrowtotal);
           findTotals();
        });

        function findTotals() {

            var subTotal = '';
            $('#item_list tr.template_item').each(function() {

                //console.log($('.row_total').text());              
                var row_total = 0,
                        price = Number($('.price',this).text());
                        qty   = Number($('.qty',this).val());

                    row_total = price*qty; 
                    subTotal = Number(row_total) + Number(subTotal);
            });
            subTotal = Number(subTotal);
            $('#grandtotal').text('$ '+ subTotal);

        }
");


    ?>
<table id="item_list" name="item_list[]">
<tr class="item_list">
    <td>name</td>
    <td>price</td>
    <td>qty</td>
    <td>row_total</td>
<tr>
</table>
<label id="grandtotal"></label>
    <p align="right">
        <?php echo CHtml::link('Add Item','#',array('class'=>'btn btn-info', 'id'=>'add-item')); ?>
    </p>
    <div class="row">
        <?php echo $form->labelEx($model,'acc_category_id'); ?>
        <?php
            echo $form->dropDownList($model,'acc_category_id', $acccategorieslist, array(
            'prompt'=>'Please select a category',
            'options'=>array("$model->acc_category_id"=>array('selected'=>'selected')),
            )
        ); 
        ?>
        <?php echo $form->error($model,'acc_category_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'customer_id'); ?>
        <?php echo $form->textField($model,'customer_id',array('size'=>11,'maxlength'=>11)); ?>
        <?php echo $form->error($model,'customer_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'job_id'); ?>
        <?php echo $form->textField($model,'job_id',array('size'=>11,'maxlength'=>11)); ?>
        <?php echo $form->error($model,'job_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'accmain_id'); ?>
        <?php echo $form->textField($model,'accmain_id',array('size'=>11,'maxlength'=>11)); ?>
        <?php echo $form->error($model,'accmain_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'date'); ?>
        <div class="controls">
        <?php 
            $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                'name'=>'AccRecei[date]',       
                'model'=>$model,
                'value' => $model->date,    
                'options'=>array(
                    'showAnim'=>'fold',
                    'dateFormat' => 'dd-mm-yy',                   
                ),
                'htmlOptions'=>array(
                    'style'=>'height:20px;'
                ),
            ));
            ?>

        <p> Expire Date should not be Current Date.</p>
        </div>
        <?php echo $form->error($model,'date'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'status'); ?>
        <?php echo $form->dropDownList($model,'status', $arr_status, array('prompt'=>'Please select a status')); ?>
        <?php echo $form->error($model,'status'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'description'); ?>
        <?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
        <?php echo $form->error($model,'description'); ?>
    </div>  

    <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,
                'item_name', 
                $accitemslist, 
                array(
                    'prompt'=>'Please select a category',
                    'options'=>array("$model->acc_category_id"=>array('selected'=>'selected')),
                    'class'=>'item-name',
                    )                                    
                ); 
        ?></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>

这是我的控制器

public function actionCreate()
    {
        $model=new AccRecei;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['AccRecei']))
        {
            //$model->attributes=$_POST['AccRecei'];
            //if($model->save())
                //$this->redirect(array('view','id'=>$model->id));

            var_dump($_POST['AccRecei']);
        }

        $records = AccCategory::model()->findAll();
        $acccategorieslist = CHtml::listData($records, 'id', 'name');

        $records = AccItem::model()->findAll();
        $accitemslist = CHtml::listData($records, 'id', 'name');

        $arr_status = array('Open'=>'Open', 'Close'=>'Close', 'Late'=>'Late', 'Draft'=>'Draft', 'Partial'=>'Partial');
        $this->render('create',array(
            'model' => $model,
            'acccategorieslist' => $acccategorieslist,
            'accitemslist'=>$accitemslist,
            'arr_status'=> $arr_status,
        ));
    }

1 个答案:

答案 0 :(得分:-2)

在View

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

在控制器

for($i=0; $i < count($_POST['AccRecei']['item_name']); $i++)
            {
                echo    $_POST['AccRecei']['item_name'][$i] . " - " . $_POST['item_qty'][$i] . " - " . $_POST['item_price'][$i] . "<br />";
            }