为 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)的更多相关文章

  1. hibernate左连接查询时在easyUI的dataGrid中有些行取值为空的解决办法

    1 当使用left join左连连接,sql语句为 select t from SecondPage t left join t.rightNavbar n where 1=1 页面中出现了部分空行的 ...

  2. jquery easyui datagrid改变某行的值

    $("#DeterminateMembers").datagrid("updateRow",{index:index,row:{fzr:"0" ...

  3. jquery easyui datagrid动态改变title的值

    title:'<input type="text" id="txtTitle1" style="background:none;border:n ...

  4. jQuery EasyUI DataGrid Checkbox 数据设定与取值

    纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...

  5. EasyUI datagrid checkbox数据设定与取值(转自http://blog.csdn.net/baronyang/article/dnetails/9323463,感谢分享,谢谢)

    这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数据列的 Checkbox 设定为 Che ...

  6. JQuery easyUi datagrid 中 自定义editor作为列表操作按钮列

    转自   http://blog.csdn.net/tianlincao/article/details/7494467 前言 JQuery easyUi datagrid 中 使用datagrid生 ...

  7. JAVA中自定义扩展Swagger的能力,自动生成参数取值含义说明,提升开发效率

    大家好,又见面了. 在JAVA做前后端分离的项目开发的时候,服务端需要提供接口文档供周边人员做接口的对接指导.越来越多的项目都在尝试使用一些基于代码自动生成接口文档的工具来替代由开发人员手动编写接口文 ...

  8. Oracle存储过程-自定义数据类型,集合,遍历取值

    摘要 Oracle存储过程,自定义数据类型,集合,遍历取值 目录[-] 0.前言 1.Packages 2.Packages bodies 3.输出结果 0.前言 在Oracle的存储过程中,可能会遇 ...

  9. 【freemaker】之自定义变量,特殊变量 globals ,循环对象取值

    entity public class Employee { private Integer id; private String name; private Integer age; private ...

随机推荐

  1. MPLS VPN随堂笔记1

    MPLS VPN 基础 1.MPLS vpn架构的特点 1.1.允许不同CE传递相同私网路由 1.2.SP内部(所有P路由器)不需要学习CE路由 1.3.无安全保障但有带宽保障(跟SP租用服务) 2. ...

  2. 201521123082《Java程序设计》第2周学习总结

    201521123082<Java程序设计>第2周学习总结 标签(空格分隔): Java 1.本周学习总结 巩固了类型转换的相关细节 初步认识了类和对象,使用Java撰写程序几乎都在使用对 ...

  3. 团队作业4——第一次项目冲刺(Alpha版本)3rd day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.界面 界面已初步完成并能够进行简单的界面关联 界面内的功能正在完善 2.登陆方面 QQ授权已申请,等待通过 申请通过后在登 ...

  4. 201521123057 《Java程序设计》 第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 1.clone方法 1.1 Object对 ...

  5. 201521123098 《Java程序设计》 第4周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1. 学习了继承的基本含义,用"class 子类名 extend 父类名" ...

  6. 201521123033《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  7. linux(CentOS5.8)环境下搭建Radius

    本文记录了freeRadius在CentOS5.8环境下的基本搭建过程,未涉及mysql的加入及配置 freeradius官方地址:http://freeradius.org/ 环境:CentOS5. ...

  8. es6中对象的类与继承方法

    对于对象,我一直搞不清楚到底是该如何去继承,如何去书写.在熟练es6之后,终于会尝试写出来了. 代码如下: //我们假定父类为person,子类为man class person{ construct ...

  9. 模拟实现一个ATM+购物商城程序

    记得上次小编上传了一个购物车程序,这次呢稍微复杂一点,还是那句话,上传在这里不是为了炫耀什么,只是督促小编学习,如果大神有什么意见和建议,欢迎指导.(PS:本次主要参考学习为主,自己原创的很少) 要求 ...

  10. 《MySQL必知必会》[03] 表数据的增删改

    1.增:插入数据 INSERT关键字可以插入新的行到数据库表中: 插入完整的行 插入行的一部分 插入多行 插入某些查询的结果 基本的INSERT语句是: INSERT INTO R(A1, A2, . ...