为 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. 软件工程(GZSD2015)学生博客列表

    2015年贵州师范大学软件工程课程学生博客列表 陈小丽 郑倩 唐洁 周娟 李利思 肖俊 罗文豪 周静 徐明艳 毛涛 邓洪虹 岳庆 李盼 安坤 何亚 涂江凤 张义平 杨明颢 杨家堂 胡贵玲 寿克霞 吴明 ...

  2. spring的配置文件和加载

    ①:创建applicationContext.xml配置文件放在src下 //applicationContext.xml代码 <?xml version="1.0" enc ...

  3. 201521123042 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 参考资料: 百度脑图 XMind 2. 书面作业 作业参考文件下载 Q1.代码阅读:Child压缩包内源代码 1.1 com. ...

  4. java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    在使用C3P0连接池的时候,发现了这个错误-.原来要使用C3P0的使用,不仅仅要导入c3p0-0.9.2-pre1.jar这个jar包,还要导入mchange-commons-0.2.jar这个jar ...

  5. ASP.Net开发WebAPI跨域访问(CORS)的精简流程

    1: Web.config里有一行: <remove name="OPTIONSVerbHandler" /> 这个要删除. 2: nuget安装Microsoft.A ...

  6. 【Debian 8.8】Java 8 安装以及环境变量配置

    事实上可以分为简单的三个步骤: 下载 JDK 压缩包 解压压缩包 配置环境变量 需要注意的是: 所有命令默认在 root 权限下进行! 演示环境是 Debian 8.8 64位 (阿里云学生机) 1. ...

  7. ACM学习之路__HDU 1045

    Fire Net Description : Suppose that we have a square city with straight streets. A map of a city is ...

  8. 认识StringBuffer类

    概述: StringBuffer类是线程安全的可变字符序列 线程安全效率低 StringBuffer和String的区别 * String是一个不可变的字符序列 * StringBuffer是一个可变 ...

  9. [js高手之路] 设计模式系列课程 - DOM迭代器(2)

    如果你对jquery比较熟悉的话,应该用过 eq, first, last, get, prev, next, siblings等过滤器和方法.本文,我们就用迭代设计模式来封装实现,类似的功能 < ...

  10. express 安装和运行

    1.npm install -g express-generator 2.进入服务目录(自己定义的文件夹,或者express Myapp && cd Myapp 新建Myapp文件夹并 ...