mirror of https://github.com/abpframework/abp.git
10 changed files with 2403 additions and 21 deletions
@ -0,0 +1,12 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form |
||||
|
{ |
||||
|
[AttributeUsage(AttributeTargets.Property)] |
||||
|
public class AbpRadioButton : Attribute |
||||
|
{ |
||||
|
public bool Inline { get; set; } = false; |
||||
|
|
||||
|
public bool Disabled { get; set; } = false; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using JetBrains.Annotations; |
||||
|
using Microsoft.AspNetCore.Mvc.Rendering; |
||||
|
using Microsoft.AspNetCore.Mvc.ViewFeatures; |
||||
|
using Microsoft.AspNetCore.Razor.TagHelpers; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form |
||||
|
{ |
||||
|
public class AbpRadioInputTagHelper : AbpTagHelper<AbpRadioInputTagHelper, AbpRadioInputTagHelperService> |
||||
|
{ |
||||
|
public ModelExpression AspFor { get; set; } |
||||
|
|
||||
|
public string Label { get; set; } |
||||
|
|
||||
|
public bool? Inline { get; set; } |
||||
|
|
||||
|
public bool? Disabled { get; set; } |
||||
|
|
||||
|
public IEnumerable<SelectListItem> AspItems { get; set; } |
||||
|
|
||||
|
[HtmlAttributeNotBound] |
||||
|
[ViewContext] |
||||
|
public ViewContext ViewContext { get; set; } |
||||
|
|
||||
|
public AbpRadioInputTagHelper(AbpRadioInputTagHelperService tagHelperService) |
||||
|
: base(tagHelperService) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,125 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Reflection; |
||||
|
using System.Text; |
||||
|
using Microsoft.AspNetCore.Mvc.Rendering; |
||||
|
using Microsoft.AspNetCore.Mvc.ViewFeatures; |
||||
|
using Microsoft.AspNetCore.Razor.TagHelpers; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form |
||||
|
{ |
||||
|
public class AbpRadioInputTagHelperService : AbpTagHelperService<AbpRadioInputTagHelper> |
||||
|
{ |
||||
|
public override void Process(TagHelperContext context, TagHelperOutput output) |
||||
|
{ |
||||
|
var selectItems = GetSelectItems(context,output); |
||||
|
|
||||
|
var order = GetInputOrder(TagHelper.AspFor.ModelExplorer); |
||||
|
|
||||
|
var html = GetHtml(context, output, selectItems); |
||||
|
|
||||
|
|
||||
|
AddGroupToFormGroupContents(context, TagHelper.AspFor.Name, html, order, out var surpress); |
||||
|
|
||||
|
if (surpress) |
||||
|
{ |
||||
|
output.SuppressOutput(); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
output.TagName = "div"; |
||||
|
output.Attributes.Clear(); |
||||
|
output.TagMode = TagMode.StartTagAndEndTag; |
||||
|
output.Content.SetHtmlContent(html); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetHtml(TagHelperContext context, TagHelperOutput output, List<SelectListItem> selectItems) |
||||
|
{ |
||||
|
var html = new StringBuilder(""); |
||||
|
|
||||
|
foreach (var selectItem in selectItems) |
||||
|
{ |
||||
|
var inlineClass = (TagHelper.Inline ?? false) ? " custom-control-inline" : ""; |
||||
|
var id = TagHelper.AspFor.Name + "Radio" + selectItem.Value; |
||||
|
var name = TagHelper.AspFor.Name; |
||||
|
var selected = selectItem.Selected ? " checked=\"checked\"" : ""; |
||||
|
var disabled = (TagHelper.Disabled??false) ? " disabled" : ""; |
||||
|
|
||||
|
var htmlPart = "<div class=\"custom-control custom-radio" + inlineClass + "\">\r\n" + |
||||
|
" <input type=\"radio\" id=\"" + id + "\" name=\"" + name + "\" value=\"" + selectItem.Value + "\"" + selected + " class=\"custom-control-input\""+ disabled + ">\r\n" + |
||||
|
" <label class=\"custom-control-label\" for=\"" + id + "\">" + selectItem.Text + "</label>\r\n" + |
||||
|
"</div>"; |
||||
|
|
||||
|
html.AppendLine(htmlPart); |
||||
|
} |
||||
|
|
||||
|
return html.ToString(); |
||||
|
} |
||||
|
|
||||
|
protected virtual List<SelectListItem> GetSelectItems(TagHelperContext context, TagHelperOutput output) |
||||
|
{ |
||||
|
var selectItems = TagHelper.AspItems?.ToList(); |
||||
|
|
||||
|
if (TagHelper.AspItems == null && |
||||
|
!GetSelectItemsIfProvidedByEnum(context, output, TagHelper.AspFor.ModelExplorer, out selectItems) && |
||||
|
!GetSelectItemsIfProvidedFromAttribute(context, output, TagHelper.AspFor.ModelExplorer, out selectItems)) |
||||
|
{ |
||||
|
throw new Exception("No items provided for select attribute."); |
||||
|
} |
||||
|
|
||||
|
SetSelectedValue(context, output, selectItems); |
||||
|
|
||||
|
return selectItems; |
||||
|
} |
||||
|
|
||||
|
protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems) |
||||
|
{ |
||||
|
selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static) |
||||
|
.Select((t, i) => new SelectListItem { Value = i.ToString(), Text = t.Name }).ToList() : null; |
||||
|
|
||||
|
return selectItems != null; |
||||
|
} |
||||
|
|
||||
|
protected virtual bool GetSelectItemsIfProvidedFromAttribute(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List<SelectListItem> selectItems) |
||||
|
{ |
||||
|
selectItems = GetAttribute<SelectItems>(explorer)?.GetItems(explorer)?.ToList(); |
||||
|
|
||||
|
return selectItems != null; |
||||
|
} |
||||
|
|
||||
|
protected virtual void SetSelectedValue(TagHelperContext context, TagHelperOutput output, List<SelectListItem> selectItems) |
||||
|
{ |
||||
|
var selectedValue = GetSelectedValue(context, output); |
||||
|
|
||||
|
if (!selectItems.Any(si => si.Selected)) |
||||
|
{ |
||||
|
var itemToBeSelected = selectItems.FirstOrDefault(si => si.Value.ToString() == selectedValue); |
||||
|
|
||||
|
if (itemToBeSelected != null) |
||||
|
{ |
||||
|
itemToBeSelected.Selected = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
protected virtual string GetSelectedValue(TagHelperContext context, TagHelperOutput output) |
||||
|
{ |
||||
|
if (TagHelper.AspFor.ModelExplorer.Metadata.IsEnum) |
||||
|
{ |
||||
|
var baseType = TagHelper.AspFor.ModelExplorer.Model?.GetType().GetEnumUnderlyingType(); |
||||
|
|
||||
|
if (baseType == null) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var valueAsString = Convert.ChangeType(TagHelper.AspFor.ModelExplorer.Model, baseType); |
||||
|
return valueAsString != null ? valueAsString.ToString() : ""; |
||||
|
} |
||||
|
|
||||
|
return TagHelper.AspFor.ModelExplorer.Model?.ToString(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,8 +0,0 @@ |
|||||
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form |
|
||||
{ |
|
||||
public enum SelectType |
|
||||
{ |
|
||||
Dropdown, |
|
||||
Radio |
|
||||
} |
|
||||
} |
|
||||
File diff suppressed because it is too large
Loading…
Reference in new issue