by prettyscripts on 2009-06-30 23:08:44 • Leave a comment »
append a table row should sound straight forward when using with a xajax function.
the html code for table and a button within a form:
Code:
<table> | |
<th><td>header1</td><td>header2</td></th> | |
<tbody id="tbody_id"> | |
</tbody> | |
</table> | |
<form id="form_id"> | |
// some input fields... | |
<input type="button" onClick="xajax_add_row(xajax.getFormValues('form_id'));" /> | |
</form> |
xajax function to append a row to table:
PHP:
function add_row($data) { | |
$obj = new xajaxResponse(); | |
$obj->append('tbody_id', 'innerHTML', | |
'<tr><td>' . $data['input1'] . '</td><td>' . $data['input2'] . '</td></tr>'); | |
return $obj; | |
} |
it works perfectly in firefox. but nothing is happening when you click the button in IE.
i wasted a lot of time trying to find out why. finally found this forum post:
For whatever reason, the W3C deemed it "incorrect" to write to the innerHTML of a TABLE, TBODY or TR (among other tags)... the odd thing is that IE is one of the only browsers that currently FOLLOWS the standard...
thankfully there’s a solution (based on the solution provided from the same post). instead assigning table cells to the <tbody> tag, create <tr> and <td> tags and assign to innerHTML of <td>.
in the form, add a hidden field for next row id.
now update the xajax function:
PHP:
function add_row($data) { | |
$obj = new xajaxResponse(); | |
$id = $data['next']; // hidden field with id="next" | |
| |
// add <tr> | |
$obj->create('tbody_id', 'tr', 'row_' . $id); | |
// add <td> | |
$obj->create('row_' . $id, 'td', 'td_row_' . $id . '_input1'); | |
$obj->create('row_' . $id, 'td', 'td_row_' . $id . '_input2'); | |
| |
// assign values to <td> | |
$obj->assign('td_row_' . $id . '_input1', 'innerHTML', $data['input1']); | |
$obj->assign('td_row_' . $id . '_input2', 'innerHTML', $data['input2']); | |
| |
// increment value for next row | |
$obj->assign('next', 'value', $id + 1); | |
return $obj; | |
} |