EasyUI DataGrid 基于 Ajax 自定义取值(loadData)

为 datagrid 加载数据分两种情况:
- 一种是基于 Ajax 请求获取数据然后通过"loadData"方法来赋值;
- 另一种是直接使用 datagrid 自带的"load"方法向服务端发起请求。
无论采取哪一种方式,通常建议将 datagrid 的定义(Init)和加载(Load)分作两个方法来撰写。
以下总结一下使用 Ajax 方法加载 datagrid 的使用过程。
一、纯JS本地赋值
这种方式没有 Ajax 异步请求,数据是来自于其他控件或者是在本地自行组织。
1、样式
<!-- 单页面样式 -->
<style type="text/css">
/* 当标签文本过长时,可灵活调整宽度 */
.SearchForm .grid_1 {
width: 18.333%;
}
.SearchForm .grid_2 {
width: auto !important;
}
#maintainForm .grid_2 {
/*width: 12.333%;*/
}
</style>
2、JSP 片段
<div id="rationPackageExceedDatagrid"></div>
<div id="rationPackageExceedDatagridToolbar" class="ToolbarArea ">
<!-- 操作区 按钮 -->
<%--<div class="OperationRow">--%>
<table cellpadding="0" cellspacing="0" style="width: auto;">
<tr>
<td>
<%--<a id="btnAddEditDatagrid" href="javascript:void(0)" class="easyui-linkbutton"
data-options="iconCls:'icon-add',plain:false">添加</a>--%>
</td>
<td>
<%--<div class="dialog-tool-separator"></div>--%>
</td>
</tr>
</table>
<%--</div>--%>
<!-- 查询区 表单 -->
<div class="container_12 SearchRow">
<form id="rationPackageExceedDatagridSearchForm" class="SearchForm" method="get">
<%--<div class="clear" title="换行标记"></div>--%>
<div class="grid_1 label">总超出额:</div>
<div class="grid_2 value">
<span id="lblRationExcessAmounts_rationPackageExceedDatagridSearchForm">¥0.00</span>
</div>
</form>
</div>
</div>
这里包含一个 datagrid 和一个与之配套的 toolbar。
3、js 初始化
可以注意,这个函数是以 Init- 打头。
function InitRationPackageExceedDatagrid() {
$rationPackageExceedDatagrid.datagrid({
title: '',
fit: true,
fitColumns: false, // 设置列固定宽度,true值表示自动填满整个横向空间
toolbar: "#rationPackageExceedDatagridToolbar",
idField: 'projectPartId',
frozenColumns: [[{
field: 'ck',
checkbox: true
}// 选择
]],
//
columns: [[
{field: 'projectPartName', title: '分项名称', width: 180, sortable: false},
{field: 'rationLimitedQuantity', title: '套餐限定数量', width: 100, sortable: false, align: 'right'},
{
field: 'actualQuantity', title: '实际所需数量', width: 100, sortable: false, align: 'right',
styler: function (value, row, index) {
// 当实际所需数量 大于 套餐限定数量,则采取“加粗标红”显示。
if (row.actualQuantity > row.rationLimitedQuantity) {
return "color:#AE1027;font-weight:bold;";
}
}
},
{
field: 'rationExcessQuantity', title: '超出数量', width: 100, sortable: false, align: 'right',
formatter: function (val) {
if (val != null) {
return Number(val).toFixed(2);
} else {
return val;
}
}
},
{
field: 'projectPartUnitPrice', title: '单价(元)', width: 90, sortable: false, align: 'right',
formatter: function (val) {
if (val != null) {
return '¥' + Number(val).toFixed(2);
} else {
return val;
}
}
},
{
field: 'rationExcessAmount', title: '超出额(元)', width: 100, sortable: false, align: 'right',
formatter: function (val) {
if (val != null) {
return '¥' + Number(val).toFixed(2);
} else {
return val;
}
}
}
]],
onLoadSuccess: function (data) {
console.info("rationPackageDatagrid onLoadSuccess.");
// 针对不同按钮个性化处理
//$(this).datagrid("clearChecked");
//$(this).datagrid("clearSelections");
},
onDblClickRow: function (rowIndex, rowData) {
console.info("rationPackageDatagrid onDblClickRow.");
},
onSelect: function (rowIndex, rowData) {
console.info('rationPackageDatagrid onSelect');
// 确保没有任何缓存痕迹(必不可少)
// 要点提示:在点击选中新的一行时,使其只勾选当前行,故先清除所有历史勾选项,让勾选项与选中项同步。
$(this).datagrid("clearChecked");
$(this).datagrid("checkRow", rowIndex);
}
}); // end rationPackageExceedDatagrid
}
4、赋值
通过 loadData 方法就可以直接赋值了,无论是给予一个空数组,还是一个有效的数组。要注意的是,在清空 datagrid 时,其对象值最好是这个格式:“{total: 0, rows: []}”
// 清空
$rationPackageExceedDatagrid.datagrid('loadData', {total: 0, rows: []});
$rationPackageExceedDatagrid.datagrid("clearChecked");
$rationPackageExceedDatagrid.datagrid("clearSelections");
var rationPackageExceedDataArray = [];
// 对数组进行赋值等处理...
// 定额套餐分项-超出额 datagrid
$rationPackageExceedDatagrid.datagrid('loadData', rationPackageExceedDataArray);
二、通过 Ajax 赋值(treegrid)
以下示例没有找到 datagrid,只有 treegrid,好在整体的操作几乎差不多,最终的赋值操作也相同。
1、JSP 片段
<div id="ProjectPartCategoryAndItemDatagrid" style="width:auto;"></div>
2、js 初始化
// 初始化表格
function initDataGrid() {
$datagrid.treegrid({
idField: 'projectPartId',
treeField: 'projectPartName',
singleSelect: false,
animate: true,
lines: true,
//toolbar: "#generalDatagridToolbar",
checkbox: true,
cascadeCheck: true,
fit: true,
fitColumns: false, // 设置列固定宽度,true值表示自动填满整个横向空间
frozenColumns: [[
{field: 'ck', checkbox: true},
{field: 'projectPartName', title: '工程分项', width: 230, sortable: false}
]],
columns: [[
{field: 'projectPartCode', title: '工程分项编码', width: 100, sortable: false}
]],
onLoadSuccess: function (row, data) {
console.info("ProjectPartCategoryAndItemDatagrid onLoadSuccess.");
$(".tooltipNote").tooltip({});
},
onClickRow: function (row) {
var idField = $(this).treegrid('options').idField;
console.info("clickRow," + idField + " = " + row[idField]);
console.info(row);
//级联选择
$(this).treegrid('cascadeCheck', {
id: row[idField], //节点ID
deepCascade: true //深度级联
});
},
onDblClickRow: function (row) {
console.info("ProjectPartCategoryAndItemDatagrid onDblClickRow.");
var idField = $(this).treegrid('options').idField;
var id = row[idField];
// 切换节点的 展开/折叠 状态
$datagrid.treegrid('toggle', id);
},
onContextMenu: function (e, row) {
console.info("ProjectPartCategoryAndItemDatagrid onContextMenu.");
//var idField = $(this).treegrid('options').idField;
//var id = row[idField];
//
//e.preventDefault();
//$(this).treegrid('select', id);
//
//$('#editMenu').menu('show', {
// left: e.pageX,
// top: e.pageY
//});
}
})
}
3、赋值
通过 jQuery 的 ajax() 方法获取到数据后,由 loadData 方法即可绑定数据。
// 加载数据
function loadDataGrid() {
console.info('加载表格 ProjectPartCategoryAndItemDatagrid');
$.ajax({
type: 'POST',
dataType: 'JSON',
url: UrlEnum.GetProjectPartCategoryAndItem,
//async: false, // 同步
data: {},
success: function (result) {
console.info("获取数据成功,返回的数据为:↓");
console.info(result);
if (result.success) {
console.info(result.data);
$datagrid.treegrid('loadData', result.data);
}
else {
if (result.operationType == operationTypeEnum.CookieTimeout) {
result.message = decodeURIComponent(result.message);
}
$.messager.alert('提示', result.message, 'warning');
}
}
});
}
EasyUI DataGrid 基于 Ajax 自定义取值(loadData)的更多相关文章
- hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法
1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...
- jquery easyui datagrid改变某行的值
$("#DeterminateMembers").datagrid("updateRow",{index:index,row:{fzr:"0" ...
- jquery easyui datagrid动态改变title的值
title:'<input type="text" id="txtTitle1" style="background:none;border:n ...
- jQuery EasyUI DataGrid Checkbox 数据设定与取值
纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...
- EasyUI datagrid checkbox数据设定与取值(转自http://blog.csdn.net/baronyang/article/dnetails/9323463,感谢分享,谢谢)
这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数据列的 Checkbox 设定为 Che ...
- JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列
转自 http://blog.csdn.net/tianlincao/article/details/7494467 前言 JQuery easyUi datagrid 中 使用datagrid生 ...
- JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率
大家好,又见面了. 在JAVA做前后端分离的项目开发的时候,服务端需要提供接口文档供周边人员做接口的对接指导.越来越多的项目都在尝试使用一些基于代码自动生成接口文档的工具来替代由开发人员手动编写接口文 ...
- Oracle存储过程-自定义数据类型,集合,遍历取值
摘要 Oracle存储过程,自定义数据类型,集合,遍历取值 目录[-] 0.前言 1.Packages 2.Packages bodies 3.输出结果 0.前言 在Oracle的存储过程中,可能会遇 ...
- 【freemaker】之自定义变量,特殊变量 globals ,循环对象取值
entity public class Employee { private Integer id; private String name; private Integer age; private ...
随机推荐
- 团队作业8——第二次项目冲刺(Beta阶段)5.27
1.当天站立式会议照片 会议内容: 本次会议为第七次会议 本次会议在陆大楼2楼召开,本次会议内容: ①:检查总结上次任务完成情况 ②:安排今天的分工 ③:对昨天的问题进行讨论 2. 每个人的工作 (有 ...
- 201521123039《Java程序设计》 第二周学习总结
1.本周学习总结 答:上课老师介绍了Java基本的数据类型,需要注意的地方有:**java的整型数都为带符号数**,**byte类型范围(-127,128)太小,所以我们一般不使用byte型,byte ...
- 201521123092《java程序设计》第二周学习总结
1. 本周学习总结 (1)学习了string的类型: (2)学习了java数组的使用: (3)学习了容器的概念: (4)解决一些pta编程时遇到的困难. 2. 书面作业 (1)使用Eclipse关联j ...
- 201521123013 《Java程序设计》第10周学习总结
1. 本章学习总结 2. 书面作业 Q1.finally题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? finally块中的异常必须在fina ...
- dup和dup2详解
C语言中dup和dup2函数的不同和使用 发表时间: 2012年11月15日 | 作者: 陈杰斌 | 所属分类: C语言 | 评论: 0 | 浏览: 1024 在unix高级编程中有介绍dup和dup ...
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
Spring与Shiro整合 导入jar包 shiro-web的jar. shiro-spring的jar shiro-code的jar 快速入门 shiro也通过filter进行拦截.filter拦 ...
- Linux第二篇【系统环境、常用命令、SSH连接、安装开发环境】
系统环境 我们知道Windows的出色就在于它的图形界面那一块,而Linux对图形界面的支持并不是那么友好-其实我们在Windows下对图形界面进行的操作都是得装换成命令的方式的! 当然了,我们在Ub ...
- mybatis-basedao的实现
package com.yangwei.shop.dao; import java.util.HashMap; import java.util.List; import java.util.Map; ...
- String的内存模型,为什么String被设计成不可变的
String是Java中最常用的类,是不可变的(Immutable), 那么String是如何实现Immutable呢,String为什么要设计成不可变呢? 前言 关于String,收集一波基础,来源 ...
- TCP/IP(二)物理层详解
前言 在前面说了一下,计算机网络的大概内容,没有去深刻的去了解它,这篇文章给大家分享一下物理层! 我们知道ISO模型是七层,TCP/IP模型是五层,而tcp/ip协议只将七层概括为4层,我们将学习其中 ...