CakePHP——如何使用AJAX进行编辑。

时间:2022-12-20 13:42:47

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.

我正在尝试显示联系信息和允许编辑到位。它没有更新字段。基本上,我想要使用的div有(例如)一个地址,我点击它,它高亮显示,然后一个编辑框出现,我编辑,点击确定,并且更新成功。但是数据没有更新,我可以在检查记录的时候告诉你。

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");
    echo 'success';
    exit;
  } else {
    return 'Fail';
  }
 }
 $this->Autorender = FALSE;
 exit;
}

VIEW THAT TRIGGERS EDIT_IN_PLACE:

视图,触发器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?

我试图找到关于这方面的好的文档,但失败了。有什么建议吗?

3 个解决方案

#1


2  

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

尝试在保存之前设置id。另外,您正在传递params中的ID,因此需要添加:

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:

5注意事项:

  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.

    您必须告诉CakePHP要更新哪个记录。在您的视图中,您正在创建一个操作,比如/controller/edit_in_place/1,其中1是id,您必须以参数的形式访问函数中的id,并将其传递给模型。

  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.

    你是在重复“成功”,而只是在重复“失败”。这可能就是为什么这次行动看起来是通过了。由于autorender是错误的,并且您在失败的情况下没有响应任何东西,您可能得到了一个200个响应代码,而没有在正文中。

  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).

    你为什么要从控制器中返回?这打破了MVC设计。如果您对CakePHP方法感兴趣,您应该创建一个新的视图来响应一个变量,并将该变量设置为控制器中的成功或失败(更可能是将该变量传递为JSON字符串)。

  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.

    你告诉浏览器头它是JSON,但你没有返回JSON。我更新了示例,以显示正确的JSON编码数据。如果要做更复杂的事情,应该使用PHP json_encode函数。

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

    不需要在$this->数据上检查是否为空。如果它是空的,保存将失败。

#2


1  

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:

我得到了它的工作——问题是我没有像我期望的那样把数据输入到控制器中。我做了一个print_r来计算返回的参数。以下是我所做的,尽管我想知道更多关于正确方法的评论:

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')
        );
        ?>

#3


0  

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

我认为你的json是不对的。您应该创建一个视图,包括javascript助手并执行类似的操作

$javascript->object('success');

#1


2  

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

尝试在保存之前设置id。另外,您正在传递params中的ID,因此需要添加:

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:

5注意事项:

  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.

    您必须告诉CakePHP要更新哪个记录。在您的视图中,您正在创建一个操作,比如/controller/edit_in_place/1,其中1是id,您必须以参数的形式访问函数中的id,并将其传递给模型。

  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.

    你是在重复“成功”,而只是在重复“失败”。这可能就是为什么这次行动看起来是通过了。由于autorender是错误的,并且您在失败的情况下没有响应任何东西,您可能得到了一个200个响应代码,而没有在正文中。

  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).

    你为什么要从控制器中返回?这打破了MVC设计。如果您对CakePHP方法感兴趣,您应该创建一个新的视图来响应一个变量,并将该变量设置为控制器中的成功或失败(更可能是将该变量传递为JSON字符串)。

  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.

    你告诉浏览器头它是JSON,但你没有返回JSON。我更新了示例,以显示正确的JSON编码数据。如果要做更复杂的事情,应该使用PHP json_encode函数。

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

    不需要在$this->数据上检查是否为空。如果它是空的,保存将失败。

#2


1  

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:

我得到了它的工作——问题是我没有像我期望的那样把数据输入到控制器中。我做了一个print_r来计算返回的参数。以下是我所做的,尽管我想知道更多关于正确方法的评论:

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')
        );
        ?>

#3


0  

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

我认为你的json是不对的。您应该创建一个视图,包括javascript助手并执行类似的操作

$javascript->object('success');