通过勾选复选框删除多行

时间:2014-11-11 06:54:00

标签: cakephp cakephp-2.3 delete-row

如何通过选中复选框删除多行? 我有一个记录表,我想一次删除多行。

这是我的删除选择功能:

已更新

 public function order() {


if(!empty($this->data)) {
        //If at lesst 1 check box is selected
        if (isset($this->data) && !empty($this->data['order_id'])) {
            $selectedReferences = $this->data['order_id'];

            $flagReferenceAdded = false;
            foreach ($selectedReferences as $singleReference) {
                //NEW DELETE
                $this->Order->id = $singleReference;
                $this->Order->delete();
                //NEW DELETE
            }
            //After delete
            $this->Session->setFlash(
                __('Your All record Are deleted.')
            );
            return $this->redirect(array(''));
        }      
        else{
            echo "Select at least 1 ORDER.";
        }  
}

在我看来。

  <?php
         echo $this->Form->create('Order',array('url' => array('controller' => 'admin', 'action' => 'order'))); ?>
        <table class="table table-bordered table-hover">

                    <th>Name</th>
                    <th>Email</th>
                    <th>phone</th>
                    <th>view</th>
                    <th>delete</th>
            <thead>
                </tr>
            </thead>
            <tbody>

                <?php foreach ($order as $orders): ?>
                <tr>
                  <td><?php echo $orders['Order']['varfullname']; ?></td>
                    <td><?php echo $orders['Order']['varemailid']; ?></td>
                    <td><?php echo $orders['Order']['varphone']; ?></td>
                     <td>
<?php echo $this->Html->link('',
array('controller' => 'orders', 'action' => 'view', $orders['Order']['id']), array('title'=>'VIEW','class' => 'fa fa-search'));?>

</td>
  <td><input type="checkbox" name="order_id[]" value ="<?php echo $orders['Order']['id'];?>" hiddenField = "false">
                </td>
                </tr>

                <?php endforeach; ?>

            </tbody>
        </table>

</br>

          <?
echo $this->Form->end('Deleteselected'); ?> 
    </div>
</div>
</br>

</div>
<!-- /.row -->

使用下面的答案我的删除选择工作正常 enter image description here

1 个答案:

答案 0 :(得分:2)

在ctp中使用

//在ctp中与EACH记录一起使用Checkbox集成。不要使用你使用的Checkbox代码。

<input type="checkbox" name="order_id[]" value ="<?php echo $referenceSingle['Order']['id'];?>" hiddenField = "false">

//在控制器中

if(!empty($this->data)) {
        //If at lesst 1 check box is selected
        if (isset($this->data) && !empty($this->data['order_id'])) {
            $selectedReferences = $this->data['order_id'];

            $flagReferenceAdded = false;
            foreach ($selectedReferences as $singleReference) {
                //NEW DELETE
                $this->Order->id = $singleReference;
                $this->Order->delete();
                //NEW DELETE
            }
            //After delete
            $this->Session->setFlash(
                __('Your All record Are deleted.')
            );
            return $this->redirect(array(''));
        }      
        else{
            echo "Select at least 1 ORDER.";
        }  
}

<强>更新

在Index方法本身中使用Above Code,不要创建deleteAll方法。

OR 让<form action删除所有

LIKE
    echo $this->Form->create('searchCourse',array('url' => array('controller' => 'Courses', 'action' => 'index')));

<强>更新 正确的工作流程请参考: enter image description here 在编码SIR之前,总是做文书工作和算法。