Still banging on this row problem. How do I get the content of an element
from my controller so that I can echo it to the view? Is the completely
wrong approach? What is the correct way to achieve this? What is it exactly
that CakePHP wants me to do?

At this point I am ready to give up on CakePHP and just write a function in
my controller that returns the HTML I need, the old fashioned way because I
would be done in about 10 minutes. However that sort of defeats the purpose
of using CakePHP.


CONTROLLER EDIT ACTION

        function edit()
        {
                $this->pageTitle = 'Edit Page';

                
                if($this->RequestHandler->isAjax())
                {
                        // HOW DO I ECHO THE CONTENT OF THE ROW ELEMENT
INSTEAD OF THE ECHO BELOW?
                        // THERE HAS TO BE A DRY WAY TO DO THIS.
                        
                        echo '<tr id="'.$_POST['row_count'].'"><td>Col1 ::
Row '.$_POST['row_count'].'</td><td>Col2 :: Row
'.$_POST['row_count'].'</td></tr>';
                        $this->autoRender = false; 
                }

                // Handle the edit stuff
                
        }



EDIT VIEW

<table id="the_table">
<?php 
        echo $this->renderElement('row', array('row_count' => 0));
?>
</table>

<div id="the_button">Click</div>


ROW ELEMENT

<tr id="<?php echo $row_count; ?>">
        <td>Col 1 :: Row <?php echo $row_count; ?></th>
        <td>Col 2 :: Row <?php echo $row_count; ?></th>
</tr>


JAVASCRIPT

$(document).ready(function()
{
        $('#the_button').click(function()
        {
                $.ajax({
                        url: 'edit/',
                        type: 'POST',
                        data: 'row_count=' +
document.getElementById('the_table').rows.length,
                        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: Sunday, January 04, 2009 3:01 AM
To: [email protected]
Subject: Re: Adding table row with AJAX


On Sat, Jan 3, 2009 at 11:15 PM, Steven Wright <[email protected]>
wrote:
>
> Actually this works in place of the dummy file
>
> $this->autoRender = false;

Yes, that's correct. The reason (AFAIK) that RequestHandler doesn't suppress
that automatically is that one should use a .ctp file for the output. You're
better off setting a variable in your controller and handing it off to a
view:

$this->set('row_count', $row_count);
$this->set('the_content', $the_content); $this->render('the_view_ajax');

the_view_ajax:
<tr id="row<?= $row_count ?>"><td><?= $the_content ?></td></tr>

This keeps HTML cruft out of your methods, which is always a Very Good
Thing.

I think RequestHandler might use a view named, 'the_view' + '_ajax' if it
finds it, so you might not need the render() call.

I'm also pretty sure it takes care of setting $layout = 'ajax', btw.
Try removing that line and see if everything still works.

(yeah, I'm too lazy to go check that. What do you expect from someone who
uses short tags? ;-)



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to