首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Yii::为什么类的值只改变一次?

Yii::为什么类的值只改变一次?
EN

Stack Overflow用户
提问于 2012-09-14 15:09:02
回答 1查看 150关注 0票数 0

我在视图上使用ajaxlink来添加提交表单的新行。我需要一个索引来指示哪一行是created.so的,我使用一个类变量来保存索引。但是我发现这个变量只改变了一次。

以下是我的代码

代码语言:javascript
运行
复制
public function actionNewrow()
{
    $this->i++;

    $form = new CActiveForm();
    $temp = new exportprice();

    array_push($this->exps, $temp);
    //echo count($this->exps);

    $i = count($this->exps)-1;

    $html = '<tr><td>'.
                $this->i.$form->labelEx($this->exps[0],'['.$i.']productname').$form->textField($this->exps[0],'['.$i.']productname').$form->error($this->exps[0],'['.$i.']productname')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']trend').$form->textField($this->exps[0],'['.$i.']trend').$form->error($this->exps[0],'['.$i.']trend')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']margin').$form->textField($this->exps[0],'['.$i.']margin').$form->error($this->exps[0],'['.$i.']margin')
            .'</td></tr>';
    echo $html;
}

echo CHtml::ajaxLink("新增",
    Yii::app()->createUrl( 'InputReport/newrow' ),

    // ajax options
    array(
        'error' => 'function(data)   { alert("error!");  }',
        'success' => 'function(data) { $("#exptbl").append(data); }',
        'cache'=>false,
    ),

    // htmloptions
    array(
        'id' => 'handleClick',
    )
);
EN

Stack Overflow用户

回答已采纳

发布于 2012-09-14 16:21:57

所以你通过AJAX调用actionNewrow到一个预定义的类varialbe $i,你每次都是这样做的吗?这就是为什么。PHP与客户端不一致,因为当以这种方式使用PHP时,$i将始终等于先前的值,因为这样的++只会使其只包含一次。

您还需要在客户端使用某种方法来将$i从解密中发送下来:

从您拥有的行数+1中创建索引(这是打开的,以便对JS中的$i变量进行索引,并在自定义函数(可能在ajaxLink构建器之外)中使用它,以使用JS中的$i变量作为类var来传播新行。

下面是一个简单的例子:

代码语言:javascript
运行
复制
var i = <?php echo $this->i // presuming i is a controller car you pass to the view ?>;

$('.add_new_row').on('click', function(){
    $.get('InputReport/newrow', {i:i}, function(data){
        //append your row now
        i++; // inc i here
    });
});

然后在你的控制器中,你会做类似这样的事情:

代码语言:javascript
运行
复制
public function actionNewrow($i = null)
{
    $i = $i===null ? $this->i++ : $i;

    $form = new CActiveForm();
    $temp = new exportprice();

    array_push($this->exps, $temp);
    //echo count($this->exps);

    $i = count($this->exps)-1;

    $html = '<tr><td>'.
                $this->i.$form->labelEx($this->exps[0],'['.$i.']productname').$form->textField($this->exps[0],'['.$i.']productname').$form->error($this->exps[0],'['.$i.']productname')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']trend').$form->textField($this->exps[0],'['.$i.']trend').$form->error($this->exps[0],'['.$i.']trend')
            .'</td>'.
            '<td>'.
                $form->labelEx($this->exps[0],'['.$i.']margin').$form->textField($this->exps[0],'['.$i.']margin').$form->error($this->exps[0],'['.$i.']margin')
            .'</td></tr>';
    echo $html;
}

希望这能帮到你一点忙,

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12419702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档