问题:最近在优化一个项目时,前端用到了 easyui这个插件来实现表格,搞了很久,才实现出一部分功能,但是还是有很多地方不熟悉,故记录一下,以后再研究
第一个实例------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css" />
<script src="easyui/jquery-1.7.2.min.js"></script>
<script src="easyui/jquery.easyui.min.js"></script>
<script src="easyui/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<div id='grid-toolbar'>
<div class="criteria">
<span>
<a type="btn-save" class="easyui-linkbutton btn-blue" iconCls="icon-save">全部保存</a>
</span>
</div>
</div>
<table id="result_grid" class="easyui-datagrid" style="410px;height:250px" ,fitColumns:true,singleSelect:true">
<thead>
<tr>
<th data-options="field:'code',100">Code</th>
<th data-options="field:'name',100">Name</th>
<th data-options="field:'price',100,align:'right'">Price</th>
<th data-options="field:'cc',100,align:'right',editor:{type:'text'}">可编辑</th>
</tr>
</thead>
<tbody>
<tr>
<td>001</td>
<td>name1</td>
<td>2323</td>
<td>11</td>
</tr>
<tr>
<td>002</td>
<td>name2</td>
<td>4612</td>
<td>11</td>
</tr>
</tbody>
</table>
<script>
$('#result_grid').datagrid({
onClickRow: function(rowIndex, rowData) {
$("#result_grid").datagrid('selectRow', rowIndex);
$("#result_grid").datagrid('beginEdit', rowIndex); //设置可编辑状态
},
onBeforeEdit: function(index, row) {
editRow = row;
row.editing = true;
},
onAfterEdit: function(index, row, changes) {
$('#result_grid').datagrid('updateRow', {
index: index,
row: {
occupation: parseInt(row.occupation),
cause: row.cause
}
});
datagridMgr.addRow(row);
row.editing = false;
},
onCancelEdit: function(index, row) {
row.editing = false;
},
})
/**
* 数据管理对象
*/
var datagridMgr = {
dataList:[],
/**
* 查询数据
*/
queryData:function(){
var name=$('input[name="username"]').val();
var startTime=$('input[name="startTime"]').val();
var endTime=$('input[name="endTime"]').val();
$('#result_grid').datagrid('load',{
name: name,
startTime: startTime,
endTime:endTime
});
},
addRow:function(row){
this.dataList.push(row);
},
submitData:function(){
var json=JSON.stringify(this.dataList);
}
}
/**
* 查询事件
*/
$('a[type="btn-search"]').bind("click",function(){
datagridMgr.queryData();
});
/**
* 全部保存
*/
$('a[type="btn-save"]').bind("click",function(){
if(editRow){
datagridMgr.dataList.length=0;
var rows = $('#result_grid').datagrid("getSelections");
for(var i=0;i<rows.length;i++){
var index=$('#result_grid').datagrid('getRowIndex',rows[i]);
$('#result_grid').datagrid("endEdit", index);
}
var data=$('#result_grid').datagrid('getData');
$('#result_grid').datagrid('loadData',data);
editRow=null;
datagridMgr.submitData();
}else{
$.messager.alert('温馨提示','没有行需要保存');
}
});
</script>
</body>
</html>
第二个实例:----------------------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css" />
<link rel="stylesheet" type="text/css" href="easyui/texteditor.css" />
<script src="easyui/jquery.min.js"></script>
<script src="easyui/jquery.easyui.min.js"></script>
<script src="easyui/locale/easyui-lang-zh_CN.js"></script>
<script src="easyui/jquery.texteditor.js"></script>
<style>
.criteria{
margin-top: 10px;
margin-bottom: 10px;
}
.criteria > span{
display: inline-block;
20%;
}
.criteria label{
margin: 0px 10px;
}
.criteria span a{
margin: 0px 5px;
}
</style>
</head>
<body>
<div id='grid-toolbar'>
<div class="criteria">
<span><label>名称:</label><input type="text" class="easyui-textbox" name="username" ></span>
<span ><label>开始时间:</label><input type="text" class="easyui-datebox" name="startTime" id="startTime" data-options="editable:false"/></span>
<span><label>结束时间:</label><input type="text" class="easyui-datebox" name="endTime" id="endTime" data-options="editable:false"/></span>
<span>
<a type="btn-search" class="easyui-linkbutton btn-blue" iconCls="icon-search">查询</a>
<a type="btn-save" class="easyui-linkbutton btn-blue" iconCls="icon-save">全部保存</a>
</span>
</div>
</div>
<table id="result_grid"></table>
</body>
<script>
var editRow=null;
$('#result_grid').datagrid({
url:"new_file.json",
method: "get",
fitColumns:true,//宽度自适应
height: 280,
rownumbers:true,
nowrap:true,
pagination:true,
pageNumber:1,
pageSize:10,
pageList:[10,20,30],
onClickRow: function (rowIndex, rowData) {
$("#result_grid").datagrid('selectRow', rowIndex);
$("#result_grid").datagrid('beginEdit', rowIndex);//设置可编辑状态
},
onBeforeEdit:function(index,row){
editRow=row;
row.editing = true;
},
onAfterEdit:function(index, row, changes){
$('#result_grid').datagrid('updateRow',{
index: index,
row: {
occupation: parseInt(row.occupation),
cause: row.cause
}
});
datagridMgr.addRow(row);
row.editing = false;
},
onCancelEdit:function(index,row){
row.editing = false;
},
columns:[[
{field: 'id', checkbox:true,60},
{field:'name',title:'名字',150},
{field:'age',title:'年龄',150},
{field:'sex',title:'性别',150,formatter:function(value,row,index){
var result='';
switch(value){
case 0:
result='女';
break;
case 1:
result='男';
break;
}
return result;
}},
{field:'occupation',title:'职业(可编辑)',150,
formatter:function(value,row,index){
var result='';
switch(parseInt(value)){
case 1:
result='教师';
break;
case 2:
result='工程师';
break;
}
return result;
},
editor : {
type : 'combobox',
options : {
editable:false,
valueField:'code',
textField:'text',
url:"file:///C:/Users/86155/Documents/HBuilderProjects/easyui/new_file.json"
}
}
},
{field:'cause',title:'参赛原因(可编辑)',150,
editor : {
type : 'text'
}
},
{field:'creatTime',title:'报名时间',150}
]],
toolbar: '#grid-toolbar'
});
/**
* 数据管理对象
*/
var datagridMgr = {
dataList:[],
/**
* 查询数据
*/
queryData:function(){
var name=$('input[name="username"]').val();
var startTime=$('input[name="startTime"]').val();
var endTime=$('input[name="endTime"]').val();
$('#result_grid').datagrid('load',{
name: name,
startTime: startTime,
endTime:endTime
});
},
addRow:function(row){
this.dataList.push(row);
},
submitData:function(){
var json=JSON.stringify(this.dataList);
subimtJsonData(json);
}
}
/**
* 查询事件
*/
$('a[type="btn-search"]').bind("click",function(){
datagridMgr.queryData();
});
/**
* 全部保存
*/
$('a[type="btn-save"]').bind("click",function(){
if(editRow){
datagridMgr.dataList.length=0;
var rows = $('#result_grid').datagrid("getSelections");
for(var i=0;i<rows.length;i++){
var index=$('#result_grid').datagrid('getRowIndex',rows[i]);
$('#result_grid').datagrid("endEdit", index);
}
var data=$('#result_grid').datagrid('getData');
$('#result_grid').datagrid('loadData',data);
editRow=null;
datagridMgr.submitData();
}else{
$.messager.alert('温馨提示','没有行需要保存');
}
});
/**
* 提交数据
* @param {Object} json
*/
function subimtJsonData(json){
$.ajax({
type: "POST",
url:"file:///C:/Users/86155/Documents/HBuilderProjects/easyui/new_file.json",
dataType: "json",
data: {data:json},
success: function (data) {
$.messager.alert('温馨提示',data.message);
}
});
}
</script>
</html>
new_file.json文件:
[{"id":1,"name":"张三","sex":1,"age":"13","cc":11},{"id":1,"name":"张三","sex":1,"age":"13","cc":11},{"id":1,"name":"张三","sex":1,"age":"13","cc":11}]
备注:jQuery EasyUI官网
https://www.runoob.com/jeasyui/ext-edatagrid.html