Skip to content

Commit a600bea

Browse files
committed
Added support for grouped select lists
Added support for manually setting NamePlural Added support for setting item count to display in list display Reworked bool list helper param order to be more useful Fixed stack overflow in bool list helper Send parent item viewdata to settings request Don't hide popover on selection for multiple select lists Fixed sort attribute output to ensure no whitespace gets in the way
1 parent e5b0b91 commit a600bea

File tree

10 files changed

+401
-288
lines changed

10 files changed

+401
-288
lines changed

Build/CommonAssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
//
1616
// You can specify all the values or you can default the Revision and Build Numbers
1717
// by using the '*' as shown below:
18-
[assembly: AssemblyVersion("1.0.18.0")]
19-
[assembly: AssemblyFileVersion("1.0.18.0")]
18+
[assembly: AssemblyVersion("1.0.26.0")]
19+
[assembly: AssemblyFileVersion("1.0.26.0")]

Griddly.Mvc/Griddly.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="IHasOverallCount.cs" />
108108
<Compile Include="ListPage.cs" />
109109
<Compile Include="Properties\AssemblyInfo.cs" />
110+
<Compile Include="SelectListItemGroup.cs" />
110111
<Compile Include="SortField.cs" />
111112
</ItemGroup>
112113
<ItemGroup>

Griddly.Mvc/GriddlyFilter.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@ namespace Griddly.Mvc
88
{
99
public abstract class GriddlyFilter
1010
{
11-
public string Name { get; set; }
11+
string _name;
1212

13-
public string NamePlural
13+
public string Name
1414
{
1515
get
1616
{
17-
return PluralizationService.CreateService(CultureInfo.CurrentUICulture).Pluralize(Name);
17+
return _name;
18+
}
19+
set
20+
{
21+
if (!string.IsNullOrWhiteSpace(value))
22+
NamePlural = PluralizationService.CreateService(CultureInfo.CurrentUICulture).Pluralize(value);
23+
else
24+
NamePlural = null;
25+
26+
_name = value;
1827
}
1928
}
2029

30+
public string NamePlural { get; set; }
31+
2132
public string Field { get; set; }
2233
public object Default { get; set; }
2334
public virtual FilterDataType DataType { get; set; }
@@ -77,13 +88,17 @@ public GriddlyFilterList()
7788
{
7889
IsMultiple = true;
7990
IsNoneAll = true;
91+
DisplayItemCount = 1;
8092
}
8193

8294
public List<SelectListItem> Items { get; set; }
95+
public List<SelectListItem> SelectableItems { get; internal set; }
8396

8497
public bool IsMultiple { get; set; }
8598
public bool IsNoneAll { get; set; }
8699
public bool IsNullable { get; set; }
100+
101+
public int DisplayItemCount { get; set; }
87102
}
88103

89104
public enum FilterDataType

Griddly.Mvc/GriddlyFilterExtensions.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,15 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
8888

8989
itemsList.AddRange(items);
9090

91+
List<SelectListItem> selectableItemsList = new List<SelectListItem>();
92+
93+
selectableItemsList.AddRange(itemsList.Where(x => !(x is SelectListItemGroup)));
94+
selectableItemsList.AddRange(itemsList.Where(x => x is SelectListItemGroup).SelectMany(x => ((SelectListItemGroup)x).Items));
95+
9196
if (isMultiple && defaultSelectAll && defaultValue == null)
9297
{
93-
foreach (SelectListItem item in itemsList)
98+
// TODO: set default value to all selected
99+
foreach (SelectListItem item in selectableItemsList)
94100
item.Selected = true;
95101
}
96102
else if (!isMultiple || defaultValue != null)
@@ -99,11 +105,11 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
99105
{
100106
IEnumerable defaultValues = defaultValue as IEnumerable;
101107

102-
if (defaultValues == null)
108+
if (defaultValues == null || defaultValue is string)
103109
{
104110
string value = GetDefaultValueString(defaultValue);
105111

106-
foreach (SelectListItem item in itemsList)
112+
foreach (SelectListItem item in selectableItemsList)
107113
item.Selected = item.Value == value;
108114
}
109115
else
@@ -112,14 +118,15 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
112118
{
113119
string valueString = GetDefaultValueString(value);
114120

115-
foreach (SelectListItem item in itemsList.Where(x => x.Value == valueString))
121+
foreach (SelectListItem item in selectableItemsList.Where(x => x.Value == valueString))
116122
item.Selected = true;
117123
}
118124
}
119125
}
120126
else
121127
{
122-
for (int i = 0; i < itemsList.Count; i++)
128+
// TODO: set default value to all selected
129+
for (int i = 0; i < selectableItemsList.Count; i++)
123130
itemsList[i].Selected = (i == 0);
124131
}
125132
}
@@ -129,6 +136,7 @@ public static GriddlyFilterList FilterList(this GriddlyColumn column, IEnumerabl
129136
Field = field,
130137
Default = defaultValue,
131138
Items = itemsList,
139+
SelectableItems = selectableItemsList,
132140
Name = name,
133141
IsMultiple = isMultiple,
134142
IsNoneAll = isNoneAll,
@@ -183,35 +191,33 @@ public static GriddlyFilterList FilterEnum<T>(this GriddlyColumn column, T?[] de
183191
return column.FilterList(Extensions.ToSelectListItems<T>(), defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, name);
184192
}
185193

186-
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool isMultiple = false, bool defaultSelectAll = false, string nullItemText = null, bool isNoneAll = false, string trueLabel = "Yes", string falseLabel = "No", string field = null, string name = null)
194+
public static GriddlyFilterList FilterBool(this GriddlyColumn column, string trueLabel = "Yes", string falseLabel = "No", string nullItemText = null, bool isMultiple = false, bool defaultSelectAll = false, bool isNoneAll = false, string field = null, string name = null)
187195
{
188-
return column.FilterBool((object)null, isMultiple, defaultSelectAll, nullItemText, isNoneAll, trueLabel, falseLabel, field, name);
196+
return column.FilterList(BuildBoolItems(trueLabel, falseLabel), (object)null, isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, name);
189197
}
190198

191-
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool? defaultValue, bool isMultiple = false, bool defaultSelectAll = false, string nullItemText = null, bool isNoneAll = false, string trueLabel = "Yes", string falseLabel = "No", string field = null, string name = null)
199+
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool? defaultValue, string trueLabel = "Yes", string falseLabel = "No", string nullItemText = null, bool isMultiple = false, bool defaultSelectAll = false, bool isNoneAll = false, string field = null, string name = null)
192200
{
193-
return column.FilterBool(defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, trueLabel, falseLabel, field, name);
201+
return column.FilterList(BuildBoolItems(trueLabel, falseLabel), defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, name);
194202
}
195203

196-
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool[] defaultValue, bool isMultiple = false, bool defaultSelectAll = false, string nullItemText = null, bool isNoneAll = false, string trueLabel = "Yes", string falseLabel = "No", string field = null, string name = null)
204+
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool[] defaultValue, string trueLabel = "Yes", string falseLabel = "No", string nullItemText = null, bool isMultiple = false, bool defaultSelectAll = false, bool isNoneAll = false, string field = null, string name = null)
197205
{
198-
return column.FilterBool(defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, trueLabel, falseLabel, field, name);
206+
return column.FilterList(BuildBoolItems(trueLabel, falseLabel), defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, name);
199207
}
200208

201-
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool?[] defaultValue, bool isMultiple = false, bool defaultSelectAll = false, string nullItemText = null, bool isNoneAll = false, string trueLabel = "Yes", string falseLabel = "No", string field = null, string name = null)
209+
public static GriddlyFilterList FilterBool(this GriddlyColumn column, bool?[] defaultValue, string trueLabel = "Yes", string falseLabel = "No", string nullItemText = null, bool isMultiple = false, bool defaultSelectAll = false, bool isNoneAll = false, string field = null, string name = null)
202210
{
203-
return column.FilterBool(defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, trueLabel, falseLabel, field, name);
211+
return column.FilterList(BuildBoolItems(trueLabel, falseLabel), defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, name);
204212
}
205213

206-
static GriddlyFilterList FilterBool(this GriddlyColumn column, object defaultValue, bool isMultiple = false, bool defaultSelectAll = false, string nullItemText = null, bool isNoneAll = false, string trueLabel = "Yes", string falseLabel = "No", string field = null, string name = null)
214+
static List<SelectListItem> BuildBoolItems(string trueLabel, string falseLabel)
207215
{
208-
List<SelectListItem> items = new List<SelectListItem>()
216+
return new List<SelectListItem>()
209217
{
210218
new SelectListItem() { Value = "True", Text = trueLabel },
211219
new SelectListItem() { Value = "False", Text = falseLabel },
212220
};
213-
214-
return column.FilterList(items, defaultValue, isMultiple, defaultSelectAll, nullItemText, isNoneAll, field, name);
215221
}
216222
}
217223
}

Griddly.Mvc/GriddlySettingsResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public static GriddlySettings GetSettings(ControllerContext context, string view
4545
{
4646
GriddlySettingsResult settingsResult = new GriddlySettingsResult()
4747
{
48-
ViewName = viewName
48+
ViewName = viewName,
49+
ViewData = context.Controller.ViewData
4950
};
5051

5152
ControllerContext settingsContext = new ControllerContext(new RequestContext(new EmptyHttpContext(), context.RouteData), context.Controller);

Griddly.Mvc/SelectListItemGroup.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
using System.Web.Mvc;
3+
4+
namespace Griddly.Mvc
5+
{
6+
public class SelectListItemGroup : SelectListItem
7+
{
8+
public List<SelectListItem> Items { get; set; }
9+
}
10+
}

Griddly/Content/griddly.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@
131131
padding: 3px 6px; white-space: normal
132132
}
133133

134+
.filter-content .dropdown-menu li.griddly-list-group-header
135+
{
136+
padding-left:2px;
137+
}
138+
139+
.filter-content .dropdown-menu li.griddly-list-group a
140+
{
141+
padding-left:16px;
142+
}
143+
134144
.filter-content .dropdown-menu li a input
135145
{
136146
display:none

Griddly/Scripts/griddly.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,18 +546,25 @@
546546
}
547547
else if (filter.hasClass("griddly-filter-list"))
548548
{
549-
if (filter.data("griddly-filter-ismultiple") && !dontHide)
549+
if (!filter.data("griddly-filter-ismultiple") && !dontHide)
550550
filter.find(".filter-trigger").popover("hide");
551551

552-
var allItems = content.find("li");
552+
var allItems = content.find("li:not(.griddly-list-group-header)");
553553
var selectedItems = allItems.filter(".griddly-filter-selected");
554+
var displayItemCount = parseInt(filter.data("griddly-filter-displayitemcount"));
554555

555556
if (selectedItems.length == allItems.length || (selectedItems.length == 0 && filter.data("griddly-filter-isnoneall")))
556557
display = (allItems.length == 2 ? "Both " : "All ") + filter.data("filter-name-plural");
557-
else if (selectedItems.length > 1)
558+
else if (selectedItems.length > displayItemCount)
558559
display = selectedItems.length + " " + filter.data("filter-name-plural");
559-
else if (selectedItems.length == 1)
560-
display = selectedItems.find("a").text();
560+
else if (selectedItems.length > 0 && selectedItems.length <= displayItemCount)
561+
{
562+
var itemTexts = selectedItems.find("a");
563+
var display = $.trim($(itemTexts[0]).text());
564+
565+
for (var i = 1; i < selectedItems.length && i < displayItemCount; i++)
566+
display += ", " + $.trim($(itemTexts[i]).text());
567+
}
561568
else
562569
display = "No " + filter.data("filter-name-plural");
563570
}
@@ -643,7 +650,7 @@
643650
{
644651
var content = filter.data("griddly-filter-content");
645652

646-
content.find(".dropdown-menu li").not(item).removeClass("griddly-filter-selected");
653+
content.find(".dropdown-menu li:not(.griddly-list-group-header)").not(item).removeClass("griddly-filter-selected");
647654
content.find("input").not(checkbox).prop("checked", false);
648655

649656
item.addClass("griddly-filter-selected");
@@ -658,7 +665,7 @@
658665

659666
$(el).click(function ()
660667
{
661-
$(this).parents(".filter-content").find(".dropdown-menu li").addClass("griddly-filter-selected");
668+
$(this).parents(".filter-content").find(".dropdown-menu li:not(.griddly-list-group-header)").addClass("griddly-filter-selected");
662669
$(this).parents(".filter-content").find("input").prop("checked", true).first().change();
663670
});
664671
}, this));
@@ -669,7 +676,7 @@
669676

670677
$(el).click(function ()
671678
{
672-
$(this).parents(".filter-content").find(".dropdown-menu li").removeClass("griddly-filter-selected");
679+
$(this).parents(".filter-content").find(".dropdown-menu li:not(.griddly-list-group-header)").removeClass("griddly-filter-selected");
673680
$(this).parents(".filter-content").find("input").prop("checked", false).first().change();
674681
});
675682
}, this));
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11

22
@{
33
ViewBag.Title = "TestGrid";
4+
5+
List<SelectListItem> groupedList = new List<SelectListItem>()
6+
{
7+
new SelectListItemGroup()
8+
{
9+
Text = "Group1",
10+
Items = new List<SelectListItem>()
11+
{
12+
new SelectListItem() { Text = "Item1", Value = "Item1" },
13+
new SelectListItem() { Text = "Item2", Value = "Item2" },
14+
new SelectListItem() { Text = "Item3", Value = "Item3" },
15+
new SelectListItem() { Text = "Item4", Value = "Item4" },
16+
new SelectListItem() { Text = "Item5", Value = "Item5" },
17+
}
18+
},
19+
new SelectListItemGroup()
20+
{
21+
Text = "Group2",
22+
Items = new List<SelectListItem>()
23+
{
24+
new SelectListItem() { Text = "Item6", Value = "Item6" },
25+
new SelectListItem() { Text = "Item7", Value = "Item7" },
26+
new SelectListItem() { Text = "Item8", Value = "Item8" },
27+
new SelectListItem() { Text = "Item9", Value = "Item9" },
28+
new SelectListItem() { Text = "Item10", Value = "Item10" },
29+
}
30+
}
31+
};
432
}
533

634
@Html.Griddly(new GriddlySettings<TestGridItem>()
@@ -10,6 +38,15 @@
1038
.Column(x => x.FirstName, "First Name")
1139
.Column(x => x.LastName, "Last Name", defaultSort: SortDirection.Ascending)
1240
.Column(x => x.City, "City", filter: x => x.FilterEnum<Title>())
13-
.Column(x => x.State, "State", filter: x => x.FilterList(TestGridItem.UsStates))
14-
.Column(x => x.PostalCode, "Gender", filter: x => x.FilterBool(nullItemText:"none"))
41+
.Column(x => x.State, "State", filter: x =>
42+
{
43+
GriddlyFilterList filter = x.FilterList(TestGridItem.UsStates, new string[] { "AZ", "VA" });
44+
45+
filter.NamePlural = "State";
46+
filter.DisplayItemCount = 2;
47+
48+
return filter;
49+
})
50+
.Column(x => x.Address, "Address", filter: x => x.FilterList(groupedList))
51+
.Column(x => x.PostalCode, "Gender", filter: x => x.FilterBool(false, "YEAHH", "Naw", "what?"))
1552
)

0 commit comments

Comments
 (0)