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