Updating SelectList shape with support for SelectListGroup.

This commit is contained in:
Sipke Schoorstra
2014-10-28 17:52:42 -07:00
parent d18bbedc42
commit 98bcf73198

View File

@@ -320,6 +320,19 @@ namespace Orchard.Forms.Shapes {
return option.ToString(TagRenderMode.Normal);
}
private static string ListGroupToOption(string name = null, bool isFirstGroup = false, bool finalize = false) {
var option = new TagBuilder("optgroup");
option.Attributes["label"] = name;
if(isFirstGroup)
return option.ToString(TagRenderMode.StartTag);
if(finalize)
return option.ToString(TagRenderMode.EndTag);
return String.Concat(option.ToString(TagRenderMode.EndTag), option.ToString(TagRenderMode.StartTag));
}
private static object GetSelectProperty(object obj, string propertyName) {
if (string.IsNullOrEmpty(propertyName)) {
return obj.ToString();
@@ -358,10 +371,13 @@ namespace Orchard.Forms.Shapes {
string selectedValue = Convert.ToString(Shape.Value) ?? "";
var selectedValues = selectedValue.Split(new[] { ',' });
var isFirstGroup = true;
var hasGroups = false;
foreach (var item in Items) {
var selectItem = item as SelectListItem;
if (selectItem == null) {
var selectGroup = item as SelectListGroup;
if (selectItem == null && selectGroup == null) {
selectItem = new SelectListItem();
if (item is string) {
var itemStr = (string)item;
@@ -377,13 +393,22 @@ namespace Orchard.Forms.Shapes {
}
}
}
else {
else if (selectItem != null) {
selectItem.Selected = selectedValues.Any(x => x == selectItem.Value);
}
Output.WriteLine(ListItemToOption(selectItem));
if (selectGroup != null) {
isFirstGroup = isFirstGroup == false;
hasGroups = true;
Output.WriteLine(ListGroupToOption(selectGroup.Name, isFirstGroup));
}
else
Output.WriteLine(ListItemToOption(selectItem));
}
if(hasGroups)
Output.WriteLine(ListGroupToOption(finalize: true));
Output.WriteLine(select.ToString(TagRenderMode.EndTag));
}