nick2price
09-26-2012, 10:19 PM
Hi guys. Just wanted to start a new thread as I have most things working (besides one thing), but mainly I want to try and clean things up because I think I am over doing things. If you have seen my previous threads, you will know I have a table
<table class="table1">
<tr>
<td class="title">1:</td>
<td><input type="text" placeholder="Giving" class="selector" name="giving2" autocomplete="off"/></td>
<td><input type="text" placeholder="Getting" class="selector" name="getting2" autocomplete="off"/></td>
<td><input type="text" name="popupdate2" id="popupdate2" class="popupdate" value="When" /></td>
</tr>
<tr>
<td class="title">2:</td>
<td><input type="text" placeholder="Giving" class="selector" name="giving2" autocomplete="off"/></td>
<td><input type="text" placeholder="Getting" class="selector" name="getting2" autocomplete="off"/></td>
<td><input type="text" name="popupdate2" id="popupdate2" class="popupdate" value="When" /></td>
</tr>
</table>
I then have a couple of methods to add and remove rows to this table
var stdContent = "<td class='tabletitle'>xx:</td>\n" +
"<td><input type='text' placeholder='Giving' class='giving_selector' name='giving1[]' autocomplete='off'/></td>\n" +
"<td><input type='text' placeholder='Getting' class='giving_selector' name='getting1[]' autocomplete='off'/></td>\n" +
"<td><input type='text' name='popupdate2[]' id='popupdatexx' class='popupdatepicker' value='when' /></td>\n";
var newno = 5;
function addTableRow() {
if(newno>20){
alert("Max row limit");
return;
}
var newrow = document.createElement('tr');
var theTable = document.getElementById('table1');
var tbody = theTable.getElementsByTagName("tbody")[0];
tbody.appendChild(newrow);
var newcontent = stdContent.replace('xx', newno);
newrow.innerHTML = newcontent;
newno++;
$("input.giving_selector").autocomplete({
source: 'http://www.example.com/form_ajax.php?function=giving_selector',
minLength: 2
});
$('.popupdatepicker').datepicker({
numberOfMonths: 2,
showButtonPanel: false,
dateFormat: 'dd/mm/yy',
minDate: 0,
maxDate: '+2Y'
});
}
Now I kind of cheated and added the giving_selector and datepicker into this function, but it seems to work (although I dont think it is the best way). Now somewhere else in my code, I have another datepicker thing, which is used for the fields in the original table.
$('.popupdatepicker').datepicker({
onSelect: function() {
addTableRow();
},
numberOfMonths: 2,
showButtonPanel: false,
dateFormat: 'dd/mm/yy',
minDate: 0,
maxDate: '+2Y'
});
I added an onSelect function to this, because my aim was to get it to add a new row automatically to the table whenever the last date is selected. So, initially there are 2 rows in the table, if the date in the second row is selected, the event should fire and add a new row. If two new rows are then added, the fourth calandar will be the only one which can now fire this event. The way I have it at the moment, it adds a new row on every date, because I have no idea how to do it only to the date in the last row.
So really, I am just looking for advise on this, and generally on the code as a whole and what I can do to make it better. I come from a java background where everything is very OO, and to me, I feel my code at the moment is really messy, and I really want to tidy it up.
Cheers
Nick
<table class="table1">
<tr>
<td class="title">1:</td>
<td><input type="text" placeholder="Giving" class="selector" name="giving2" autocomplete="off"/></td>
<td><input type="text" placeholder="Getting" class="selector" name="getting2" autocomplete="off"/></td>
<td><input type="text" name="popupdate2" id="popupdate2" class="popupdate" value="When" /></td>
</tr>
<tr>
<td class="title">2:</td>
<td><input type="text" placeholder="Giving" class="selector" name="giving2" autocomplete="off"/></td>
<td><input type="text" placeholder="Getting" class="selector" name="getting2" autocomplete="off"/></td>
<td><input type="text" name="popupdate2" id="popupdate2" class="popupdate" value="When" /></td>
</tr>
</table>
I then have a couple of methods to add and remove rows to this table
var stdContent = "<td class='tabletitle'>xx:</td>\n" +
"<td><input type='text' placeholder='Giving' class='giving_selector' name='giving1[]' autocomplete='off'/></td>\n" +
"<td><input type='text' placeholder='Getting' class='giving_selector' name='getting1[]' autocomplete='off'/></td>\n" +
"<td><input type='text' name='popupdate2[]' id='popupdatexx' class='popupdatepicker' value='when' /></td>\n";
var newno = 5;
function addTableRow() {
if(newno>20){
alert("Max row limit");
return;
}
var newrow = document.createElement('tr');
var theTable = document.getElementById('table1');
var tbody = theTable.getElementsByTagName("tbody")[0];
tbody.appendChild(newrow);
var newcontent = stdContent.replace('xx', newno);
newrow.innerHTML = newcontent;
newno++;
$("input.giving_selector").autocomplete({
source: 'http://www.example.com/form_ajax.php?function=giving_selector',
minLength: 2
});
$('.popupdatepicker').datepicker({
numberOfMonths: 2,
showButtonPanel: false,
dateFormat: 'dd/mm/yy',
minDate: 0,
maxDate: '+2Y'
});
}
Now I kind of cheated and added the giving_selector and datepicker into this function, but it seems to work (although I dont think it is the best way). Now somewhere else in my code, I have another datepicker thing, which is used for the fields in the original table.
$('.popupdatepicker').datepicker({
onSelect: function() {
addTableRow();
},
numberOfMonths: 2,
showButtonPanel: false,
dateFormat: 'dd/mm/yy',
minDate: 0,
maxDate: '+2Y'
});
I added an onSelect function to this, because my aim was to get it to add a new row automatically to the table whenever the last date is selected. So, initially there are 2 rows in the table, if the date in the second row is selected, the event should fire and add a new row. If two new rows are then added, the fourth calandar will be the only one which can now fire this event. The way I have it at the moment, it adds a new row on every date, because I have no idea how to do it only to the date in the last row.
So really, I am just looking for advise on this, and generally on the code as a whole and what I can do to make it better. I come from a java background where everything is very OO, and to me, I feel my code at the moment is really messy, and I really want to tidy it up.
Cheers
Nick