使用Mootools将模板注入现有表,但是在eval中未定义rownum

问题描述 投票:0回答:1

我是MooTools的新手,也是JavaScript的新手。

我试图通过点击按钮显示一个新行。我复制了另一个程序员所做的代码,并根据我的需要进行了调整(根据我的理解和我老板的指示),但它没有用。

click事件注册,但是当我昨天在浏览器中调试代码时,我得到一条消息,rownum未定义,但今天我没有得到该错误(但它仍然无效)。

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

// This is what should be injected on button click
<script id="connectedjobtemplate" type="text/plain">
    <tr id="childjobrow<%= rownum %>" class="<%= cl %>" data-row-id="<%= rownum %>">
        <td>
            <input class="childjobid" type="hidden" id="childjobid<%= rownum %>" name="childjobid[<%= rownum %>][transcribername]" value="0" />
            <input data-row-id="<%= rownum %>" id="assignee[<%= rownum %>]" name="childjobid[<%= rownum %>][transcribername]" class="" style="width:140px;" list="transcribers" autocomplete="off" value="" placeholder="Name" />
            <input type="hidden" id="transcriberid[<%= rownum %>]" name="childjobid[<%= rownum %>][transcriberid]" value="" />
        </td>
        <td>
            <input data-row-id="<%= rownum %>" id="refNum[<%= rownum %>]" name="childjobid[<%= rownum %>][refNum]" value="<%= refNum %>" class="" style="" />
        </td>
        <td>
            <input data-row-id="<%= rownum %>" id="pages[<%= rownum %>]" name="childjobid[<%= rownum %>][pages]" value="<%= pages %>" class="" style="" />
        </td>
        <td>
            <input data-row-id="<%= rownum %>" id="orderDate[<%= rownum %>]" name="childjobid[<%= rownum %>][orderDate]" value="<%= orderDate %>" class="" style="" />
        </td>
        <td>
            <input data-row-id="<%= rownum %>" id="nysid[<%= rownum %>]" name="childjobid[<%= rownum %>][nysid]" value="<%= nysid %>" class="" style="" />
        </td>
        <td>
            <input data-row-id="<%= rownum %>" id="dinNum[<%= rownum %>]" name="childjobid[<%= rownum %>][dinNum]" value="<%= dinNum %>" class="" style="" />
        </td>
        <td>
            <input data-row-id="<%= rownum %>" id="warrantNum[<%= rownum %>]" name="childjobid[<%= rownum %>][warrantNum]" value="<%= warrantNum %>" class="" style="" />
        </td>
    </tr>
</script>

<script type="text/javascript" src="<?php echo base_url(); ?>js/underscore-1.8.3-min.js"></script>

<script type="text/javascript">
// VARIABLES
var lastrow = 0;

// TEMPLATE
var rowtemplate = _.template(document.getElementById('connectedjobtemplate').innerHTML);

window.addEvent('load', function() {
    if ($('linkedJobsBody').getChildren().length == 0) {
        addChildJobRow();
    }
});

$(document.body).addEvent('click:relay(#add-new-row)', function (e, el) {
    try{
        e.preventDefault();
        addChildJobRow();
    }catch(e){
        console.log(e);
        logevent({jobid: jid,event:'click#add-new-row',uri:window.location.href, eventdata: e});
    }
});

function addChildJobRow() { 
    try {
        lastrow++;
        cl = (lastrow % 2 ? 'odd' : 'even');
        Elements.from(rowtemplate({
            rownum: lastrow,
            cl: cl
        })).inject($('linkedJobsBody'));
    } catch (e) {
        console.log(e);
        logevent({jobid: jid, event: 'add-new-row', uri: window.location.href, eventdata: e});
    }
}

单击“添加新”按钮时没有任何反应。我已经发出警报,表明它的功能正常,所以我知道它已经到了那里。我不明白在eval中rownum未定义的错误,因为我检查了其他程序员的代码并且在这方面它是相同的。我不想开始搞乱JavaScript文件,因为我对MooTools很新。

在此先感谢您的帮助。

javascript php mootools
1个回答
0
投票

这是我的最终代码。事实证明我必须传递几个变量,否则它将无法工作。

function addChildJobRow() {
                try {
                    lastrow++;
                    let cl = (lastrow % 2 ? 'odd' : 'even');
                    var refNum = '<?php echo $this->MJob->getNewJobRef(232); ?>';
                    Elements.from(connectedjobtemplate({
                        rownum: lastrow,
                        cl: cl,
                        ref: refNum,
                        nysid: '',
                        dinNum: '',
                        warrantNum: ''
                    })).inject($('newChildJobTable'));
                } catch (e) {
                    console.log(e);
                }
            }
© www.soinside.com 2019 - 2024. All rights reserved.