js实现批量删除,编辑操作

12 篇文章 0 订阅

批量删除

1. js

 <script type="text/javascript" src="../static/js/jQuery1.8.2.js"></script>
<script type="text/javascript">
    $(function () {
        $('#batch_action').change(function() {
            var act = $(this).val();
            if(act != '') {
                $('#act').val(act);
                $('#form1').submit();        
            }
        })
        
        $('#selectAll').click(function(){
            var is_check = $(this).attr('checked');
            if(is_check=='checked')
            {
                $(".listTable input:checkbox").attr('checked',true);
            }
            else
            {
                $(".listTable input:checkbox").attr('checked',false);
            }
        })
        
           
    })

</script>

2. 界面示例

 <form id="form1" action="index.php" method="post">
      <input id="act" type="hidden" name="act" value="" />

      <table class="listTable">
          <tr>
             <th><input id="selectAll" type="checkbox" title="全选" /></th>
             <th>id</th>
             <th>标题</th>
             <th>分类</th>
             <th>创建时间</th>
             <th>更新时间</th>
             <th>操作</th>
          </tr>
          <?php
          if(!empty($articles))
          {
            foreach($articles as $key=>$val){
          ?>
          <tr>
             <td><input name="sid[<?php echo $val['id'];?>]" type="checkbox" /></td>
             <td><?php echo $val['id'];?></td>
             <td><?php echo $val['title'];?></td>
             <td><?php echo $val['cate_name'];?></td>
             <td><?php echo date('Y-m-d H:i:s',$val['add_time']);?></td>
             <td><?php echo date('Y-m-d H:i:s',$val['update_time']);?></td>
             <td>
             <a href="../view.php?id=<?php echo $val['id'];?>" target="new">预览</a>
             <a href="index.php?act=edit&id=<?php echo $val['id'];?>">编辑</a>|
            <a href="javascript:remove(<?php echo $val['id'];?>)">删除</a></td>
          </tr>
          <?php
            }
          }else{
          ?>
          <tr><td colspan="6" align="center">没有数据</td></tr>
           <?php
            }
          ?>
       </table>
       </form>
        <div>
            <select id="batch_action">
                <option value="">请选择</option>
                <option value="batch_del">批量删除</option>
                <option value="batch_edit">批量编辑</option>
            </select>

        </div>
     </div>

 </div>

3. 数据库实现  act='batch_del'

  if(!empty($_POST['sid']))
    {
        foreach($_POST['sid'] as $key => $val)
        {
            $sid[] = $key;
        }
    }
    else
    {
        show_msg('请求值为空','index.php?act=list');
        exit;
    }
    
    if(batchDelete()) {
        show_msg('删除成功','index.php?act=list');
    }else{
        show_msg('程序异常','index.php?act=trash');
    }


调用的函数batchDelete()

/* 批量删除 */
function batchDelete() {
    foreach($_POST['sid'] as $key => $val) {
        $sid[] = $key;
    }
    $sid = implode(',',$sid);
    $sql = "UPDATE `article` SET `status` = -1 WHERE id IN(".$sid.")";
    return mysql_query($sql);    
}

批量编辑(批量编辑实现的是对选中的记录的字段进行统一修改修改成一致的)

1.js同上

2.页面示例同上

3.数据库操作

   得到要修改的记录的id   act="batch_edit"

     if(!empty($_POST['sid']))
    {
        foreach($_POST['sid'] as $key => $val)
        {
            $sid[] = $key;
        }
    }
    else
    {
        show_msg('请求值为空','index.php?act=list');
        exit;
    }

   $sid = implode(',',$sid);
   include template('batch_edit.php');//进入的页面时batch_edit.php

4.batch_edit.php页面主要内容示例

   <form action="index.php?act=do_batch_edit" method="post" onSubmit="return true;">
        <input type="hidden" name="ids" value="<?php echo $sid;?>">
        <table class="formTable">
            <tr>
                <td><label>分类</label></td>
                <td><select name="cate_id">
                        <option value="0">请选择分类</option>
                        <?php echo get_cate_option();?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label>作者</label></td>
                <td><input name="author" type="text" size="10"  value=""></td>
            </tr>
            <tr>
                <td><label>来源</label></td>
                <td><input name="source" type="text" size="30" value=""></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="提交"></td>
            </tr>
        </table>
        </form>

5.batch_edit.php页面提交后执行的后台代码  act="do_batch_edit"

  if(empty($_POST['ids'])) {
        show_msg('请求数据出错','index.php?act=list');
        exit;
    }
    
    if(batchUpdateArticle()) {
        
        show_msg('编辑完成','index.php?act=list');
    }else{
        show_msg('程序异常','index.php?act=list');
    }


  batchUpdateArticle()函数

/* 批量更新 */
function batchUpdateArticle() {
    $ids = $_POST['ids'];
    $cate_id = intval($_POST['cate_id']);
    $author = intval($_POST['author']);
    $source = intval($_POST['source']);
    $now_time = time();
    $update = "";
    if($cate_id != 0 ) {
        $update .= " cate_id = '$cate_id', ";
    }
    
    if(!empty($author)) {
        $update .= " author = '$author', ";    
    }
    if(!empty($source)) {
        $update .= " source = '$source', ";    
    }
    if(empty($update)) {
        show_msg('可编辑项目为空','index.php?act=list');
    }
    $sql = "UPDATE `article` SET ".$update." update_time = '$now_time' WHERE `id` IN($ids)";
    return mysql_query($sql);    
}


扩展:

show_msg函数

function show_msg($msg,$link = 'window',$sec=3000)
{
    if(empty($link))
    {
        $link = '<a href="javascript:;" οnclick="window.history.go(-1)">返回</a>';
    }
    $link = '<a href="'.$link.'">返回</a>';
    echo '<!doctype html>'.
         '<html>'.
         '<head>'.
         '<title>信息提示</title>'.
         '</head>>'.
         '<body>'.
         '<p align="center" style=" margin-top:100px;">'.$msg.'</p>'.
         '<p align="center" style=" margin-top:20px;">'.$link.'</p>'.
         '</body>'.
         '</html>';
        
}

template函数

function template($tplname)
{
    return TPL_ROOT.$tplname;
}

注释:该程序是在php 面向过程实现

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值