dsfdsfds521521 2019-03-27 08:09
浏览 40
已采纳

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

I am very new to MooTools and somewhat new to JavaScript.

I am trying to get a new row to show up on a button click. I have copied the code that another programmer did and adapted it to suit my needs (as far as I understand and on the instructions of my boss), but it is not working.

The click event registers, but when I debugged the code yesterday in the browser, I got a message that rownum is undefined, but today I don't get that error (but it still isn't working).

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

Nothing happens when I click the Add New button. I have put in alerts to show that it is going to the right function, so I know it's getting there. I don't understand the error that rownum is undefined at eval because I checked the other programmer's code and it's identical in that respect. I don't want to start messing with the JavaScript file as I am very new to MooTools.

Thanks in advance for any help.

  • 写回答

1条回答 默认 最新

  • dongzhan9100 2019-04-08 11:40
    关注

    Here is my final code that works. It turns out I had to pass several variables in or it wouldn't work.

    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);
                    }
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值