【问题标题】:Edit table-cells in a view在视图中编辑表格单元格
【发布时间】:2016-10-20 03:16:56
【问题描述】:

Yii 版本 1.1.15

我有一个模型,它在属性中包含一个序列化数组(表)。将此数组显示为视图中的表格可以正常工作。

但我想更新表格单元格,然后再次保存模型中序列化的数组。这不起作用。输出在此处停止 (*) 且没有错误。

在这里你可以看到我做了什么:

$model->table 包含一个序列化数组。未序列化的数组如下所示:

array(
    array('dog', 'cat', 'cow'),
    array('meat', 'milk', 'gras'),
)

我在控制器中反序列化表:

控制器:contactController.php

$model = Contact::model()->findByPk((int) $id);
$table = unserialize($model->input)

$this->render('contact_form', array(
        'table' => $table));
}

现在我想以 html-table 的形式显示和编辑数组 $table:

查看:contact_form.php

<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'contact-form',
));
?>

<table border="2">
    <?php foreach ($table as $row): ?>
        <tr>
            <?php foreach ($row as $value): ?>                
                <td>      
                    <!-- (*) output stops here, without errors -->
                    <?= $form->textArea($value, '', array('rows' => 3, 'cols' => 20)); ?>
                </td>
            <?php endforeach ?>
        </tr>
    <?php endforeach ?>
</table>

<div class="row buttons">
    <?php echo CHtml::submitButton("save"); ?>
</div>

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

输出在“... textArea ...”之前停止。

如果我只在视图中显示 $table(没有表单),它就像一个魅力:

<table border="2">
    <?php foreach ($table as $row): ?>
        <tr>
            <?php foreach ($row as $value): ?>
                <td><?= CHtml::encode($value) ?></td>
            <?php endforeach ?>
        </tr>
    <?php endforeach ?>
</table>

希望这对我有所帮助 :-) 我怎样才能让这个好主意发挥作用?

【问题讨论】:

    标签: arrays forms serialization yii html-table


    【解决方案1】:

    实际上你缺少 CactiveForm.textArea(.......) 的参数

    http://www.yiiframework.com/doc/api/1.1/CActiveForm#textArea-detail

    textArea($value, '', array('rows' => 3, 'cols' => 20)); ?>

    你的代码应该是

    textArea($model, $value, array('rows' => 3, 'cols' => 20)); ?>

    【讨论】:

    • $table 包含的数据不是 $model。所以我尝试了: textArea($table, $value, array('rows' => 3, 'cols' => 20)); ?>
    • $table 包含的数据不是 $model 我已经用 $table 尝试过你的建议并得到一个错误:“数组到字符串的转换”。也许这很重要:正如您在我的代码中看到的:$table 不是模型,它只是一个数组。
    • public string textArea(CModel $model, string $attribute, array $htmlOptions=array ( )) 如您所见,第一个参数应该是模型对象,因此它将是 textArea($model, $value,数组('rows' => 3, 'cols' => 20));
    • 我知道我需要一个模型......但我只有一个数组。是否可以将数组转换为模型?如果没有文件 model/fruits.php,这可能吗?如果可能的话,怎么做?
    • 实际上你在contact_form.php页面上有模型 $this->render('contact_form', array( 'model' => $model, 'table' => $table));这意味着你在 contact_form 页面上有 $model 但是如果你想在那个 textArea 上使用不同的模型,你可以很容易地通过 new Fruits() NB 做到这一点:model/fruits.php(应该是 Fruits.php)
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多