您的位置:首页 > Web前端 > JQuery

jquery easyui DataGrid

2015-10-06 23:22 736 查看
Easyui Demo网站:
http://www.jeasyui.com/ 英文
http://www.phptogether.com/juidoc/ 中文

datagrip的基本属性方法:http://www.phptogether.com/juidoc/datagrid.html

推荐:http://www.cnblogs.com/Philoo/tag/jQuery/

冻结列 (2013-01-17 )

$('#tbList').datagrid({ pagination: true,
frozenColumns: [[
{ field: 'BId',checkbox:'true',width:30},
{ field: 'BNo', title: '牌号', width: 100 },
{ field: 'FNo', title: '班号', width: 100 }
  ]],
       fitColumns:false //禁止自适应宽度、可以水平滚动
});


去掉分页(2013-02-20)

$('#tbList').datagrid({pagination: true});

更改为

$('#tbList').datagrid();



$('#tbList').datagrid({pagination: false});

注意:同时需要设置table的高度,而且不能为auto

复选框[b]以及[b]单选[/b](2013-02-20)[/b]

<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');
}
});


获取行数(2013-02-20)

$('#tbList').datagrid("getRows").length;

隐藏列(2013-02-20)

<th field="Dept" width="100" hidden="true">名称</th>

清空原有数据(2013-02-20)

方法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:载入本地数据,旧记录将被移除。

事件(2013-02-20):

$('#tbList').datagrid({ onClickRow: function () {//代码 } });

datagrip单击行的时候,将单选按钮设置为选中(2013-02-20):

<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的时间格式问题(2013-03-14)

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内容太长自动换行(2013-03-18)

 添加属性 nowrap="false"

行和复选框的分离(2013-03-25)

方法一:(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() } });
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: