2

我有一个帖子列表,我想使用多个复选框删除它们。我在 cake php 中关注了这个链接Multiple Check boxes 但我得到了这个错误(我使用 cakephp 2.4):

  1. 未找到 PostsController::deleteSelect() 的视图。

  2. 确认您已创建文件:C:\xampp\htdocs\cakephp2\app\View\Themed\Cakestrap\Posts\delete_select.ctp

我想从 index.ctp 而不是 delete_select.ctp 访问这些数据。我的问题是我如何访问这些数据“data['Post']['box'][]”?

我的代码是:

索引.ctp

<?php foreach ($posts as $post): ?>
<tr>
    <td><?php echo $post['Post']['id']; ?></td>
    <td>
        <?php echo $this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'])); ?>
    </td>
    <td>
        <?php echo $post['Post']['created']; ?>
    </td>

    <td>

    <?php echo $this->Form->checkbox('post', 
                                  array(
                                    'value' => $post['Post']['id'],
                                    'name' => "data['Post']['box'][]",
                                   ));?></td>

    <td>

        <?php echo $this->Form->postLink(
            'Delete',
            array('action' => 'delete', $post['Post']['id']),
            array('confirm' => 'Are you sure?'));
        ?>
        <?php echo $this->Html->link('Edit', array('action' => 'edit', $post['Post']['id'])); ?>

    </td>
</tr>
<?php endforeach; ?>
<p><?php echo $this->Html->link('deleteSelect', array('action' => 'deleteSelect')); ?></p>

删除选择功能

    public function deleteSelect(){
if(!empty($this->data)) {
    foreach($this->data['Post']['box'] as $key => $value){

            $this->Post->delete($value);
    }
    $this->redirect(array('action' => 'index'));
}

}
4

4 回答 4

1

如果您希望将某些数据传递给您的操作,则必须在表单中包含所有复选框。

但是您不能这样做,因为您正在使用Form::postLink它创建一个表单并且您不能将一个表单嵌套在另一个表单中。

所以你必须摆脱你的 postLinks。你确定你需要它们吗?它们不能都是简单的链接吗?

删除后链接后,您可以将所有代码放入一个大表单中

echo $this->Form->create('Post', array('action' => 'deleteSelect'));

// your foreach code here

echo $this->Form->end('Delete selected posts');

另外:在您的控制器中放入这段代码

$this->redirect(array('action' => 'index'));

if条件外

所以即使没有数据传递,页面也会被重定向(没有选中复选框)

于 2013-10-31T12:25:11.840 回答
0

没有发布数据

视图文件的最后一行是:

<p><?php 
echo $this->Html->link(
    'deleteSelect', 
    array('action' => 'deleteSelect')
); 
?></p>

除非有一些 javascript 监听点击 - 那只是一个链接,而不是提交表单的东西,这意味着没有表单数据。鉴于此,相关的控制器操作不会输入适当的 if 并因此尝试呈现视图文件:

public function deleteSelect(){
    if(!empty($this->data)) {
        ...
        // Unreachable
        $this->redirect(array('action' => 'index'));
    }

}

为了防止问题中提到的问题 - 简单地不要使重定向依赖于表单数据的存在:

public function deleteSelect(){
    if(!empty($this->data)) {
        ...
    }

    // Always executed
    $this->redirect(array('action' => 'index'));
}

但这不会解决所写的主要问题,它只会什么都不做。

输入需要采用一种形式

对于做任何事情的输入(忽略 javascript 的使用),它们需要在一个表单中。因此,原始 html 需要更改为:

<table>
    ...
    <input type="checkbox">
    ...
    <input type="checkbox">
    ...
    <input type="checkbox">
</table>
<p>
    <a href="/deleteSelect" ...>deleteSelect</a>
</p>

至:

<form action="/deleteSelect" ...>
    <table>
        ...
        <input type="checkbox">
        ...
        <input type="checkbox">
        ...
        <input type="checkbox">
    </table>

    <input type="submit" value="deleteSelect">
</form>

IE:

  • 将表格包裹在表格中
  • 定义表单操作以转到相应的功能
  • 该链接必须更改为提交按钮。

这样,就可以达到预期的效果。

警告 - 嵌套表格

知道将表单放在另一个表单中是无效的。因此,要使“多重删除”功能使用普通表单工作,将需要删除单个删除按钮,因为它们也是嵌入式表单。使用的另一种技术postLink是使它们成为普通链接并使用简单的 javascript 处理程序来防止通过 get 提交,例如:

$('a.delete').click(function(e) {
    if (confirm('Sure?')) {
        $.post(this.attr('href')});
    }
    return false;
});
于 2013-10-31T13:28:02.447 回答
0

我已经在我的代码中实现了它,只需查看我的代码即可。

      <div>


     <?php //echo $this->Form->create();

   echo $this->Form->create(null, array(
  'url' => array('controller' => 'manageParcels', 'action' => 'deleteParcel')
  ));

 ?>

  <table width="100%" border="1">
   <tr>
        <th></th>

         <th>Parcel Number</th>
         <th>Consignment Number </th>
         <th>Customer Name </th>
         <th>Customer Address </th>
         <th>Customer Phone-Number </th>
          <th>Customer EmailId </th>
            </tr>
        <?php foreach($parcelDatas as $parcelData){

            ?> 


        <tr>

       <td><input type="checkbox" name ="status[]" value="<?php echo   
        $parcelData['ManageParcel']['id'];  ?>"></input></td>


        <td align='center'><?php echo $this->html->link($parcelData['ManageParcel']
     ['parcelNo'], array('action' => 'editParcel',$parcelData['ManageParcel']['id']), 
      array('escape' => false));?> </td>
    <td align='center'><?php echo $parcelData['ManageParcel']['ConNo']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusName']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusAddress']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusPhone']; ?></td>
<td align='center'><?php echo $parcelData['ManageParcel']['cusEmail']; ?></td>

        </tr>
        <?php 
        }?>
        </table>
         <?php 

         echo $this->Form->end(__('Delete Parcel')); ?>

    </div>




 **My controller code**

       public function deleteParcel()
   {
    $this->autoRender=FALSE;
    if ($this->request->is('post'))
    {
        if(empty($this->request->data))

        {
            $this->Session->setFlash(__('Please select parcel '));
            return $this->redirect(
            array('controller' => 'ManageParcels', 'action' =>   
                            'listParcel')
            );
        }
        $deleteParcels=$this->request->data['status'];
        $size=sizeof($deleteParcels);
        foreach ($deleteParcels as $deleteParcel)
        {

            $this->ManageParcel->id = $deleteParcel;

            $parcelData=$this->ManageParcel->findById($deleteParcel);


            if ($this->ManageParcel->delete()) {
            $this->recordActivity('deleteParcel','Parcel 
                   Number '.$parcelData['ManageParcel']['parcelNo'] . ' deleted' );
            $this->Session->setFlash(__('Parcel data deleted'));

            }
    else {
    $this->Session->setFlash(__('Parcel data was  not Deleted'));
        return $this->redirect(array('action' => 'listParcel'));
            }

        }
    $this->recordActivity('deleteParcel',$size.' Parcels data deleted ');
        return $this->redirect(
        array('controller' => 'ManageParcels', 'action' => 'listParcel')
        );

    }


 }
于 2014-04-11T10:02:48.243 回答
0

如前所述,CakePHP 要求,如果您在控制器中调用函数,则必须有一个相应的 .ctp 文件。

为了解决这个问题,您可以$this->autoRender = false;在您的方法中使用。

于 2013-10-31T11:16:52.770 回答