namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\data\Pagination;
use app\models\Shop;
class LoginController extends Controller
{
public $enableCsrfValidation = false;
function actionIndex(){
//$test=new TestForm(); //实例化model模型
// $arr=$test->find();
$arr = Shop::find();
//$countQuery = clone $arr;
$pages = new Pagination([
//'totalCount' => $countQuery->count(),
'totalCount' => $arr->count(),
'pageSize' => 2 //每页显示条数
]);
$models = $arr->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('show', [
'models' => $models,
'pages' => $pages
]);
}
function actionDel(){
$id=\yii::$app->request->post('id');
// print_r($id);die;
$sql=\yii::$app->db->createCommand("delete from shop where u_id in ($id)")->execute();
if($sql){
return $this->redirect("?r=login/index");
}else{
return $this->redirect("?r=login/index");
}
}
}
v
<?php
use yii\widgets\LinkPager;
?>
<center>
<table border="1">
<tr>
<td>ID</td>
<td>名称</td>
<td>内容</td>
</tr>
<?php foreach($models as $k=>$v){ ?>
<tr id="list">
<td><input type="checkbox" class="everycb" name="run" value="<?php echo $v['u_id']?>"/><?php echo $v['u_id']?></span></td>
<td><?php echo $v['u_name']?></td>
<td><?php echo $v['u_son']?></td>
</tr>
<?php } ?>
<input type="checkbox" class="checkall2"/>全选
<input type="button"id="but" value="反选"/>
<input type="button" value="批量删除" id="btn3"/>
</table>
<?php echo LinkPager::widget([
'pagination' => $pages,
]);
?>
</center>
<script src="jquery.js"></script>
<script>
//全选
$(".checkall2").click(function(){
var isChecked = $(this).prop("checked");
$(".everycb").prop("checked",isChecked);
})
//反选
$("#but").click(function(){
$('input:checkbox').each(function(){
this.checked=!this.checked;
})
})
$("#btn3").click(function(){
var str='';
$("input[name='run']:checked").each(function(){
str+=$(this).val()+",";
})
var length=str.length-1;
var id=str.substr(0,length);
$.ajax({
type:"POST",
url:"?r=login/del",
data:{id:id},
success:function(result)
{
alert(result);
}
})
})
</script>