此功能还为真正完成,起到抛砖引玉的效果,发动大家的力量把这个功能完善起来,效果图如下:

基本上就是扩展了 datagrid.view 中的onAfterRender 这个事件,具体代码如下:

$.extend($.fn.datagrid.defaults.view,{
onAfterRender:function(target){
var dc = $.data(target,'datagrid').dc;
if(dc.header2.find('[filter="true"]').length == 0){
var header = dc.header1; //固定列表头
var header2 = dc.header2; // 常规列表头
var filterRow = $('<tr></tr>');
var opts = $.data(target,'datagrid').options;
var columns = opts.columns;
var frozenColumns = opts.frozenColumns; $.each(frozenColumns[0],function(){
if(!this.checkbox){
var w = header.find('[field="'+this.field+'"] > div').width(); filterRow.append('<td><input style="width:'+w+'px"/></td>');
}
else{
header.find('.datagrid-header-check').parent().attr('rowspan',2)
}
});
header.find('tbody').append(filterRow);
filterRow = $('<tr filter="true"></tr>'); $.each(columns[0],function(){
var w = header2.find('[field="'+this.field+'"] > div').width();
if(this.hfilter){
var a = $('<input field="'+this.field+'" class="easyui-combobox" style="width:'+w+'px" />');
filterRow.append($('<td></td>').append(a));
a.data('options',this.hfilter);
}else{
filterRow.append('<td><input style="width:'+w+'px"/></td>');
} }); header2.find('tbody').append(filterRow); var dgData = $(target).datagrid('getData').rows; header2.find('input[field]').each(function(){
var opts = $(this).data('options');
var field = $(this).attr('field');
$.extend(opts.options,{
onSelect:function(item){
var d = _.filter(dgData,function(row){
return row[field].indexOf(item[opts.options.textField]) > -1;
}); $(target).datagrid('loadData',d);
}
}); $(this)[opts.type](opts.options);
})
}
}
});

在定义列时,我们加上个自定义的属性

{ title: '部门名称', field: 'depname', width: 120,hfilter:{type:'combobox',options:{data:roleData,valueField:'KeyId',textField:'RoleName'}} }

红色字体就是自定义的属性,用来定义过滤行中的控件的

在使用的时候,扩展的代码要放到Datagrid 初始化的前边!

示例DEMO 完整代码

<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../../jquery-easyui-1.3.3/themes/2013/easyui.css">
<link rel="stylesheet" type="text/css" href="../../jquery-easyui-1.3.3/themes/icon.css">
<link rel="stylesheet" type="text/css" href="../../jquery-easyui-1.3.3/themes/default/lookup.css"> <script type="text/javascript" src="../../jquery-easyui-1.3.3/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../../jquery-easyui-1.3.3/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="../../jquery-easyui-1.3.3/jquery.easyui.min.js"></script> <script src="../../jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script src="../../jquery-easyui-1.3.3/underscore-min.js"></script> </head>
<body> <table id=userlist></table> <script> var userData ={"total":6,"rows":[{"KeyId":1,"UserName":"admin2","Password":"93BCC5699183BED3AD5E3C050ADEF183","PassSalt":"K6FqbXXK","Email":"hxl_apple@163.com","IsAdmin":false,"IsDisabled":false,"TrueName":"疯狂秀才","DepartmentId":4,"Mobile":"","QQ":"","Remark":"","AddTime":null,"MenusJson":null,"ConfigJson":"{\"theme\":{\"title\":\"默认皮肤\",\"name\":\"default\"},\"showType\":\"menubutton\",\"gridRows\":\"40\"}","depname":"生产部","Departments":"4,15"},{"KeyId":9,"UserName":"admin","Password":"7D34608F3723F586DC28FC5D88BCECC6","PassSalt":"ZWvu4jSn","Email":"hxl_apple@163.com","IsAdmin":true,"IsDisabled":false,"TrueName":"疯狂秀才","DepartmentId":3,"Mobile":"18668088525","QQ":"1055818239","Remark":"系统默认帐号,不可删除","AddTime":null,"MenusJson":null,"ConfigJson":"{\"theme\":{\"title\":\"流行灰\",\"name\":\"gray\"},\"showType\":\"Accordion2\",\"gridRows\":\"20\",\"showValidateCode\":false}","depname":"综合办公室","Departments":"3,15"},{"KeyId":10,"UserName":"test","Password":"5C7E95836A99FB74723F54C43511154A","PassSalt":"RGVJejaR","Email":"","IsAdmin":false,"IsDisabled":false,"TrueName":"Test1","DepartmentId":1,"Mobile":"","QQ":"","Remark":"","AddTime":null,"MenusJson":null,"ConfigJson":null,"depname":"幸福集团","Departments":"3,4,15,5"},{"KeyId":12,"UserName":"ddd","Password":"7A3C56FA5A59953C058E7525FA0F754F","PassSalt":"O8RnCnaH","Email":"","IsAdmin":false,"IsDisabled":false,"TrueName":"ddd","DepartmentId":0,"Mobile":"","QQ":"","Remark":"","AddTime":null,"MenusJson":null,"ConfigJson":null,"depname":"","Departments":""},{"KeyId":13,"UserName":"eee","Password":"2FE5DFAE3F8AC41F907FCF8B1C1579D7","PassSalt":"vKbkyBib","Email":"","IsAdmin":false,"IsDisabled":false,"TrueName":"eeee","DepartmentId":3,"Mobile":"","QQ":"","Remark":"","AddTime":null,"MenusJson":null,"ConfigJson":null,"depname":"综合办公室","Departments":""},{"KeyId":15,"UserName":"007","Password":"9E704A8699D72ADA11A7EB7BF07739FA","PassSalt":"bcLJiqHB","Email":"","IsAdmin":false,"IsDisabled":false,"TrueName":"007","DepartmentId":1,"Mobile":"","QQ":"","Remark":"","AddTime":null,"MenusJson":null,"ConfigJson":null,"depname":"幸福集团","Departments":""}]}
var roleData = [{"KeyId":2,"RoleName":"生产部","Sortnum":2,"Remark":"工程师","IsDefault":0,"Navigations":null,"Users":null,"Departments":""},
{"KeyId":3,"RoleName":"幸福集团","Sortnum":1,"Remark":"6666","IsDefault":0,"Navigations":null,"Users":null,
"Departments":"1,3,4,15,5,9,10,11,12,13,14,8"},
{"KeyId":17,"RoleName":"综合办公室","Sortnum":1,"Remark":"","IsDefault":1,"Navigations":null,"Users":null,"Departments":"1,4,15"},{"KeyId":20,"RoleName":"普通用户","Sortnum":3,"Remark":"","IsDefault":0,"Navigations":null,"Users":null,"Departments":"1,3,4,15,5,9,10,11,12,13,14,8"}] $.extend($.fn.datagrid.defaults.view,{
onAfterRender:function(target){
var dc = $.data(target,'datagrid').dc;
if(dc.header2.find('[filter="true"]').length == 0){
var header = dc.header1; //固定列表头
var header2 = dc.header2; // 常规列表头
var filterRow = $('<tr></tr>');
var opts = $.data(target,'datagrid').options;
var columns = opts.columns;
var frozenColumns = opts.frozenColumns; $.each(frozenColumns[0],function(){
if(!this.checkbox){
var w = header.find('[field="'+this.field+'"] > div').width(); filterRow.append('<td><input style="width:'+w+'px"/></td>');
}
else{
header.find('.datagrid-header-check').parent().attr('rowspan',2)
}
});
header.find('tbody').append(filterRow);
filterRow = $('<tr filter="true"></tr>'); $.each(columns[0],function(){
var w = header2.find('[field="'+this.field+'"] > div').width();
if(this.hfilter){
var a = $('<input field="'+this.field+'" class="easyui-combobox" style="width:'+w+'px" />');
filterRow.append($('<td></td>').append(a));
a.data('options',this.hfilter);
}else{
filterRow.append('<td><input style="width:'+w+'px"/></td>');
} }); header2.find('tbody').append(filterRow); var dgData = $(target).datagrid('getData').rows; header2.find('input[field]').each(function(){
var opts = $(this).data('options');
var field = $(this).attr('field');
$.extend(opts.options,{
onSelect:function(item){
var d = _.filter(dgData,function(row){
return row[field].indexOf(item[opts.options.textField]) > -1;
}); $(target).datagrid('loadData',d);
}
}); $(this)[opts.type](opts.options);
})
}
}
}); $(function(){
$('#userlist').datagrid({
toolbar:'#toolbar',
data:userData,
fit:true,
title:'用户列表',
selectOnCheck:false,
checkOnSelect:true,
singleSelect:true,
//tools:[{iconCls:'icon-add'}],
frozenColumns:[[
{checkbox:true},
{ title: 'ID', field: 'KeyId', width: 40, sortable: true },
{ title: '用户名', field: 'UserName', width: 100, sortable: true }
]], columns:[[
{ title: '真实姓名', field: 'TrueName', width: 100, sortable: true },
{ title: '部门名称', field: 'depname', width: 120,hfilter:{type:'combobox',options:{data:roleData,valueField:'KeyId',textField:'RoleName'}} },
{ title: '邮箱', field: 'Email', width: 100, sortable: true },
{
title: '超管',
field: 'IsAdmin',
width: 60,
align: 'center',
formatter: function (v, d, i) {
if (d.UserName == "admin")
return '';
return v ? '√':'x'
}
},
{
title: '状态',
field: 'IsDisabled',
width: 60,
align: 'center',
formatter: function (v, d, i) {
if (d.UserName == "admin")
return '';
return v ? '√':'x'
}
},{title:'描述',field:'Remark',width:160}
]],
pagination: true,
pageSize:20,
rowStyler: function (index, row, css) {
if (row.UserName=="admin") {
return 'font-weight:bold;';
}
} });
}); </script> </body>
</html>

  

扩展 easyui 控件系列:为datagrid 增加过滤行的更多相关文章

  1. 扩展GridView控件——为内容项添加拖放及分组功能

    引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...

  2. easyui表单多重验证,动态设置easyui控件

    要实现的功能:在做添加学生信息的时候,利用easyui的验证功能判断 学号是否重复和学号只能为数字 最终效果如下图: 但在做这个的过程中,遇到了一系列的问题: 扩展validatebox的验证方法,最 ...

  3. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  4. 验证控件插图扩展控件ValidatorCalloutExtender(用于扩展验证控件)和TextBoxWatermarkExtender

    <asp:ScriptManager ID="ScriptManager1" runat="server">  </asp:ScriptMan ...

  5. Android控件系列之RadioButton&RadioGroup(转)

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

  6. Android控件系列之CheckBox

    学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...

  7. C#控件系列--文本类控件

    C#控件系列--文本类控件         文本类控件主要包含Label.LinkLabel.Button.TextBox以及RichTextBox. Label 功能         Label用来 ...

  8. easyui 控件获取焦点方式

    针对easyui控件前端组织的dom做分析,如下: combo/combobox/combogrid类似结构如下: <input class="easyui-datebox dateb ...

  9. 动态添加easyui 控件

    jquery提供了append,appendTo方法,可以动态添加静态的html文本,在easyui中,要动态添加easyui控件要怎么做呢,下面就来介绍动态添加easyui控件. 使用方法:和添加静 ...

随机推荐

  1. json数组的序列化和反序列化json数组的序列化和反序列化

    如题,我就不多说了,自己看代码的,很好理解 using System; using System.Collections.Generic; using System.Web; using System ...

  2. Asp.net MVC使用Filter解除Session, Cookie等依赖

    本文,介绍了Filter在MVC请求的生命周期中的作用和角色,以及Filter的一些常用应用场景. 同时针对MVC中的对于Session,Cookie等的依赖,如何使用Filter解依赖. 如果大家有 ...

  3. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

  4. SQL Server 2014新特性——Buffer Pool扩展

    Buffer Pool扩展 Buffer Pool扩展是buffer pool 和非易失的SSD硬盘做连接.以SSD硬盘的特点来提高随机读性能. 缓冲池扩展优点 SQL Server读以随机读为主,S ...

  5. 十五天精通WCF——第三天 client如何知道server提供的功能清单

     通常我们去大保健的时候,都会找姑娘问一下这里能提供什么服务,什么价格,这时候可能姑娘会跟你口述一些服务或者提供一份服务清单,这样的话大 家就可以做到童嫂无欺,这样一份活生生的例子,在wcf中同样是一 ...

  6. PHP 类型判断和NULL,空值检查

    PHP是一种宽松类型的编程语言,在函数中对传入的参数值的“类型”以及”值是否为空或者NULL“进行检查是不可缺少的步骤. 类型检查 从PHP5开始,PHP允许对函数的参数进行类型约束,即可以约束参数的 ...

  7. CentOS 7 网卡子接口的创建

    OS:CentOS 7 在linux上创建vlan需要加载802.1q模块: 1.检测OS是否已经加载802.1q模块 [root@controller ~]# modinfo 8021q filen ...

  8. linux学习之路——ubuntu 16.04 开机开启数字小键盘解决方法

    第一步:安装numlockx,输入命令 sudo apt-get install numlockx 第二步:用 vim 打开 rc.local 文件,输入命令 sudo vim /etc/rc.loc ...

  9. php cli方式下获取服务器ip

    (未整理....) (1)php cli方式下获取服务器ip [php]  function getServerIp(){          $ss = exec('/sbin/ifconfig et ...

  10. 基于.net开发chrome核心浏览器【六】

    写在前面: 距离发这个系列的上一篇文章已经过去两个多月了 因为工作上不涉及这一部分的内容,兼且琐事缠身,一直无力动笔写这个系列的第六篇文章 然而,有很多朋友都关注这个系列,希望我能再写写. 写文章有人 ...