您的位置:首页 > Web前端 > JavaScript

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

2013-08-28 15:48 441 查看
批量删除

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:;" onclick="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 面向过程实现

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP javascript
相关文章推荐