gpt4 book ai didi

php - 动态添加表单字段行 - cakePHP

转载 作者:行者123 更新时间:2023-12-01 05:50:18 25 4
gpt4 key购买 nike

我有一个发票表和一个invoice_item 表。每个发票有许多发票项。

创建发票时,系统会向用户显示一个包含发票表单字段的表单以及包含发票项表单字段的行。

我想要做的是有一个“添加新项目”链接,可以动态(jQuery、AJAX)添加新的项目字段行。用户应该能够添加任意多的行,并且每个新行应该出现在最后一行的下方。

当然,行字段属性也必须正确,以便可以使用 saveAll 方法轻松插入数据。

使用 CakePHP 实现此目的的最佳且最正确的方法是什么?我正在使用 CakePHP 2.4.7。

最佳答案

以下是我如何处理包含隐藏 ID、标签和输入字段的数据,所有数据都包含在字段集中。您可以使用实际的表格来概括它。

以下是为两组字段生成的 HTML 以及单击以添加行的链接:

   <fieldset>
<input type="hidden" name="data[VmfrDesignatedIncome][0][id]" value="668" id="VmfrDesignatedIncome0Id"/>
<div class="input text">
<label for="VmfrDesignatedIncome0Designation">Designation</label>
<input name="data[VmfrDesignatedIncome][0][designation]" value="blah" maxlength="512" type="text" id="VmfrDesignatedIncome0Designation"/></div>
</fieldset>

<fieldset>
<input type="hidden" name="data[VmfrDesignatedIncome][1][id]" value="669" id="VmfrDesignatedIncome1Id"/>
<div class="input text">
<label for="VmfrDesignatedIncome1Designation">Designation</label>

<input name="data[VmfrDesignatedIncome][1][designation]" value="blah2" maxlength="512" type="text" id="VmfrDesignatedIncome1Designation"/></div>
</fieldset>

<a href="#" id="addrow">Add row</a>

下面是 Javascript,它克隆页面上的最后一个字段集,然后修改 id、名称和字段值,将其中的数字加一。请注意,选择器必须使用 > 子选择器准确选择每个标签或字段。

/* As the strings to the function below may have [ or ]
** we want to stop it being treated as a regexp
*/
RegExp.quote = function(str) {
return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};

function findNumberAddOne(attributeString) {
/*Finds the number in the given string
** and returns a string with that number increased by one
*/
var re = new RegExp("(.*)([0-9])(.*)");
var nPlusOne = attributeString.replace(re, "$2")+"+1";
var newstr = attributeString.replace(re, "$1")+eval(nPlusOne)+attributeString.replace(re, "$3");
return newstr;
}

$(document).ready(function() {
/* Duplicate the last set of designated income fields and increase the relevants id/name etc.
** so that it works as a new row in the table when saved*/
$('#addrow').click(function() {
$('fieldset:last').clone().insertAfter('fieldset:last');
$('fieldset:last > input').attr('id',findNumberAddOne($('fieldset:last > input').attr('id')));
$('fieldset:last > input').attr('value',''); /*Blank out the actual value*/
$('fieldset:last > input').attr('name',findNumberAddOne($('fieldset:last > input').attr('name')));
$('fieldset:last > div > label').attr('for',findNumberAddOne($('fieldset:last > div > label').attr('for')));
$('fieldset:last > div > input').attr('id',findNumberAddOne($('fieldset:last > div > input').attr('id')));
$('fieldset:last > div > input').attr('value',''); /*Blank out the actual value*/
$('fieldset:last > div > input').attr('name',findNumberAddOne($('fieldset:last > div > input').attr('name')));
});
});

关于php - 动态添加表单字段行 - cakePHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com