编辑cakephp 2后,将标签更改为可编辑并提交表单

时间:2019-05-09 02:13:45

标签: javascript cakephp-2.x

我正在做一个cakephp 2项目。在我看来,我有一个表格,我想在该表格上编辑单元格。编辑后,我想保存它。

我尝试了HTML 5的contentEditable。这样我可以进行编辑,但数据不会传递给控制器​​。我也尝试过innerHTML,它将单元格更改为输入,但是我无法在输入字段上写任何东西。我该如何解决这个问题?谢谢

<script>
function makeInput(e) {
   document.getElementById('test').innerHTML= "<input type='text'>"
}

function formSubmit2(){
  document.getElementById('StockReportEditForm').submit();
}
</script>
<?php if(!empty($results)): ?>
  <?php echo $this->Form->create('StockReport',array('url'=>array('type'=>'post','action'=>'edit')));?>
<table>
  <tr>
      <th>Year Week</th>
      <th>Counter</th>
      <th>Refrigerator</th>
      <th>Freezer</th>
      <th>Summary</th>
  </tr>

  <?php foreach ($results as $result): ?>
    <?php $today = date("Y-m-d", strtotime("+1 week"));
    $date = new DateTime($today);
    $week = $date->format("W");
    $week_int = (int)$week;
    $week_int=$week_int-1;
    $last_week = (string)$week_int;
    $year = date("Y"); ?>
<tr>
  <?php if($year.$last_week==$result['StockReport']['yearweek']): ?>
    <td onclick="makeInput(this)" id="test">
      <?php echo $result['StockReport']['yearweek']; ?>
  <?php else:?>
    <td><?php echo $result['StockReport']['yearweek'];?></td>
  <?php endif ?>
  </td>
    <td><?php echo $result['StockReport']['counter']; ?></td>
    <td><?php echo $result['StockReport']['refrigerator']; ?></td>
    <td><?php echo $result['StockReport']['freezer']; ?></td>
    <td><?php echo $result['StockReport']['summary']; ?></td>
</tr>

<?php endforeach; ?>
</table>
<?php
// echo $this->Form->submit('Submit');
$options = array(
    'label' => 'Submit',
    'div' => array(
        'class' => 'glass-pill',
    ),
    'id'=>'submitButton',
    'onclick'=>'formSubmit2()'
);
echo $this->Form->end($options); ?>
<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

我会在td元素中创建一个隐藏的输入,提交表单时,我将contenteditabe的htmlinner复制到该隐藏的输入值中。