<?php
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\widgets\ActiveForm;
$this->registerJsFile("jquery.js",['position' => \yii\web\View::POS_HEAD]);
?>
<table class="table">
<tr>
<td><input type="checkbox" id="checkAll"/></td>
<td>id</td>
<td>商品名称</td>
<td>操作</td>
</tr>
<?php foreach($data as $k=>$v):?>
<tr>
<td>
<input type="checkbox" name="checkid" value=<?=$v['gid']?> >
</td>
<td><?=$v['gid']?></td>
<td><?=$v['gname']?></td>
<td><a href="#" class="del" ids=<?=$v['gid']?> >删除</a></td>
</tr>
<?php endforeach; ?>
</table>
<a href="#" id="delAll">批量删除</a>
<script src="../../web/jquery.js"></script>
<script>
$(function(){
$(document).on('click','#checkAll',function(){
//alert('1');
$(":checkbox[name='checkid']").attr('checked',true);
})
$(document).on('click','#delAll',function(){
var vals="";
$(":checkbox[name='checkid']").each(function(){
if($(this).prop("checked")==true){
vals += $(this).val()+",";
}
});
alert(vals);
var url="?r=weibo/show";
$.post(url,{ids:vals})
})
$(document).on('click',".del",function(){
var ids=$(this).attr('ids');
var url="?r=weibo/show";
$.post(url,{ids:ids});
$(this).parent().parent().remove();
})
})
</script>
2:controller
<?php
namespace frontend\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\db\Query;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
/**
* Site controller
*/
class WeiboController extends Controller
{
public function actionShow(){
$query=new Query();
$data=$query->from('goods')->all();
$request=Yii::$app->request;
if($request->isPost){
$ids=$request->post("ids");
$ids=trim($ids,",");
Yii::$app->db->createCommand()->delete("goods","gid in ($ids)")->execute();
//echo $ids;die;
// if($res){
// return $this->render('show',['data'=>$data]);
// }
}else{
return $this->render('show',['data'=>$data]);
}
}
}