JavaScript--全选、全不选、反选、无刷新删除、批量删除、即点即改入库(在yii框架中操作)

效果展示:


代码实现:

控制器

<?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;
        }
    }
}

视图层

<?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="全选" οnclick="check_qx(this);">全选
    <input type="checkbox" value="全不选" οnclick="check_bx(this);">全不选
    <input type="checkbox" value="反选" οnclick="check_fx();">反选
	<input type="button" value="批量删除" οnclick="check_del();">
</p>		
	<table border="1">
		<tr class="trs">
		<td><input type="checkbox" οnclick="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 class='num' 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']?>" οnclick="del(this);" class="dels">删除</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' class='asdf' 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 class='num' id="+old_id+">"+old_val+"</span>");  
                }  
                else  
                {    
                    inp.parent().html("<span class='num' 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>  
好的这就可以了!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值