Thanks for the reply. This is mostly working but there is something I am
still missing.
I tried two different things.
1) If I call the 'edit' action I get a jQuery error ( 'div is null' ' elem =
jQuery.makeArray( div.childNodes ); ')
I am not sure what div its looking for. The second problem with that is that
although I can see the table row returned at the start of the response
string the rest of the content for the view is also returned and I don't
know why.
2) If I call the 'add_ingredient_row' action I only get the content for the
view in the response but no new row.
So two questions:
1) Why is CakePHP returning all the content for the view?
2) Why is jQuery giving me that error?
Thanks again. If I can just get over this hurdle I will be off and running.
VIEW
<h2>Testing AJAX with CakePHP and Prototype and Scriptaculous</h2>
<table id="the_table">
<tr id="row1">
<td>Row1</td>
</tr>
</table>
<div id="mydiv">
This is a div
</div>
<div id="the_button">Click</div>
CONTROLLER
class AjaxtestsController extends AppController
{
var $components = array( 'RequestHandler' );
function index()
{
$this->pageTitle = 'Index Page';
}
function view()
{
$this->pageTitle = 'View Page';
}
function edit()
{
$this->pageTitle = 'Edit Page';
if($this->RequestHandler->isAjax())
{
$this->layout = 'ajax';
echo '<tr><td>This is a row</td></tr>';
}
}
function add_ingredient_row()
{
echo '<tr><td>This is a row</td></tr>';
}
}
JAVASCRIPT
$(document).ready(function()
{
$('#the_button').click(function()
{
$.ajax({
url: 'add_ingredient_row/',
type: 'GET',
data: 'data string',
dataType: 'html',
timeout: 2000,
error: function(XMLHttpRequest, textStatus,
errorThrown)
{
// ...
alert('There has been an error');
},
success: function(msg)
{
//alert(msg);
$('#the_table').append(msg);
}
}); // ends ajax
}); // ends click
}); // ends document.ready
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf
Of brian
Sent: Friday, January 02, 2009 8:26 PM
To: [email protected]
Subject: Re: Adding table row with AJAX
On Fri, Jan 2, 2009 at 7:45 PM, Steven Wright <[email protected]>
wrote:
>
> Hi Adam,
> Thanks for writing back. How would you get the data back for the row
> from CakePHP?
>
> My row contains four columns with the following inputs:
>
> amount [text]
> measurement_type [select]
> description [text]
> ingredient [select]
>
> This is normally rendered from an element as a table row. I would like
> the output of that element appended to my table. So on the button
> click I call the action add_ingredient_row and it should return the
> content of the element. How do I get that content appended to my table.
>
> I can do this without CakePHP. But for me this exercise is to use the
> framework.
>
Being a jQuery user, I never bother with Cake's ajax() stuff. But jQuery is
dead simple (mostly) to use, so I don't mind having to write out a few lines
of JS. To do what you describe, I'd do something like
this:
$(document).ready()
{
$('#the_button').click(function()
{
$.ajax({
url: the_href,
type: 'GET',
data: the_data,
dataType: 'html',
timeout: 2000,
error: function(XMLHttpRequest, textStatus,
errorThrown)
{
// ...
},
success: function(msg)
{
/* assuming your controller is returning
'<tr>...</tr>'
*/
$('#the_table').append(msg);
}
});
});
});
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---