控制器層中
namespace frontend\controllers; use frontend\models\Topic; use Yii;
use yii\data\Pagination;
public function actionTopic_show() { //獲取此班級的評論 $request=Yii::$app->request; $cid=$request->get('cid'); // $topic=new Topic(); // $arr=$topic->show_topic($cid); $topic=new Topic(); $data = $topic->find()->andWhere(['cid'=>$cid]); //這寫要顯示的數據 //實例化分頁類,帶上參數(總條數,每頁顯示條數) $pages = new Pagination(['totalCount' =>$data->count(),'pageSize'=>'4']); // var_dump($arr);die; $model = $data->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('topic_show',['arr'=>$data,'m'=>$model,'pages'=>$pages]); } //批量刪除 public function actionAll_del() { $request=Yii::$app->request; $tid=$request->get('str'); //傳入model批刪 $topic=new Topic(); $res=$topic->all_del($tid); var_dump($res); }
模型層中
使用gii創建就可以了
<?PHP
public function all_del($tid) { return $this->deleteAll("tid in($tid)"); }
視圖層中
<?PHP use yii\widgets\LinkPager; //使用分頁 use yii\widgets\ActiveForm; use yii\helpers\Url; use yii\helpers\Html; ?> <script src="jquery-1.9.1.min.js"></script> <table> <tr> <th>標題</th> <th>作者</th> <th>時間</th> <th>內容</th> </tr> <?php foreach($m as $key=>$value): ?> <tr> <td><input type="checkbox" name="box1[]" value="<?= $value['tid'] ?>"></td> <td><?= $value['title'] ?></td> <td><?= $value['t_name'] ?></td> <td><?= date("Y-m-d H:i:s",$value['time']) ?></td> <td><?= $value['content'] ?></td> </tr> <?php endforeach; ?> <tr> <td><button id="all_true">全選</button></td> <td><button id="all_false">全不選</button></td> <td><button id="all_delete">批刪</button></td> </tr> <script> //全選 $(function(){ //全選 $('#all_true').click(function () { $("input[name='box1[]']").each(function () { $(this).prop('checked',true); }) }) //全不選 $('#all_false').click(function () { $("input[name='box1[]']").each(function () { $(this).prop('checked',false); }) }) //批刪 $('#all_delete').click(function () { var str=''; //取到選中的值並移除 $("input[name='box1[]']:checked").each(function () { str+=','+$(this).val(); $(this).parent().parent().remove(); }) str=str.substr(1); // alert(str); //傳入後臺批量刪除 var url="http://localhost/day10.21/frontend/web/index.php?r=show/all_del" $.get(url,{str:str}) }) }) </script> </table> <?= LinkPager::widget([ 'pagination' => $pages, 'nextPageLabel'=>'下一頁', 'prevPageLabel'=>'上一頁', 'lastPageLabel'=>'尾頁', 'firstPageLabel'=>'首頁', ]); ?>