简体   繁体   English

如何在动态显示的网页中增加生成的值

[英]How to increment a generated value in a dynamically-displayed webpage

I have a page that allows the user to add child jobs to the parent job by clicking a button to display a new row in a table (to fill in the details of the child job). 我有一个页面,允许用户通过单击按钮在父表中添加子作业来显示表中的新行(以填写子作业的详细信息)。 One of the fields in that row is a reference number that's generated by a PHP script. 该行中的一个字段是由PHP脚本生成的引用号。 The problem is that the reference number is the same in every row. 问题是每行的参考编号是相同的。

I tried reading the field value using JavaScript and then stripping out the number and incrementing it and then putting it all back together, but that seemed like overkill. 我尝试使用JavaScript读取字段值,然后剥离数字并递增它然后将它们全部重新组合在一起,但这看起来有点过分。 There must be a simpler way of doing this. 必须有一种更简单的方法。 I also couldn't figure out how to get it to write the new value to the row that's about to be generated. 我也无法弄清楚如何让它将新值写入即将生成的行。

My boss doesn't want me to write new functions if I can help it, but rather to use existing functions. 如果我可以提供帮助,我的老板不希望我写新功能,而是使用现有功能。 My thought is to update the getNewJobRef() to pass it the current value so that it automatically gets the next value, but I am not sure how to pass the current value as it's laid out below in the code. 我的想法是更新getNewJobRef()以传递当前值,以便它自动获取下一个值,但我不知道如何传递当前值,因为它在代码中列出如下。 I am also not sure how many other places in the code call this function, so I don't want to mess those up. 我也不确定代码中有多少其他地方都调用这个函数,所以我不想搞砸那些。

<button id="add-new-row" style="text-align:center;margin-top:5px;width:85px;" class="boxbutton">Add New</button>

And: 和:

$(document.body).addEvent('click:relay(#add-new-row)', function (e, el) {
                try{
                    e.preventDefault();
                    addChildJobRow();
                }catch(e){
                    ...
                }
            });

And: 和:

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) {
                    ...
                }
            }

And: 和:

<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>

The PHP script is: 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 '';
}

Any help or guidance would be very much appreciated. 任何帮助或指导将非常感谢。

EDIT: the page uses MooTools, so I was told that I cannot use jQuery on the same page because they conflict. 编辑:该页面使用MooTools,所以我被告知我不能在同一页面上使用jQuery,因为它们会发生冲突。 So how do I pass the variable back to the PHP script without a page refresh/Ajax to get the current reference number to be passed back to the script. 那么如何在没有页面刷新/ Ajax的情况下将变量传递回PHP脚本以将当前引用号传递回脚本。

I have updated my code as follows: 我更新了我的代码如下:

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});
                }
            }

And: 和:

<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 '';
}

What I would like to do is call the getNewJobRef function with the second parameter passed being the current reference number, but I have no clue how to do that. 我想做的是调用getNewJobRef函数,传递的第二个参数是当前引用号,但我不知道如何做到这一点。

EDIT: This works in theory, but for the database side of things, the function has to be called, so it's back to the drawing board. 编辑:这在理论上是有效的,但对于数据库方面的东西,必须调用函数,所以它回到绘图板。

I added a check to see if the refNum had been defined yet. 我添加了一个检查以查看refNum是否已定义。 If not, it called the PHP script and set the value; 如果没有,它调用PHP脚本并设置值; if yes, it split out the text part, converted to integer and incremented the number and then put it all back together. 如果是,它将文本部分拆分,转换为整数并递增数字,然后将它们全部重新组合在一起。

\n

This is my code: 这是我的代码:

if (typeof refNum === 'undefined') {
   refNum = '<?php echo $this->MJob->getNewJobRef(232); ?>';
} else {
   var num = refNum.replace('ABC', '').toInt();
   num += 1;
   refNum = 'ABC' + num;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM