MyTable MvcContrib.UI.Grid扩展项目

MyTable 是对MvcContrib.UI.Grid的一个扩展控件.该代码须运行于.net 3.5++. 在原来的基础上进行了一些功能性扩展,主要是从合并行,合并列,以及单行多列,分组补白等.上作了稍许的修改.希望大家喜欢. 因为该表格没有作过需求计划,因此在写代码方面主要从主观方面着手,希望大家谅解.
同时为了使代码连写做得更好,在很多方面花了不少的代价.具体的一些情况下载后自己慢慢研究.以下是一个示例程序:

欢迎大家一起探讨,共同学习.如果在使用中有什么问题,可以通过E-mail:xy109@qq.com与我联系,谢谢.

源码下载:https://files.cnblogs.com/xy109/MyTable.rar 

示例代码
Html.MyTable<MyModel>(ViewData.Model).Columns((MyTable.ColumnBuilder<MyModel> c)=>{
    c.For((MyTableViewData
<MyModel> x) => x.RowsIndex, "行号").Visible(false).HeaderAttributes("style","background:#BBB");
    c.For(x 
=> x.Item.ID);
    c.For((MyTableViewData
<MyModel> x) => "" + x.GroupIndex + """分组");
    c.For(x 
=> x.Name,"姓名");
    c.For(x 
=> x.Sex ? "√ " : null).HeaderValue((d) => d.RowsIndex == 0 ? "性别" : "").Command((d, x) =>
    {
        
if (d.ItemType == ViewDataType.HeaderItem && d.RowsIndex == 0) x.MergeCols(2);
    });
    c.For(x 
=> x.Sex ? null : "√ """).Command((d, x) => x.Visible(!(d.ItemType == ViewDataType.HeaderItem && d.RowsIndex == 0)));
    c.For(x 
=> x.Tel,"电话").CellCondition(x => !String.IsNullOrEmpty(x.Tel) && x.Tel.Length > 2);
    c.For(x 
=> x.Address);
    c.For(
"其它").Visible(false);
}).RowAttributes((MyTableViewData
<MyModel> x) =>
{
    var d 
= new Dictionary<stringobject>();
    
if (x.ItemType == ViewDataType.DataItem && x.IsAlternateRow)
    {
        d.Add(
"style""background:#EFEFEF");
    }
    
else if (x.ItemType == ViewDataType.DataItem && x.Item.ID % 5 == 0 && x.Item.ID > 0)
    {
        d.Add(
"style""background:red;color:blue");
    }
    
return d;
}).Repeates(
3).HeaderAttributes("style""background:#999").HeaderRows(2).Command((c, d) =>
{
    MyTable.IGridColumn
<MyModel>[] columns = c.ToArray();
    
if (d.IsFirstRepeat) columns[0].Visible(d.IsFirstRepeat);
    
if (d.IsLastRepeat) columns[columns.Length - 1].Visible(true);
    
if (d.ItemType == ViewDataType.HeaderItem)
    {
        
for (int i = 0; i < columns.Length; i++)
        {
            
if (i == 4 || i == 5continue;
            
if (d.RowsIndex == 0)
            {
                columns[i].MergeRows(
2);
            }
            
else
            {
                columns[i].MergeRows(
0);
            }
        }
    }
    
else 
    {
        
if (d.RowsIndex % 5 == 0)
        {
            columns[
2].MergeRows(5);
        }
        
else
        {
            columns[
2].MergeRows(0);
        }
    }
}).Groups(
5)

 

 

通过扩展后,生成一些花样表格并不是一件很难的事情,我这里是一个示例,下面还有些参数说明:

枚举类型:
MyTable.ViewDataType 单元格类型
HeaderItem 表头单元格
DataItem 包含数据单元格
FooterItem 表脚单元格
BlankItem 空数据单无格

MyTable.GroupType 分组类型
Staggered 交叉分组,即按组的顺序进行分配
Quere 顺序分组,即先按行的顺序一个一个进行分配

MyTable.SkipType 跳过类型
SaveDataSkip 保存数据跳过,数据将在下一单元格中出现
NoSaveSkip 不保存数据跳过,当前数据将不再出现
NoSkip 不跳过

GridRow<T>扩展
名称 返回类型 说明
Command(Action<ICollection<IGridColumn<T>>, MyTableViewData<T>> cmd) void 设置在行生成时执行的委托,建议,不在在委托中操作固定属性值
ExecuteCommand(ICollection<GridColumn<T>> columns, MyTableViewData<T> data) void 执行委托
Attribute(Func<MyTableViewData<T>, IDictionary<string, object>> attributes) void 设置动态求值属性委托
Attribute(IDictionary<string, object> attributes) void 设置固定属性
Attribute(string key, object value) void 设置固定属性
RemoveAttribute(params string[] attributes) void 移除固定属性

IGridSections<T>扩展
名称 返回类型 说明
FooterRow { get; } GridRow<T> 表脚
IsEmptyShowHeader { set; get; } bool 当数据为空时是否显示表头,默认值false
IsEmptyShowFooter { get; set; } bool 当数据为空时是否显示表脚,默认值false
Repeates { get; set; } int 数据重复次数,指一行中,显示几条数据
HeaderRows { get; set; } int 表头数据行数,默认为1
FooterRows { get; set; } int 表脚数据行数,默认为0
Groups { set; get; } int 数据分组,默认为0,即不分组
BlankRows { set; get; } int 数据补行,即在表格底部,表脚前面添加空白行,默认值为0
GroupType { set; get; } GroupType 分组类型
NullDataText { set; get; } string 当单元格数据为null时,替换的值,默认值 "&nbsp;"(空格)
ReplaceBlank { set; get; } Func<MyTableViewData<T>, SkipType> 数据替换行计算委托

IGridWithOptions<T>扩展
名称 返回值 说明
EmptyShowHeader() IGridWithOptions<T> 启用空数据显示表头
EmptyShowFooter() IGridWithOptions<T> 启用空数据显示表脚
Repeates(int repeateCount) IGridWithOptions<T> 设置单行数据重复数
HeaderRows(int headerRows) IGridWithOptions<T> 表头行数
FooterRows(int footerRows) IGridWithOptions<T> 表脚行数
Groups(int groups) IGridWithOptions<T> 分组数量设置,默认使用交错分组
Groups(int groups, GroupType groupType) IGridWithOptions<T> 分组数量设置,并设置分组类型
ReplaceBlank(Func<MyTableViewData<T>,SkipType> func) IGridWithOptions<T> 数据替换委托设置
NullDataText(string text) IGridWithOptions<T> 单元格null值替换设置
BlankRows(int rows) IGridWithOptions<T> 空白补行设置
Command(Action<ICollection<IGridColumn<T>>, MyTableViewData<T>> func) IGridWithOptions<T> 数据行生成时执行的命令设置,默认设置所有行,即表头,表脚及数据列(空白列作数据列处理)
Command(ViewDataType viewDataType,Action<ICollection<IGridColumn<T>>, MyTableViewData<T>> func) IGridWithOptions<T> 指定列命令设置(空白列作数据列处理)
Attributes(string key, object value) IGridWithOptions<T> 设置表格属性
RowsAttributes(ViewDataType viewDataType, Func<MyTableViewData<T>, IDictionary<string, object>> func) IGridWithOptions<T> 动态行属性设置(空白列作数据列处理)
RowsAttributes(ViewDataType viewDataType, IDictionary<string, object> attributes) IGridWithOptions<T> 固定行属性设置(空白列作数据列处理)
RowsAttributes(ViewDataType viewDataType, string key, object value) IGridWithOptions<T> 固定行属性设置(空白列作数据列处理)
RemoveRowsAttriutes(ViewDataType viewDataType, params string[] attributes) IGridWithOptions<T> 移除固定属性设置(空白列作数据列处理)
HeaderRowAttributes(Func<MyTableViewData<T>, IDictionary<string, object>> func) IGridWithOptions<T> 表头动态属性设置
HeaderAttributes(string key, object value) IGridWithOptions<T> 表头固定属性设置
RowAttributes(Func<MyTableViewData<T>, IDictionary<string, object>> attributes) IGridWithOptions<T> 数据行动态属性设置
RowAttributes(IDictionary<string, object> attributes) IGridWithOptions<T> 数据行固定属性设置
RowAttributes(string key, object value) IGridWithOptions<T> 数据行固定属性设置
FooterRowAttributes(Func<MyTableViewData<T>, IDictionary<string, object>> attributes) IGridWithOptions<T> 表脚动态属性设置
FooterRowAttributes(IDictionary<string, object> attributes) IGridWithOptions<T> 表脚固定属性设置
FooterRowAttributes(string key, object value) IGridWithOptions<T> 表脚固定属性设置


IGridColumn<T>扩展
名称 返回值 说明
MergeRows(int rows) IGridColumn<T> 合并行设置
MergeCols(int cols) IGridColumn<T> 合并列设置
Attributes(Func<MyTableViewData<T>, IDictionary<string, object>> attributes) IGridColumn<T> 单元格动态属性设置
Attributes(IDictionary<string, object> attributes) IGridColumn<T> 单元格固定属性设置
Attributes(string key, object value) IGridColumn<T> 单元格固定属性设置
HeaderAttributes(Func<MyTableViewData<T>, IDictionary<string, object>> attributes) IGridColumn<T> 表头单元格动态属性设置
HeaderAttributes(string key, object value) IGridColumn<T> 表头单元格固定属性设置
FooterAttributes(Func<MyTableViewData<T>, IDictionary<string, object>> attributes) IGridColumn<T> 表脚单元格动态属性设置
FooterAttributes(IDictionary<string, object> attributes) IGridColumn<T> 表脚单元格固定属性设置
FooterAttributes(string key, object value) IGridColumn<T> 表脚单元格固定属性设置
Attributes(ViewDataType viewDataType, Func<MyTableViewData<T>, IDictionary<string, object>> attributes) IGridColumn<T> 单元格动态属性设置(空白行当数据行处理)
Attributes(ViewDataType viewDataType, IDictionary<string, object> attributes) IGridColumn<T> 单元格固定属性设置(空白行当数据行处理)
Attributes(ViewDataType viewDataType, string key, object value) IGridColumn<T> 单元格固定属性设置(空白行当数据行处理)
RemoveAttributes(ViewDataType viewDataType, params string[] attributes) IGridColumn<T> 单元格固定属性移除(空白行当数据行处理)
HeaderValue(Func<MyTableViewData<T>, object> value) IGridColumn<T> 表头值取值委托
FooterValue(Func<MyTableViewData<T>, object> value) IGridColumn<T> 表脚取值委托
CellCondition(Func<MyTableViewData<T>, bool> func) IGridColumn<T> 单元格数据验证,未通过验证的数据将按null处理
FooterName(string name) IGridColumn<T> 表脚名称
EnableEmptyItemValue() IGridColumn<T> 空数据取值,即当数据为null时,一样进行委托求值
Command(Action<MyTableViewData<T>, IGridColumn<T>> command) IGridColumn<T> 单元格获取前执行委托,主要用于设置可视性


ColumnBuilder<T>扩展
名称 返回类型 说明
For(Expression<Func<T, object>> valueFunc, string displyName) IGridColumn<T> 添加新列,valueFunc为取值委托,displayName为表头名称
For(Expression<Func<MyTableViewData<T>, object>> valueFunc) IGridColumn<T> 添加新列,valueFunc为取值委托
For(Expression<Func<MyTableViewData<T>, object>> valueFunc, string displayName) IGridColumn<T> 添加新列,valueFunc为取值委托,displayName为表头名称
For(Func<MyTableViewData<T>, object> valueFunc, string displayName, Type type) IGridColumn<T> 添加新列,valueFunc为取值委托,displayName为表头名称,type为数据类型

MyTableViewData<T> 扩展了原来的ViewData

名称返回类型说明
IsFirstRowbool是否每一行
IsLastRowbool是否最后一行
IsAlternateRowbool是否为隔行
IsFirstRepeatbool单行中是否为每一次重复
IsLastRepeatbool单行中是否为最后一次重复
IsAlternatRepeatbool单行中数据是否为双数的重复
RepeateIndexint单行中数据重复索引
Repeatesint单行数据重复量
RowsIndexint行索引
RowsCountint共计行统计
ItemIndexint数据行索引
ItemCountint共计数据行量,这个数量会因替换空白行于改变
Groupsint分组时单组记录数量
GroupIndexint当启用分组时,这里为第几个分组
ItemTypeViewDataType数据类型

里面有一个关于MyHtmlTable的项目,是从中抽取出来的部分代码组合,主要是用来脱离Mvc框架.我这里不多说了.

自己觉得用起来方便就行了 

posted @ 2010-08-04 17:35  竹子小毅  阅读(946)  评论(0编辑  收藏  举报