77using System . Web ;
88using System . Web . Helpers ;
99using System . Web . Mvc ;
10+ using System . Web . Routing ;
1011
1112namespace Griddly . Mvc
1213{
@@ -45,6 +46,8 @@ public GriddlySettings()
4546 Filters = new List < GriddlyFilter > ( ) ;
4647 Buttons = new List < GriddlyButton > ( ) ;
4748 RowIds = new Dictionary < string , Func < object , object > > ( ) ;
49+ HtmlAttributes = new RouteValueDictionary ( ) ;
50+ TableHtmlAttributes = new RouteValueDictionary ( ) ;
4851
4952 ClassName = DefaultClassName ;
5053 TableClassName = DefaultTableClassName ;
@@ -64,6 +67,8 @@ public GriddlySettings()
6467 public FilterMode ? AllowedFilterModes { get ; set ; }
6568 public FilterMode ? InitialFilterMode { get ; set ; }
6669 public bool ShowRowSelectCount { get ; set ; }
70+ public IDictionary < string , object > HtmlAttributes { get ; set ; }
71+ public IDictionary < string , object > TableHtmlAttributes { get ; set ; }
6772
6873 public int ? PageSize { get ; set ; }
6974 public int ? MaxPageSize { get ; set ; }
@@ -80,6 +85,7 @@ public GriddlySettings()
8085 public Func < object , object > RowClickUrl { get ; set ; }
8186 public string RowClickModal { get ; set ; }
8287 public Func < object , object > RowClass { get ; set ; }
88+ public Func < object , object > RowHtmlAttributes { get ; set ; }
8389
8490 public Func < GriddlyResultPage , object > FooterTemplate { get ; set ; }
8591 public Func < GriddlyResultPage , object > HeaderTemplate { get ; set ; }
@@ -106,6 +112,29 @@ public virtual object RenderRowClass(object o)
106112 return RowClass ( o ) ;
107113 }
108114
115+ public virtual IDictionary < string , object > GenerateRowHtmlAttributes ( object o )
116+ {
117+ if ( RowHtmlAttributes != null )
118+ {
119+ object value = RowHtmlAttributes ( o ) ;
120+
121+ if ( value != null )
122+ {
123+ RouteValueDictionary attributes = new RouteValueDictionary ( ) ;
124+
125+ if ( ! ( value is IDictionary < string , object > ) )
126+ value = HtmlHelper . AnonymousObjectToHtmlAttributes ( value ) ;
127+
128+ foreach ( KeyValuePair < string , object > entry in ( IDictionary < string , object > ) value )
129+ attributes . Add ( entry . Key , entry . Value ) ;
130+
131+ return attributes ;
132+ }
133+ }
134+
135+ return null ;
136+ }
137+
109138 public GriddlySettings RowId ( Expression < Func < object , object > > expression , string name = null )
110139 {
111140 if ( name == null )
@@ -339,6 +368,17 @@ public class GriddlySettings<TRow> : GriddlySettings
339368 }
340369 }
341370
371+ public new Func < TRow , object > RowHtmlAttributes
372+ {
373+ set
374+ {
375+ if ( value != null )
376+ base . RowHtmlAttributes = ( x ) => value ( ( TRow ) x ) ;
377+ else
378+ base . RowHtmlAttributes = null ;
379+ }
380+ }
381+
342382 public GriddlySettings < TRow > RowId ( Expression < Func < TRow , object > > expression , string name = null )
343383 {
344384 if ( name == null )
@@ -352,7 +392,7 @@ public GriddlySettings<TRow> RowId(Expression<Func<TRow, object>> expression, st
352392 return this ;
353393 }
354394
355- public GriddlySettings < TRow > Column < TProperty > ( Expression < Func < TRow , TProperty > > expression , string caption = null , string format = null , string expressionString = null , SortDirection ? defaultSort = null , string className = null , bool isExportOnly = false , string width = null , SummaryAggregateFunction ? summaryFunction = null , object summaryValue = null , Func < TRow , object > template = null , Func < GriddlyColumn , GriddlyFilter > filter = null , int defaultSortOrder = 0 )
395+ public GriddlySettings < TRow > Column < TProperty > ( Expression < Func < TRow , TProperty > > expression , string caption = null , string format = null , string expressionString = null , SortDirection ? defaultSort = null , string className = null , bool isExportOnly = false , string width = null , SummaryAggregateFunction ? summaryFunction = null , object summaryValue = null , Func < TRow , object > template = null , Func < GriddlyColumn , GriddlyFilter > filter = null , Func < TRow , object > htmlAttributes = null , object headerHtmlAttributes = null , int defaultSortOrder = 0 )
356396 {
357397 ModelMetadata metadata = null ;
358398
@@ -401,6 +441,9 @@ public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>>
401441 if ( string . IsNullOrWhiteSpace ( expressionString ) && summaryFunction != null )
402442 throw new InvalidOperationException ( "Must specify an expression to use a summary function." ) ;
403443
444+ if ( headerHtmlAttributes != null && ! ( headerHtmlAttributes is IDictionary < string , object > ) )
445+ headerHtmlAttributes = HtmlHelper . AnonymousObjectToHtmlAttributes ( headerHtmlAttributes ) ;
446+
404447 Add ( new GriddlyColumn < TRow > ( )
405448 {
406449 Template = template ,
@@ -413,15 +456,17 @@ public GriddlySettings<TRow> Column<TProperty>(Expression<Func<TRow, TProperty>>
413456 DefaultSortOrder = defaultSortOrder ,
414457 ClassName = className ,
415458 IsExportOnly = isExportOnly ,
416- Width = width
459+ Width = width ,
460+ HtmlAttributesTemplate = htmlAttributes ,
461+ HeaderHtmlAttributes = ( IDictionary < string , object > ) headerHtmlAttributes
417462 } , filter ) ;
418463
419464 return this ;
420465 }
421466
422- public GriddlySettings < TRow > Column ( string caption = null , string format = null , string expressionString = null , SortDirection ? defaultSort = null , string className = null , bool isExportOnly = false , string width = null , SummaryAggregateFunction ? summaryFunction = null , object summaryValue = null , Func < TRow , object > template = null , Func < GriddlyColumn , GriddlyFilter > filter = null , int defaultSortOrder = 0 )
467+ public GriddlySettings < TRow > Column ( string caption = null , string format = null , string expressionString = null , SortDirection ? defaultSort = null , string className = null , bool isExportOnly = false , string width = null , SummaryAggregateFunction ? summaryFunction = null , object summaryValue = null , Func < TRow , object > template = null , Func < GriddlyColumn , GriddlyFilter > filter = null , Func < TRow , object > htmlAttributes = null , object headerHtmlAttributes = null , int defaultSortOrder = 0 )
423468 {
424- return Column < object > ( null , caption , format , expressionString , defaultSort , className , isExportOnly , width , summaryFunction , summaryValue , template , filter , defaultSortOrder ) ;
469+ return Column < object > ( null , caption , format , expressionString , defaultSort , className , isExportOnly , width , summaryFunction , summaryValue , template , filter , htmlAttributes , headerHtmlAttributes , defaultSortOrder ) ;
425470 }
426471
427472 public GriddlySettings < TRow > SelectColumn ( Expression < Func < TRow , object > > id , object summaryValue = null )
0 commit comments