javascript - CakePHP 2.0 添加 html 表格行

标签 javascript jquery cakephp html-table

我正在尝试向 CakePHP View 中的 html 表中添加行,并在必要时增加 ID/名称。用于生成表格的代码是:

<tbody>
  <tr>
    <td><?php echo $this->Form->input('OrderDetail.0.product_code'); ?></td>
    <td><?php echo $this->Form->input('OrderDetail.0.product_name'); ?></td>
    <td><?php echo $this->Form->input('OrderDetail.0.product_qty'); ?></td>
    <td><?php echo $this->Form->input('OrderDetail.0.product_price'); ?></td>
  </tr>
</tbody>

CakePHP 输出以下 html:

<tbody>
  <tr>
    <td><div class="input text"><input name="data[OrderDetail][0][product_code]" maxlength="100" type="text"/></div></td>
    <td><div class="input text"><input name="data[OrderDetail][0][product_name]" maxlength="255" type="text"/></div></td>
    <td><div class="input number"><input name="data[OrderDetail][0][product_qty]" step="any" type="number"/></div></td>
    <td><div class="input number"><input name="data[OrderDetail][0][product_price]" step="any" maxlength="24" type="number"/></div></td>
  </tr>
</tbody>

单击链接/按钮时,我希望以相同的格式向表中添加一行,但数组增加 1:

<tbody>
  <tr>
    <td><div class="input text"><input name="data[OrderDetail][0][product_code]" maxlength="100" type="text"/></div></td>
    <td><div class="input text"><input name="data[OrderDetail][0][product_name]" maxlength="255" type="text"/></div></td>
    <td><div class="input number"><input name="data[OrderDetail][0][product_qty]" step="any" type="number"/></div></td>
    <td><div class="input number"><input name="data[OrderDetail][0][product_price]" step="any" maxlength="24" type="number"/></div></td>
  </tr>
  <tr>
    <td><div class="input text"><input name="data[OrderDetail][1][product_code]" maxlength="100" type="text"/></div></td>
    <td><div class="input text"><input name="data[OrderDetail][1][product_name]" maxlength="255" type="text"/></div></td>
    <td><div class="input number"><input name="data[OrderDetail][1][product_qty]" step="any" type="number"/></div></td>
    <td><div class="input number"><input name="data[OrderDetail][1][product_price]" step="any" maxlength="24" type="number"/></div></td>
  </tr>
</tbody>

我可以使用普通的旧 Javascript 获得所需的结果,但我正在寻找更优雅的解决方案。我加载了 jQuery 和 JSHelper。

最佳答案

我的解决方案是这样的

$(":button").click(function() {
    $("tr:last-child").clone().appendTo('tbody'); //clone the last row
    $("tr:last-child input").each(function() { //for each input in the last row, do the following
        var nameAttr = $(this).attr('name'); //get the whole value of attribute 'name'
        var newIndex = parseInt(nameAttr.replace(/[^\d]/g, ''))+1; //get the digit and add one
        $(this).attr('name',nameAttr.replace(/\d/,newIndex)); //replace the old digit with new

    });
});

演示: http://jsfiddle.net/CKxLh/

关于javascript - CakePHP 2.0 添加 html 表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9533657/

相关文章:

php - 蛋糕php SQLSTATE[42000] : Syntax error or access violation: 1064

php - PHP 中的 EPUB 阅读器/查看器

javascript - jQuery - 当我的鼠标悬停在它上面时如何获取任何单词的值

javascript - 使用 Joomla 及其 Mootools 的 Twitter Bootstrap Carousel

javascript - 在哪里(在 object3d 中)可以找到变化的点坐标?

javascript - 使用 Javascript/D3.js 访问另一个对象内的对象属性

javascript - 如何使用自定义按钮标签模拟禁用按钮?

jquery - Asp.Net MVC 中的 Ajax 返回问题

php - 在 Media Temple 网格服务上运行 CakePHP Cron 作业

javascript - 使用 javascript 在第一个表格行旁边而不是在第一个表格行之后动态添加表格行