我有一个页面,允许用户通过单击按钮在父表中添加子作业来显示表中的新行(以填写子作业的详细信息)。该行中的一个字段是由PHP脚本生成的引用号。问题是每行的参考编号是相同的。
我尝试使用JavaScript读取字段值,然后剥离数字并递增它然后将它们全部重新组合在一起,但这看起来有点过分。必须有一种更简单的方法。我也无法弄清楚如何让它将新值写入即将生成的行。
如果我可以提供帮助,我的老板不希望我写新功能,而是使用现有功能。我的想法是更新getNewJobRef()以传递当前值,以便它自动获取下一个值,但我不知道如何传递当前值,因为它在代码中列出如下。我也不确定代码中有多少其他地方都调用这个函数,所以我不想搞砸那些。
<button id="add-new-row" style="text-align:center;margin-top:5px;width:85px;" class="boxbutton">Add New</button>
和:
$(document.body).addEvent('click:relay(#add-new-row)', function (e, el) {
try{
e.preventDefault();
addChildJobRow();
}catch(e){
...
}
});
和:
function addChildJobRow() {
try {
lastrow++;
let cl = (lastrow % 2 ? 'odd' : 'even');
showHeaderRow();
Elements.from(connectedjobtemplate({
rownum: lastrow,
cl: cl,
nysid: '',
dinNum: '',
warrantNum: ''
})).inject($('linkedJobsBody'));
} catch (e) {
...
}
}
和:
<script id="connectedjobtemplate" type="text/template">
<tr id="childjobrow<%= rownum %>" class="<%= cl %>" data-row-id="<%= rownum %>">
<td colspan="7">
<table width="100%">
<tr>
<td style="width: 15%;">
<input data-row-id="<%= rownum %>" id="reference[<%= rownum %>]" name="childjobid[<%= rownum %>][reference]" value="<?php echo $this->MJob->getNewJobRef(232); ?>" class="ref" size="14" style="background-color: transparent; border: none;" />
</td>
</tr>
</table>
</td>
</tr>
</script>
PHP脚本是:
function getNewJobRef($jobtypeid) {
if (empty($jobtypeid)) {
return '';
}
$select = $this->db->query('select * from jobtypecounters where jobtypeid =?', array($jobtypeid));
$this->db->query('update jobtypecounters set counter = last_insert_id(counter) + 1 where jobtypeid = ?', array($jobtypeid));
// read from write server to avoid replica lag
$Q = $this->db->query('select concat(prefix, lpad(last_insert_id(), 5, "0")) as newjobref from jobtypecounters where jobtypeid = ?', array($jobtypeid));
if ($Q->num_rows() == 1)
return $Q->row()->newjobref;
else
return '';
}
任何帮助或指导将非常感谢。
编辑:该页面使用MooTools,所以我被告知我不能在同一页面上使用jQuery,因为它们会发生冲突。那么如何在没有页面刷新/ Ajax的情况下将变量传递回PHP脚本以将当前引用号传递回脚本。
我更新了我的代码如下:
function addChildJobRow() {
try {
lastrow++;
let cl = (lastrow % 2 ? 'odd' : 'even');
showHeaderRow();
var refNum = '<?php echo $this->MJob->getNewJobRef(232); ?>';
Elements.from(connectedjobtemplate({
rownum: lastrow,
cl: cl,
ref: refNum,
nysid: '',
dinNum: '',
warrantNum: ''
})).inject($('linkedJobsBody'));
} catch (e) {
console.log(e);
logevent({jobid: jid, event: 'add-new-row', uri: window.location.href, eventdata: e});
}
}
和:
<input data-row-id="<%= rownum %>" id="reference[<%= rownum %>]" name="childjobid[<%= rownum %>][reference]" value="<%= ref %>" class="ref" size="14" style="background-color: transparent; border: none;" />
function getNewJobRef($jobtypeid, $refNum = 0) {
//check $refNum
if (empty($jobtypeid)) {
return '';
}
$select = $this->db->query('select * from jobtypecounters where jobtypeid =?', array($jobtypeid));
$this->db->query('update jobtypecounters set counter = last_insert_id(counter) + 1 where jobtypeid = ?', array($jobtypeid));
$Q = $this->db->query('select concat(prefix, lpad(last_insert_id(), 5, "0")) as newjobref from jobtypecounters where jobtypeid = ?', array($jobtypeid));
if ($Q->num_rows() == 1)
return $Q->row()->newjobref;
else
return '';
}
我想做的是调用getNewJobRef函数,传递的第二个参数是当前引用号,但我不知道如何做到这一点。
编辑:这在理论上是有效的,但对于数据库方面的东西,必须调用函数,所以它回到绘图板。
我添加了一个检查以查看refNum是否已定义。如果没有,它调用PHP脚本并设置值;如果是,它将文本部分拆分,转换为整数并递增数字,然后将它们全部重新组合在一起。
这是我的代码:
if (typeof refNum === 'undefined') {
refNum = '<?php echo $this->MJob->getNewJobRef(232); ?>';
} else {
var num = refNum.replace('ABC', '').toInt();
num += 1;
refNum = 'ABC' + num;
}