JQuery EasyUI(37)

                     第三十二章: DataGrid(数据表格)组件[6]

学习要点:

  1. 修改删除功能

 一、修改删除功能

列属性,在columns里设置的属性
属性名说明
checkboxboolean如果为true,则显示复选框,该复选框列固定宽度。
 DataGrid方法
属性名说明
getSelectionsnone返回所有被选中的行,当没有记录被选中的时候将返回一个空数据。
getRowIndexrow获取到当前行的索引。
unselectRowindex取消指定选择的行。
DataGrid事件
属性名说明
onDblClinkRowrowindex,rowData在用户双击一行的时候触发,参数包括:rowIndex:点击行的索引值,该索引值从0开始。rowDate:对应于点击行的记录。

 

<!DOCTYPE html>
<html>
  <head>
    <title>JQuery Easy UI</title>
    <meta charset="utf-8"/>
    <script type="text/javascript" src="easyui/jquery.min.js"></script>
    <script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>     
    <script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>     
    <script type="text/javascript" src="js/index.js"></script> 
    <link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css"/>
    <link rel="stylesheet" type="text/css" href="easyui/themes/icon.css"/>
    <style>
       .textbox{
          height:200px;
          margin:0;
          padding:0 2px;
          box-sizing:content-box;
       }
    </style>              
</head>
  <body>
     <table id="box"></table>
     <div id="tb" style="padding:5px;">
        <div style="margin-buttom:5px;">
          <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="obj.add();">添加</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-deit" plain="true" onclick="obj.edit();">修改</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="obj.remove();">删除</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" style="display:none" id="save">保存</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-redo" plain="true" style="display:none" id="redo">取消编辑</a>
        </div>
        <div style="padding:0 0 0 7px;color='#ccc'">
           查询账号:<input type="text" name="user" class="textbox" style="width:100px">
           创建时间从:<input type="text" name="date_from" class="easyui-datebox" editable="false" style="width:100px"> 
           到:<input type="text" name="date_to" class="easyui-datebox" editable="false" style="width:100px">
           <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="obj.search();">查询</a>
        </div>
     </div> 
  </body>
</html>
//扩展dateTimeBox
$.extend($.fn.datagrid.defaults.editors,{
    datetimebox:{
        init:function(container,options){
           var input = $('<input type="text">').appendTo(container);
           options.editable = false;
           input.datetimebox(options);
           return input;
         },
        getValue:function(target){
            return $(target).datetimebox('getValue');
         },
        setValue:function(target){
           $(target).datetimebox('setValue',value);
         },
        resize:function(target){
           $(target).datetimebox('reSize',width);
         },
        destory:function(target){
           $(target).datetimebox('destory');           
         },
    }
});

$(function(){
      
      obj = {
       editRow:undefined,
       search:function(){
            $('#box').datagrid('load',{
            user:$.trim($('input[name="user"]').val()),
            date_from:$('input[name="date_from"]').val(),
            date_to:$('input[name="date_to"]').val();
       });
      },
       add:function(){
        $('#save,#redo').show();

       /*
         //当前页行结尾添加
        $('#box').datagrid('appendRow',{
               user:'bnbbs',
               email:'bnbbs@163.com',
               data:'2014-11-11',
     });
      */

         if(this.editRow == undefined){

        //添加一行
        $('#box').datagrid('insertRow',{
            index:0,
            row:{
               /*
               user:'bnbbs',
               email:'bnbbs@163.com',
               data:'2014-11-11', 
               */       
          }
      });

         //将第一行设置为可编辑状态。
         $('#box').datagrid('beginEdit',0);
         this.editRow = 0;     
       }
    },

     save:function(){
        //这两句不应该放这里,应该是保存成功之后在执行。
        //$('#save,#redo').hidden();
        //$('#box').dategrid('rejectChanges');
        //将第一行设置为结束编辑状态。
         $('#box').datagrid('endEdit',this.editRow);        
     },

     redo:function(){
        $('#save,#redo').hidden();
        $('#box').dategrid('rejectChanges');
        this.editRow = undefined;
     },

     edit:function(){
        var rows = $('#box').datagrid('getSelections');
        if(rows.length == 1){

          if(this.editRow != undefined){
             $('#box').datagrid('endEdit',this.editRow);
           }

          if(this.editRow == undefined){
             var index = $('#box').datagrid('getRowIndex',rows[0]);
             $('#save,#redo').show();
             $('#box').datagrid('beginEdit',index);
             this.editRow = index;
             $('#box').datagrid('unselectRow',index);             
           } 

         }else{
           $.messager.alert('警告','修改必须或只能选择一行','warning');
         }    
     },

     remove:function(){
        var rows = $('#box').datagrid('getSelections');
        if(rows.length > 0){
          $.messager.infirm('确定操作','你正在要删除所选的记录吗?',function(flag){
             if(flag){
               var ids = [];
               for(var i= 0;i<rows.length;i++){
                   ids.push(rows[i].id);
                }
                 console.log(ids.join(','));
              }
           });  
        }else{
          $.messager.alert('提示','请选择要删除的记录!','info');
       }
     },     
   };

     $('#box').datagrid({
        width:600,
        //url:'content.json',
        url:'user.php',
        title:'用户列表',
        iconCls:'icon-search',
        striped:true,
        nowrap:true,
        rownumbers:true,
        //singleSelect:true, 
        fitColumns:true,     
        columns:[[
             {
               field:'id',
               title:'编号',
               sortable:true,
               width:100,
               checkbox:true,
             },
             {
               field:'user',
               title:'账号',
               sortable:true,
               width:100, 
               editor:{
                 type:'validatebox',
                 options:{
                      required:true,
                  },
                },             
             },
             {
               field:'email',
               title:'邮箱',
               sortable:true, 
               width:100,
               editor:{
                 type:'validatebox',
                 options:{
                      required:true,
                      validType:'email',
                  },
                },                
             },
             {
               field:'date',
               title:'注册时间',
               sortable:true,
               width:100,
               editor:{
                 type:'datebox',
                 options:{
                      required:true,
                  },
                }, 
  
             },
      ]],
        toolbar:'#tb',        
        pagination:true,
        pageSize:10,
        pageList:[10,20,30],
        pageNumber:1,
        pagePosition:'top',
        pagePosition:'both',
        pagePosition:'bottom',
        sortName:'date',
        sortOrder:'DESC',

        onDblClickRow:function(rowIdex,rowData){

         if(obj.editRow != undefined){
          $('#box').datagrid('endEdit',obj.editRow);
        }

         if(obj.editRow == undefined){
          $('#save,#redo').show();
          $('#box').datagrid('beginEdit',rowIndex);
          obj.editRow = rowIndex;
  }         
},
        onAfterEdit:function(rowIndex,rowData,changes){
         $('#save,#redo').hidden();
         obj.editRow = undefined;
          console.log(rowDate);
   },
});
[
  {
   "user":"蜡笔小新",
   "emial":"xiaoxin@163.com",
   "date":"2014-10-01",
  },
  {
   "user":"樱桃小丸子",
   "emial":"xiaowanzi@163.com",
   "date":"2014-10-02",
  },
  {
   "user":"黑崎一护",
   "emial":"yihu@163.com",
   "date":"2014-10-03",
  },
]
<?php
   header('Content-Type:text/html;chartset=utf-8');
   define('DB_HOST','localhost');
   define('DB_USER','root');
   define('DB_PWD','123456');
   define('DB_NAME','thinkphp');    

  $conn = @mysql_connect(DB_HOST,DB_USER,DB_PWD)or die('数据库连接失败:'.mysql_error());

  @mysql_select_db(DB_NAME)or die('数据库错误:'.mysql_error());

  @mysql_query('SET NMAES UTF8')or die('字符集错误:'.mysql_error());
?>
<?php
   require 'config.hph';

   $page = $_POST['page'];
   $pageSize = $_POST['row'];
   $first = $pageSize * ($page - 1);

   $order = $_POST['order'];
   $sort = $_POST['sort'];
   
   $sql = '';
   $user = '';

   $datefrom = '';
   $dateto = '';

   if(isset($_POST['user']) && !empty($_POST['user'])){
    $user = "user LINK '%{$_POST['user']}%' AND ";
    $sql .= $user;
  }

   if(isset($_POST['$datefrom']) && !empty($_POST['$datefrom'])){
    $datefrom = "date >='{$_POST['$datefrom']}' AND ";
    $sql .= $datefrom;
  }


   if(isset($_POST['$dateto']) && !empty($_POST['$dateto'])){
    $dateto = "date <='{$_POST['$dateto']}' AND ";
    $sql .= $dateto;
  }


   if(!empty($sql)){
     sql = 'WHERE '.substr($sql,0,-4);
   }


   $query = mysql_query("SELECT id,user,email,data FROM think_user $sql ORDER BY $order $sort LIMIT $first,$pageSize") or die('SQL 错误!');
   $total = mysql_num_rows(mysql_query("SELECT id,user,email,data FROM think_user $sql"));
   
   $json ='';

   while (!!$row = mysql_fetch_array($query,MYSQL_ASSOC)){
      $json .= json_encode($row).',';
    }
     
   
   echo '{"total":'.$total.',"rows":['.$json.'],"footer":[{"user":"统计","email":"统计","date":"统计"}]}'; 

   $json  = substr($json,0,-1);


   mysql_close();
?>

 

 

作者:Roger_CoderLife

链接:https://blog.csdn.net/Roger_CoderLife/article/details/103236644

本文根据网易云课堂JQuery EasyUI视频教程翻译成文档,转载请注明原文出处,欢迎转载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值