一、简单示例

HTML
<table id="tbList" striped="true" rownumbers="true" fix="true" fitcolumns="true" title="标题"
idfield="ID" checkbox="true" url="@Url.Action("ListData")">
<thead>
<tr>
<th field="ID" checkbox="true" width="30">
</th>
<th field="Name" width="200" align="center">
名称
</th>
</tr>
</thead>
</table>
JS
$('#tbList').datagrid({ pagination: true });
$("#btnSearch").click(function () {//查询方法
$('#tbList').datagrid("unselectAll");
$('#tbList').datagrid({ queryParams: { SearchName: $("#SearchName").val()} });
});
二、基本用法
冻结列
$('#tbList').datagrid({ pagination: true,
frozenColumns: [[
{ field: 'BId',checkbox:'true',width:30},
{ field: 'BNo', title: '牌号', width: 100 },
{ field: 'FNo', title: '班号', width: 100 }
]],
fitColumns:false //禁止自适应宽度、可以水平滚动
});
去掉分页
$('#tbList').datagrid({pagination: true});
更改为
$('#tbList').datagrid();
或
$('#tbList').datagrid({pagination: false});
注意:同时需要设置table的高度,而且不能为auto
复选框以及单选
<table id="tbList" style="height: 330px;" striped="true" rownumbers="true" fitColumns="true" title="" iconcls="icon-edit"
checkbox="true" idfield="Id" url="@Url.Action("ListData")">
<thead>
<tr>
<th field="Id" checkbox="true" width="150">
</th>
</tr>
</thead>
</table>
变为单选(添加singleSelect="true" )
复制代码 代码如下:
<table id="tbList" style="height: 330px;" striped="true" rownumbers="true" fitColumns="true" title="" iconcls="icon-edit" singleSelect="true" checkbox="true" idfield="Id" url="@Url.Action("ListData")">
加载数据后默认全选:
$(document).ready(function () {
$('#tbList').datagrid({
onLoadSuccess: function (data) {
$('#tbList').datagrid('selectAll');
}
});
获取行数
$('#tbList').datagrid("getRows").length;
隐藏列
<th field="Dept" width="100" hidden="true">名称</th>
清空原有数据
方法1:
var item = $('#filegrid').datagrid('getRows');
if (item) {
for (var i = item.length - 1; i >= 0; i--) {
var index = $('#filegrid').datagrid('getRowIndex', item[i]);
$('#filegrid').datagrid('deleteRow', index);
}
}
方法2:(测试过)
$('#filegrid').datagrid('loadData', { total: 0, rows: [] });
解析:loadData:载入本地数据,旧记录将被移除。
行点击事件
$('#tbList').datagrid({ onClickRow: function () {//代码 } });
datagrip单击行的时候,将单选按钮设置为选中
<script type="text/javascript">
var List = {};
List.RadioFormatter = function (value, rec, index) {
return "<input id='radio_id' name='radio_name' type='radio' value='" + rec.Id + "'/>";
};
$(document).ready( function(){ //呈现列表数据
$('#tbList').datagrid({ onClickRow:
function () {
//单击行的时候,将单选按钮设置为选中
var id = $('#tbList').datagrid("getSelected");
$("input[name='name']").each(function () {
if ($(this).val() == id.Id) {
$(this).attr("checked", true);
}
});
}
});
});
</script>
<table id="tbList" style="height: 300px;" striped="true" rownumbers="true" fitColumns="true" title="" iconcls="icon-edit"
singleSelect="true" checkbox="true" idfield="Id" url="@Url.Action("ListData")">
<thead>
<tr>
<th field="Id" width="30" formatter="PickupList.RadioFormatter">
</th>
</tr>
</thead>
</table>
table中td的时间格式问题
1.页面
<th field="Test" formatter="Common.TimeFormatter" width="50" ></th>
2.js
var Common = {
//EasyUI用DataGrid用日期格式化
TimeFormatter: function (value, rec, index) {
if (value == undefined) {
return "";
}
/*json格式时间转js时间格式*/
value = value.substr(1, value.length - 2);
var obj = eval('(' + "{Date: new " + value + "}" + ')');
var dateValue = obj["Date"];
if (dateValue.getFullYear() < 1900) {
return "";
}
var val = dateValue.format("yyyy-mm-dd HH:MM");//控制格式
return val.substr(11, 5);
}
};
table中td内容太长自动换行
添加属性 nowrap="false"
将nowrap: false这个属性设置在table的属性中,不要设置在字段的属性中,字段可以设置宽度,这样就可以做到当文字长度超过规定的宽度后自动换行了
行和复选框的分离
方法一:(1.3版本才能用)
checkOnSelect="false" selectOnCheck="false"
注意:当使用$("#tbList").datagrid("getSelections");时候,只有行被选中,才能取到该行。一般情况,选中行时候,行为黄色背景。
eg.<table checkOnSelect="false"> </table>
var selected = $("#tbList").datagrid("getSelections");
if (selected.length == 0) {
alert("请选择!");
return;
}
var idString = "";
$.each(selected, function (index, item) {
idString += item.Id + ",";
});
方法二(1.3版本之前的解决方法)
var IsCheckFlag = true;
$('#tbList').datagrid({
pagination: true,
onClickCell: function (rowIndex, field, value) {
IsCheckFlag = false;
},
onSelect: function (rowIndex, rowData) {
if (!IsCheckFlag) {
IsCheckFlag = true;
$("#tbList").datagrid("unselectRow", rowIndex);
}
},
onUnselect: function (rowIndex, rowData) {
if (!IsCheckFlag) {
IsCheckFlag = true;
$("#tbList").datagrid("selectRow", rowIndex);
}
}
});
设置数据列表的样式
$(document).ready(function () {
//呈现列表数据
$('#tbList').datagrid({ pagination: true,
rowStyler: function(index,row){
if (row.ID< 10) {//那么数据的id字段小于10的,将显示为灰色字体
return 'color:#999;';//和一般的样式写法一样
}
}
});
});
条件查询
复选框的bug:使用参数查询时候,在查询之前选中的选项 ,在查询之后,使用getSelections方法去获取,依旧存在该选项
解决方案:通过unselectAll在查询之前清空复选框即可
$("#btnSearch").click(function () {
$('#tbList').datagrid("unselectAll");
$('#tbList').datagrid({ queryParams: { SearchName: $("#SearchName").val() } });
});
查询bug:使用参数查询时候,在查询之后,显示的当前页码还是之前的 ,不会重置为1,还是之前页码;如果当前总页数为比当前小,导致页面显示为空。比如,当前第三页,加入时间查询后,只有1页数据,那么当前页码还是3,导致页面显示空白。
解决方案:设置pageNumber为 1
$("#btnSearch").click(function () {
$('#tbList').datagrid("unselectAll");
$('#tbList').datagrid({ pageNumber: 1,queryParams: { SearchName: $("#SearchName").val() } });
});
三、行数据的增删改
HTML(不分页列表)
复制代码 代码如下:
<table id="tbProductList" style="height: 500px; max-height: 500px;" fix="true" fitcolumns="true" idfield="ID" url="@Url.Action("ListData")"></table>
JS
$(document).ready(function () {
var datagrid;
var editRow = undefined;
datagrid = $("#tbList").datagrid({
border: true,
checkbox: true,
iconCls: 'icon-save', //图标
nowap: true, //列内容多时自动折至第二行
pagination: false,
rownumbers: true,
striped: true, //行背景交换
columns: [[//显示的列
{ field: 'ID', title: '编号', width: 100, align: 'center', sortable: true, checkbox: true },
{ field: 'Name', title: '名称', width: 100, sortable: true },
{
field: 'PriceType', title: '类型', width: 100, align: 'center',
formatter: function (value, row) { return row.TypeName; },
editor: {
type: 'combobox', options: {
valueField: 'Value',
textField: 'Text',
method: 'get',
url: $("#TypeUrl").val(),
required: true
}
}
},
{
field: 'Price', title: '价格', width: 100, align: 'center',
editor: {
type: 'numberbox', options: {
required: true,
min: 0,
precision: 2
}
}
},
{
field: 'Count', title: '数量', width: 100, align: 'center',
editor: {
type: 'numberbox', options: {
required: true,
min: 0,
precision: 0
}
}
}
]],
queryParams: { action: 'query' }, //查询参数
toolbar: [{ text: '添加', iconCls: 'icon-add', handler: function () {//添加列表的操作按钮添加,修改,删除等
//添加时先判断是否有开启编辑的行,如果有则把开户编辑的那行结束编辑
if (editRow != undefined) {
datagrid.datagrid("endEdit", editRow);
}
//添加时如果没有正在编辑的行,则在datagrid的第一行插入一行
if (editRow == undefined) {
datagrid.datagrid("insertRow", {
index: 0, // index start with 0
row: {
}
});
//将新插入的那一行开户编辑状态
datagrid.datagrid("beginEdit", 0);
//给当前编辑的行赋值
editRow = 0;
}
}
}, '-',
{
text: '删除', iconCls: 'icon-remove', handler: function () {
//删除时先获取选择行
var rows = datagrid.datagrid("getSelections");
//选择要删除的行
if (rows.length > 0) {
$.messager.confirm("提示", "你确定要删除吗?", function (r) {
if (r) {
var ids = [];
for (var i = 0; i < rows.length; i++) {
ids.push(rows[i].ID);
}
//将选择到的行存入数组并用,分隔转换成字符串
if ($.trim(ids) != "") {
//---- Delete(ids.join(','));//这是post
} else {
alert("请选择要删除的信息!");
}
}
});
}
else {
$.messager.alert("提示", "请选择要删除的行", "error");
}
}
}, '-',
{
text: '修改', iconCls: 'icon-edit', handler: function () {
//修改时要获取选择到的行
var rows = datagrid.datagrid("getSelections");
//如果只选择了一行则可以进行修改,否则不操作
if (rows.length == 1) {
//修改之前先关闭已经开启的编辑行,当调用endEdit该方法时会触发onAfterEdit事件
if (editRow != undefined) {
datagrid.datagrid("endEdit", editRow);
}
//当无编辑行时
if (editRow == undefined) {
//获取到当前选择行的下标
var index = datagrid.datagrid("getRowIndex", rows[0]);
//开启编辑
datagrid.datagrid("beginEdit", index);
//把当前开启编辑的行赋值给全局变量editRow
editRow = index;
//当开启了当前选择行的编辑状态之后,
//应该取消当前列表的所有选择行,要不然双击之后无法再选择其他行进行编辑
datagrid.datagrid("unselectAll");
}
}
}
}, '-',
{
text: '保存', iconCls: 'icon-save', handler: function () {
//保存时结束当前编辑的行,自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
datagrid.datagrid("endEdit", editRow);
}
}, '-',
{
text: '取消编辑', iconCls: 'icon-redo', handler: function () {
//取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
editRow = undefined;
datagrid.datagrid("rejectChanges");
datagrid.datagrid("unselectAll");
}
}, '-'],
onAfterEdit: function (rowIndex, rowData, changes) {
//endEdit该方法触发此事件
//console.info(rowData);
//---- Update(ids.join(','));//这是post
editRow = undefined;
},
onDblClickRow: function (rowIndex, rowData) {
//双击开启编辑行
if (editRow != undefined) {
datagrid.datagrid("endEdit", editRow);
}
if (editRow == undefined) {
datagrid.datagrid("beginEdit", rowIndex);
editRow = rowIndex;
}
}
});
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# jquery
# easyui
# DataGrid
# 基于datagrid框架的查询
# jQuery easyui datagrid动态查询数据实例讲解
# Jquery EasyUI Datagrid右键菜单实现方法
# jquery easyui dataGrid动态改变排序字段名的方法
# jQuery Easyui学习之datagrid 动态添加、移除editor
# jQuery EasyUI之DataGrid使用实例详解
# Jquery之datagrid查询详解
# 单选
# 复选框
# 请选择
# 这是
# 分页
# 双击
# 设置为
# 单击
# 清空
# 换行
# 行数
# 加入时间
# 如果没有
# 将被
# 可将
# 能为
# 则可
# 则在
# 转换成
# 时要
相关文章:
建站主机数据库如何配置才能提升网站性能?
建站之家VIP精选网站模板与SEO优化教程整合指南
建站主机默认首页配置指南:核心功能与访问路径优化
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
如何用搬瓦工VPS快速搭建个人网站?
已有域名和空间,如何快速搭建网站?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
如何选择网络建站服务器?高效建站必看指南
北京制作网站的公司,北京铁路集团官方网站?
建站之星导航如何优化提升用户体验?
如何用景安虚拟主机手机版绑定域名建站?
建站之星导航菜单设置与功能模块配置全攻略
平台云上自助建站如何快速打造专业网站?
安徽网站建设与外贸建站服务专业定制方案
定制建站哪家更专业可靠?推荐榜单揭晓
中山网站制作网页,中山新生登记系统登记流程?
如何通过VPS建站实现广告与增值服务盈利?
如何用狗爹虚拟主机快速搭建网站?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
无锡营销型网站制作公司,无锡网选车牌流程?
大连 网站制作,大连天途有线官网?
如何在阿里云虚拟主机上快速搭建个人网站?
制作网站公司那家好,网络公司是做什么的?
如何用wdcp快速搭建高效网站?
Swift开发中switch语句值绑定模式
网站制作公司排行榜,四大门户网站排名?
北京网站制作公司哪家好一点,北京租房网站有哪些?
网站制作难吗安全吗,做一个网站需要多久时间?
百度网页制作网站有哪些,谁能告诉我百度网站是怎么联系?
如何用免费手机建站系统零基础打造专业网站?
网站制作企业,网站的banner和导航栏是指什么?
如何制作一个表白网站视频,关于勇敢表白的小标题?
青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?
深圳网站制作设计招聘,关于服装设计的流行趋势,哪里的资料比较全面?
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
家具网站制作软件,家具厂怎么跑业务?
建站之星安装步骤有哪些常见问题?
如何使用Golang安装API文档生成工具_快速生成接口文档
大连企业网站制作公司,大连2025企业社保缴费网上缴费流程?
如何快速辨别茅台真假?关键步骤解析
定制建站方案优化指南:企业官网开发与建站费用解析
如何通过宝塔面板实现本地网站访问?
如何通过商城自助建站源码实现零基础高效建站?
如何确保西部建站助手FTP传输的安全性?
网站建设制作、微信公众号,公明人民医院怎么在网上预约?
标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
广州美橙建站如何快速搭建多端合一网站?
宝塔新建站点为何无法访问?如何排查?
香港服务器网站生成指南:免费资源整合与高速稳定配置方案
*请认真填写需求信息,我们会在24小时内与您取得联系。