JQuery EasyUI(36)

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

学习要点:

  1. 新增功能

 一、新增功能

DataGrid属性,扩展自Panel面板
属性名 说明
editors object 定义在编辑行的时候使用的编辑器。
列属性,在 columns里设置的属性
属性名 说明
editor string,object 指明编辑类型。当字符串指明编辑类型的时候,对象包含两个属性:type:字符串,可以使用的类型有:text,textarea,cheakbox,numberbox,validatebox,datebox,combobox,combotree。options:对象,object,该编辑器属性对应于编辑类型。
Editor(编辑器)
属性名 说明
 init container,options 初始化编辑器并返回目标对象。
destroy target 如果有必要销毁编辑器。
getValue target 从编辑器中获取值。
setValue target,value 向编辑器中写入值。
resize target,width 如果有必要调整编辑器的大小。
DataGrid方法
方法名 说明
appendRow row 追加一个新行,新行将被添加到最后的位置。
insertRow row 插入一个新行,参考包括以下属性:index:要插入的行索引,如果该索引值未定义,则追加新行。row:行数据
beginEdit index 开始编辑行。
endEdit index 结束编辑行。
rejectChanges none 回滚所有从创建或上一次调用acceptChange函数后更改的地址。
DataGrid事件
属性名 说明
onAfterEdit rowIndex,rowData,changes 在用户完成编辑一行的时候触发,参数包括:rowIndex:编辑行的索引,索引从0开始。rowData:对应于完成编辑的行的记录。changes:更改后的字段(键)/值对。

 

<!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">添加</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-deit" plain="true">修改</a>
          <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</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" οnclick="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:false,
       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:'[email protected]',
               data:'2014-11-11',
     });
      */

         if(!this.editRow){
}
        //添加一行
        $('#box').datagrid('insertRow',{
            index:0,
            row:{
               /*
               user:'bnbbs',
               email:'[email protected]',
               data:'2014-11-11', 
               */       
          }
      });

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

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

     $('#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:'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',
        onAfterEdit:function(rowIndex,rowData,changes){
         $('#save,#redo').hidden();
         obj.editRow = false;
          console.log(rowDate);
   }
});
[
  {
   "user":"蜡笔小新",
   "emial":"[email protected]",
   "date":"2014-10-01",
  },
  {
   "user":"樱桃小丸子",
   "emial":"[email protected]",
   "date":"2014-10-02",
  },
  {
   "user":"黑崎一护",
   "emial":"[email protected]",
   "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 user,email,data FROM think_user $sql ORDER BY $order $sort LIMIT $first,$pageSize") or die('SQL 错误!');
   $total = mysql_num_rows(mysql_query("SELECT 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/103137504

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章