您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JavaScript在yii框架中操的代码分享

2025/10/23 18:38:33发布16次查看
效果展示:
代码实现:
控制器
<?php 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\db\activerecord; use yii\data\pagination; use app\models\ecsgoods; class pagecontroller extends controller { //public $layout=false; //展示,分页 public function actionlist() { $query = ecsgoods::find(); $pagination = new pagination([ 'defaultpagesize' => 5, 'totalcount' => $query->count(), ]); $countries = $query ->offset($pagination->offset) ->limit($pagination->limit) ->all(); return $this->render('list', [ 'brr' => $countries, 'pagination' => $pagination, ]); } //即点即改 public function actionupdates() { $request = yii::$app->request; $id = $request->get('old_id'); $name = $request->get('old_val'); $connection=yii::$app->db; $command = $connection->createcommand("update ecs_goods set goods_name='$name' where goods_id='$id'"); $re=$command->execute(); if($re) { echo 1; } } //单删 public function actiondel() { $request = yii::$app->request; $id = $request->get('ids'); $connection=yii::$app->db; $command = $connection->createcommand("delete from ecs_goods where goods_id='$id'"); $re=$command->execute(); if($re) { echo 1; } else { echo 0; } } //批删 public function actionalldel() { $request = yii::$app->request; $id = $request->get('new_str'); $connection=yii::$app->db; $command = $connection->createcommand("delete from ecs_goods where goods_id in($id)"); $re=$command->execute(); if($re) { echo 1; } else { echo 0; } } } 视图层 [html] view plain copy 在code上查看代码片派生到我的代码片 <?php use yii\helpers\html; use yii\widgets\linkpager; ?> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <title>循环数据</title> </head> <style> table{ background-color: pink; } .dels{ border: 1px solid red; font-family:楷体; background-color: white; } .trs{ background-color: red; } </style> <body> <center> <p> <input type="checkbox" value="全选" onclick="check_qx(this);">全选 <input type="checkbox" value="全不选" onclick="check_bx(this);">全不选 <input type="checkbox" value="反选" onclick="check_fx();">反选 <input type="button" value="批量删除" onclick="check_del();"> </p> <table border="1"> <tr> <td><input type="checkbox" onclick="check_qx(this);"></td> <td>商品id</td> <td>商品名称</td> <td>操作</td> </tr> <?php foreach($brr as $val) { ?> <tr> <td><input type="checkbox" name="check[]" value="<?php echo $val['goods_id']?>"></td> <td><?php echo $val['goods_id']?></td> <td><span id="<?php echo $val['goods_id']?>"><?php echo $val['goods_name']?></span></td> <td><a href="javascript:void(0)" id="<?php echo $val['goods_id']?>" onclick="del(this);">删除</a></td> </tr> <?php } ?> </table> <?= linkpager::widget(['pagination' => $pagination]) ?> </center> </body> </html> <script src="jq.js"></script> <script type="text/javascript"> $(function(){ $(document).on('click','.num',function(){ var id=$(this).attr('id'); var _this=$(this); var new_val=$(this).html(); _this.parent().html("<input type='text' value="+new_val+" id="+id+">"); var inp=$('.asdf'); inp.focus(); inp.blur(function(){ var old_id=$(this).attr('id'); var old_val=$(this).val(); $.get("index.php?r=page/updates",{old_val:old_val,old_id:old_id},function(e){ if(e==1) { inp.parent().html("<span id="+old_id+">"+old_val+"</span>"); } else { inp.parent().html("<span id="+old_id+">"+new_val+"</span>"); } }) }) }) }) //单删 function del(obj) { var ids=obj.id; ajax=new xmlhttprequest(); ajax.onreadystatechange=function() { if(ajax.readystate==4) { //alert(ajax.responsetext); if(ajax.responsetext==1) { obj.parentnode.parentnode.parentnode.removechild(obj.parentnode.parentnode); } else { alert("删除失败"); } } } ajax.open("get","index.php?r=page/del&ids="+ids); ajax.send(null); } //全选 function check_qx(obj) { //alert(32432); var ids=document.getelementsbyname("check[]"); //alert(ids); if(obj.checked) { for(var i=0;i<ids.length;i++) { ids[i].checked=true; } } } //全不选 function check_bx(obj) { var ids=document.getelementsbyname("check[]"); if(obj.checked) { for(var i=0;i<ids.length;i++) { ids[i].checked=false; } } } //反选 function check_fx() { var ids=document.getelementsbyname("check[]"); for(var i=0;i<ids.length;i++) { ids[i].checked=!ids[i].checked; } } //批量删除 function check_del() { var ids=document.getelementsbyname("check[]"); var str=''; for(var i=0;i<ids.length;i++) { if(ids[i].checked) { str=str+','+ids[i].value; } } new_str=str.substr(1); ajax=new xmlhttprequest(); ajax.onreadystatechange=function() { if (ajax.readystate == 4) { //alert(ajax.responsetext); if(ajax.responsetext==1) { for(var j=ids.length-1;j>=0;j--) { if(ids[j].checked) { ids[j].parentnode.parentnode.parentnode.removechild(ids[j].parentnode.parentnode); } } } else { alert("删除失败"); } } } ajax.open("get","index.php?r=page/alldel&new_str="+new_str); ajax.send(null); } </script>
以上所述是小编给大家介绍的javascript中全选、全不选、反选、无刷新删除、批量删除、即点即改入库(在yii框架中操作)的代码分享
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product