网页布局四之jQuery-easyUI的datagrid的基本应用

来源:互联网 发布:js获取另一个页面的id 编辑:程序博客网 时间:2024/05/21 08:38

在使用easyuI时,要先引入四个包,在此就不重复了。

一、要使用easyui的datagrid一定要先在<div>或<table>中引入class,例如:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <table id="dg" class="easyui-datagrid" title="我的DataGrid" style="width:500px;height:600px;"  
  2.             data-options=" loadMsg:'数据加载中,请稍后……',            rownumbers:false,singleSelect:true,url:'data/datagrid_data1.json',method:'get',  
  3.             toolbar:'#tb',onClickRow:onClickRow,pagination:true,pageSize:2">  
在此段代码中不仅有要使用的jquery-easyUi的类型,还有其格式,在style中定义。


二、easyui的datagrid读取的数据类型为json格式。而我没有写后台的controll代码,所以,就要自己写一个json格式的数据文件。然后用url:'xx/xx/data.json'来引入数据。


三、若想在页面上修改数据,就必须要写上

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. onClickRow:onClickRow。  
它是行点击属性。


四、要编辑数据,就要使用editor来定义文本类型。text为文本,number为数字,datebox为日期。如下代码:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <th data-options="field:'id',width:45,editor:{type:'numberbox',options:{precision:0}}">id</th>  
  2.                 <th data-options="field:'name',width:80,editor:'text'">name</th>  
  3.                 <th data-options="field:'sex',width:100,editor:'text'">sex</th>  
  4.                 <!--<th data-options="field:'listprice',width:80,align:'right'">List Price</th>-->  
  5.                 <th data-options="field:'age',width:80,align:'right',editor:'numberbox'">age</th>  
  6.                 <!--<th data-options="field:'attr1',width:240">Attribute</th>-->  
  7.                 <th data-options="field:'hobby',width:60,align:'center',editor:'text'">hobby</th>  
  8.                   
  9.                 <th data-options="field:'borthDate',width:100,editor:'datebox'">出生日期(月/日/年)</th>  
引入结果图:

最后为完整代码:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Mytables</title>  
  6.     <link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.5/themes/default/easyui.css">  
  7.     <link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.5/themes/icon.css">  
  8.     <!--<link rel="stylesheet" type="text/css" href="jquery-easyui-1.3.5/demo.css">-->  
  9.     <script type="text/javascript" src="jquery-easyui-1.3.5/jquery-1.4.2.min.js"></script>  
  10.     <script type="text/javascript" src="jquery-easyui-1.3.5/jquery.easyui.min.js"></script>  
  11. </head>  
  12. <body>  
  13. <div align="center">  
  14.     <h2>表单</h2>  
  15.   
  16.     <div style="margin:10px 0;"></div>  
  17. </div>  
  18.       
  19.     <div align="center">  
  20.     <table id="dg" class="easyui-datagrid" title="我的DataGrid" style="width:500px;height:600px;"  
  21.             data-options=" loadMsg:'数据加载中,请稍后……',           rownumbers:false,singleSelect:true,url:'data/datagrid_data1.json',method:'get',  
  22.             toolbar:'#tb',onClickRow:onClickRow,pagination:true,pageSize:2">  
  23.         <thead>  
  24.             <tr>  
  25.                 <th data-options="field:'id',width:45,editor:{type:'numberbox',options:{precision:0}}">id</th>  
  26.                 <th data-options="field:'name',width:80,editor:'text'">name</th>  
  27.                 <th data-options="field:'sex',width:100,editor:'text'">sex</th>  
  28.                 <!--<th data-options="field:'listprice',width:80,align:'right'">List Price</th>-->  
  29.                 <th data-options="field:'age',width:80,align:'right',editor:'numberbox'">age</th>  
  30.                 <!--<th data-options="field:'attr1',width:240">Attribute</th>-->  
  31.                 <th data-options="field:'hobby',width:60,align:'center',editor:'text'">hobby</th>  
  32.                   
  33.                 <th data-options="field:'borthDate',width:100,editor:'datebox'">出生日期(月/日/年)</th>  
  34.                 <tbody><input class="easyui-datebox"/></tbody>  
  35.             </tr>  
  36.         </thead>  
  37.     </table>  
  38.     </div>  
  39.       
  40.     <div id="tb" style="height:auto">  
  41.         <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onClick="append()">添加</a>  
  42.         <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onClick="removeit()">删除</a>  
  43.         <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onClick="accept()">保存</a>  
  44.         <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onClick="reject()">撤销</a>  
  45.     </div>  

[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <script type="text/javascript">  
  2.         var editIndex =undefined;  
  3.         function endEditing(){  
  4.             if (editIndex == undefined){return true}  
  5.             if ($('#dg').datagrid('validateRow', editIndex)){  
  6.                 var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});  
  7.                 //var productname = $(ed.target).combobox('getText');  
  8.                 //$('#dg').datagrid('getRows')[editIndex]['productname'] = productname;  
  9.                 $('#dg').datagrid('endEdit', editIndex);  
  10.                 editIndex = undefined;  
  11.                 return true;  
  12.                     } else {  
  13.                 return false;  
  14.                 }  
  15.             }  
  16.               
  17.             function onClickRow(index){  
  18.             if (editIndex != index){  
  19.                 if (endEditing()){  
  20.                     $('#dg').datagrid('selectRow', index).datagrid('beginEdit', index);  
  21.                     editIndex = index;  
  22.                 } else {  
  23.                     $('#dg').datagrid('selectRow', editIndex);  
  24.                 }  
  25.             }  
  26.         }  
  27.               
  28.         function append(){  
  29.                 if(endEditing()){  
  30.                     $('#dg').datagrid('appendRow',{id:1});  
  31.                     editIndex = $('dg').datagrid('getRows').length-1;  
  32.                     $('#dg').datagrid('selectRow',editIndex).datagrid('beginEdit',editIndex);  
  33.                     }  
  34.             }  
  35.               
  36.               
  37.             function removeit(){  
  38.                     if(editIndex == undefined){return;}  
  39.                     $('#dg').datagrid('cancelEdit',editIndex).datagrid('deleteRow',editIndex);  
  40.                     editIdex = undefined;  
  41.                 }  
  42.                   
  43.             function accept(){  
  44.                 if (endEditing()){  
  45.                 $('#dg').datagrid('acceptChanges');  
  46.                 }  
  47.             }  
  48.                   
  49.             function reject(){  
  50.                 $('#dg').datagrid('rejectChanges');  
  51.                 editIndex = undefined;  
  52.                 }  
  53.       
  54.         /*var toolbar = [{ 
  55.             text:'添加', 
  56.             iconCls:'icon-add', 
  57.             handler:function(){ 
  58.                  $('#add').window('open'); 
  59.                 $('#fs').show(); 
  60.                 $('#fs').form('clear'); 
  61.                 $('#fs').appendTo('#aa'); 
  62.                 //alert('add'); 
  63.                 } 
  64.             },{ 
  65.             text:'删除', 
  66.             iconCls:'icon-cut', 
  67.             handler:function(){ 
  68.                  
  69.                 alert('cut');} 
  70.             },{ 
  71.             text:'修改', 
  72.             iconCls:'icon-edit', 
  73.             handler:function(){alert('edit');} 
  74.             },{ 
  75.                 text:'保存', 
  76.                 iconCls:'icon-save', 
  77.                 handler:function(){alert('保存');} 
  78.          
  79.         }];**/  
  80.           
  81.           
  82.          function formatDatebox(value) {  
  83.         if (value == null || value == '') {  
  84.                 return '';  
  85.             }  
  86.         var dt;  
  87.         if (value instanceof Date) {  
  88.             dt = value;  
  89.             }  
  90.         else {  
  91.             dt = new Date(value);  
  92.             if (isNaN(dt)) {  
  93.                 value = value.replace(/\/Date(?\d+)\//, '$1'); //标红的这段是关键代码,将那个长字符串的日期值转换成正常的JS日期格式  
  94.                 dt = new Date();  
  95.                 dt.setTime(value);  
  96.                 }     
  97.             }  
  98.   
  99.     return dt.format("yyyy-MM-dd");   //这里用到一个javascript的Date类型的拓展方法,这个是自己添加的拓展方法,在后面的步骤3定义  
  100.     }  
  101.     </script>  

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. </body>  
  2. /html>