开发者

CakePHP - how to edit in place using AJAX

I am trying to display contact information and allow edit in place. It is not updating the field. Basically, the div I want to use has (e.g.) an address, I click on it, it highlight and then an edit box appears, I edit, click ok, and the thing updates as success. Yet the data has not updated, which I can tell when I check the record.

Here are snippets of relevant code:


CONTROLLER:

var $helpers = array ('Html','Form','Ajax','Javascript','Menu');
var $components = array( 'RequestHandler');
var $name = 'People';

...

function edit_in_place() {
 if (!empty($this->data)) {
  if ($this->Person->save($this->data)) {
    $thisId=$this->Person->id;
    $this->header("Content-Type: application/json");
    ech开发者_StackOverflow中文版o 'success';
    exit;
  } else {
    return 'Fail';
  }
 }
 $this->Autorender = FALSE;
 exit;
}

VIEW THAT TRIGGERS EDIT_IN_PLACE:

<div id="addresswork">
 <?php echo $person['Person']['addresswork'] ?>
</div>

<?php
 echo $ajax->editor(
   "addresswork", array( 
      'controller'=>'People',
      'action'=>'edit_in_place',
       $person['Person']['id'] 
 ),
    array('highlightcolor'=>'#aaeeff')
 )
?>

I have tried finding good documentation on this and failed. Any suggestions?


Try setting the id before the save. Also, you're passing th ID in the params, so you need to add that:

function edit_in_place($person_id) { // notice param
    $this->header("Content-Type: application/json");
    $this->Person->id = $person_id; // set the id.
    $json = $this->Person->save($this->data) ? '{"r":"Pass"}' : '{"r":"Fail"}';
    $this->set('json', $json);
    // make a new view edit_in_place.ctp file that prints the variable.
}

Five things to note:

  1. You have to tell CakePHP which record you are trying to update. In your view, you are creating an action like /controller/edit_in_place/1 with 1 being the id. You have to access that id in the function as a parameter, and pass that to the model.

  2. You were echoing 'success' but just returning 'Fail'. This is probably why the action looked like it passed. Since autorender was false, and you weren't echoing anything in the fail condition, you were probably getting a 200 response code with nothing in the body.

  3. Why are you echoing from the controller? That's breaking the MVC design. If you are interested in doing it the CakePHP way, you should create a new view that echoes a variable, and set that variable to success or failure in the controller (more probably pass that variable a JSON string).

  4. You're telling the browser header that it's JSON, but you're not returning JSON. I've updated my example to show proper JSON encoded data. If you're going to do anything more complicated, you should use the PHP json_encode function.

  5. No need to check for empty on $this->data. If it is empty, the save will fail.


I got it working -- the problem was I wasn't getting data into the function in the controller as I expected. So I did a print_r to figure out what params were coming back. Here's what I did, although I'd be interested in any more comments on the correct way to do it:

function edit_in_place() {

    if ($this->RequestHandler->isAjax())
    {
        $this->Person->id = $this->params['named']['id'];
        $field= $this->params['form']['editorId'];
        $value= $this->params['form']['value'];

        $this->Person->saveField($field,$value);

        Configure::write('debug', 0);
        echo $value;
        exit;
    }

In the view that triggers this, I did this:

        <div id="addresswork">
        <?php echo $person['Person']['addresswork'] ?>
        </div>
        <?php
        echo $ajax->editor(
        "addresswork",
        array( 
            'controller'=>'People',
            'action'=>'edit_in_place',
            'id'=>$person['Person']['id']
            ),
        array(
            'submit'=>'OK',
            'style'=>'inherit',
            'submitdata' =>array('id'=>$person['Person']['id']),
            'tooltip'=>'Click this to edit',
            'highlightcolor'=>'#aaeeff')
        );
        ?>


I think your json is not right. You should make a view, include the javascript helper and do something like

$javascript->object('success');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜