public override void Process(TagHelperContext context, TagHelperOutput output) { (_htmlHelper as IViewContextAware).Contextualize(ViewContext); var prefix = ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix; var htmlId = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : _htmlHelper.IdForModel() + "_" + ModelItem.Metadata.PropertyName; var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName; output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last(); if (!FieldName.IsEmpty()) { htmlId = FieldName; htmlName = FieldName; } if (!LabelText.IsEmpty()) { labelText = LabelText; } var initalJson = string.Empty; var selectionJs = string.Empty; if (ItemList != null && ItemList.Count > 0) { var json = "["; var fistItem = true; foreach (var value in ItemList) { if (!fistItem) { json += ","; } json += "{id: " + value.Id + ", text: \"" + value.Name + "\"}"; fistItem = false; } json += "]"; initalJson = string.Format("$('#{0}').select2('data', {1});", htmlId, json); } else { selectionJs = $@"initSelection: function(element, callback) {{ var id = $(element).val(); if (id != '') {{ $.ajax('{LoaderUrl}/' + id, {{ dataType: 'json' }}).done(function(data) {{ callback(data); }}); }} }}"; } string js = DisableJs ? "" : $@"<script type=""text/javascript""> $(function () {{ $(""#{htmlId}"").select2({{ placeholder: ""{PlaceHolderText}"", {(CanClearSelect ? "allowClear: true, " : "")} {(MultiSelect ? "multiple:true," : "")} minimumInputLength: 1, ajax: {{ url: '{Url}', dataType: 'json', data: function (term, page) {{ return {{ q: term, {(!ParentComboBox.IsEmpty() ? $"id: $('#{ParentComboBox}').val()," : "")} }}; }}, results: function (data, page) {{ return {{ results: data }}; }} }}, {selectionJs} }}); {initalJson} }}); </script>"; var valHtml = ModelItem != null ? "value='" + ModelItem.Model + "'" : ""; if (DisableEditorTemplate) { if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <div class=""input-group""> <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} /> <span class=""input-group-btn""> <a href='{AddNewUrl}' class='btn btn-xs btn-primary btnAddPopup fancyboxAdd fancybox.iframe'><i class='fa fa-plus'></i></a> </span> {js} </div>"); } else { output.Content.SetHtmlContent($@" <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} /> {js}"); } } else { if (!DisableFormGroup) { output.Attributes.Add("class", "form-group"); output.Attributes.Add("id", "div" + htmlId); } else { output.TagName = "span"; } var irq = (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) || (IsRequired.HasValue && IsRequired.Value); var requiredHtml = irq ? $"<span class=\"{RequiredCssClass}\">{Symbol}</span>" : ""; var metaHtml = irq ? $"<span class=\"field-validation-valid help-block\" data-valmsg-for=\"{htmlId}\" data-valmsg-replace=\"true\"></span>" : ""; if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <label class=""col-xs-{LabelColCount} control-label"">{labelText} {requiredHtml}</label> <div class=""col-xs-{EditorColCount}""> <div class=""input-group""> <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} /> <span class=""input-group-btn""> <a href='{AddNewUrl}' class='btn btn-xs btn-primary btnAddPopup fancyboxAdd fancybox.iframe'><i class='fa fa-plus'></i></a> </span> </div> {metaHtml} </div> {js}"); } else { output.Content.SetHtmlContent($@" <label class=""col-xs-{LabelColCount} control-label"">{labelText} {requiredHtml}</label> <div class=""col-xs-{EditorColCount}""> <input id='{htmlId}' name='{htmlName}' type='text' class='form-control input-sm {CssClass}' {valHtml} /> {metaHtml} </div> {js}"); } } }
public override void Process(TagHelperContext context, TagHelperOutput output) { (_htmlHelper as IViewContextAware).Contextualize(ViewContext); var prefix = ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix; output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; if (Width > 0) { output.Attributes.Add("style", $"width:{Width}px"); } var htmlId = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : _htmlHelper.IdForModel() + "_" + ModelItem.Metadata.PropertyName; var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName; var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last(); if (ItemList == null && ItemList2 == null) { var comboDataInfo = GetComboBoxDataSource(ModelItem.ModelExplorer); ItemList = comboDataInfo.ListItems; } var isSelected = false; var sbOptions = new StringBuilder(); if (ItemList != null) { if (ItemList.DataGroupField.IsEmpty()) { foreach (var item in ItemList) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); isSelected = true; } option.InnerHtml.AppendHtml(item.Text); sbOptions.AppendLine(option.GetString()); } } else { var gList = ItemList.Select(d => d.Group).Distinct().OrderBy(d => d.Name).ToList(); foreach (var g in gList) { var optionGroup = new TagBuilder("optgroup"); optionGroup.Attributes.Add("label", g.Name); var gItemList = ItemList.Where(d => d.Group.Name == g.Name).ToList(); foreach (var item in gItemList) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); isSelected = true; } option.InnerHtml.AppendHtml(item.Text); optionGroup.InnerHtml.AppendHtml(option.GetString()); } sbOptions.AppendLine(optionGroup.GetString()); } } } else if (ItemList2 != null) { foreach (var item in ItemList2) { var option = new TagBuilder("option"); option.Attributes.Add("value", item.Value); if (item.Selected) { option.Attributes.Add("selected", "selected"); isSelected = true; } option.InnerHtml.AppendHtml(item.Text); sbOptions.AppendLine(option.GetString()); } } var select = new TagBuilder("select"); select.AddCssClass("form-control form-control-sm"); if (!CssClass.IsEmpty()) { select.AddCssClass(CssClass); } select.Attributes.Add("name", htmlName); select.Attributes.Add("id", htmlId); if (MultiSelect) { select.Attributes.Add("multiple", "multiple"); } if (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) { var requiredAttr = Digger2.GetMetaAttr <RequiredAttribute>(ModelItem.Metadata); var errMsg = ""; if (requiredAttr != null) { errMsg = requiredAttr.ErrorMessage; if (errMsg.IsEmpty() && requiredAttr.ErrorMessageResourceType != null) { var rm = new ResourceManager(requiredAttr.ErrorMessageResourceType); var rsm = rm.GetString(requiredAttr.ErrorMessageResourceName); if (rsm != null && !rsm.IsEmpty()) { errMsg = string.Format(rsm, labelText); } } } select.Attributes.Add("data-val", "true"); select.Attributes.Add("data-val-required", requiredAttr != null ? errMsg : "Gereklidir" ); } select.InnerHtml.AppendHtml(sbOptions.ToString()); var setToNull = isSelected ? "" : @$ "$(" "#{htmlId}" ").val(null); $(" "#{htmlId}" ").trigger(" "change" ");"; string js = DisableJs ? "" : $@" <script type=""text/javascript""> $(function () {{ $(""#{htmlId}"").select2({{ language: ""tr"", placeholder: ""{(PlaceHolderText.IsEmpty() ? "" : PlaceHolderText)}"", {(MultiSelect ? "multiple:true," : "")} {(Tag ? "tags:true," : "")} {(ReadOnly ? "disabled:true," : "")} {(CanClearSelect ? "allowClear: true" : "")} }}); {setToNull} }}); </script>"; if (DisableEditorTemplate) { output.Attributes.Add("id", "div" + htmlId); output.Attributes.Add("class", $"col-sm-{EditorColCount}"); if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <div class=""input-group input-group-sm""> {select.GetString()} <div class=""input-group-append""> <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe'><i class='fas fa-plus'></i></a> </div> {js} </div>"); } else { output.Content.SetHtmlContent($@"{select.GetString()} {js}"); } } else { output.Attributes.Add("class", "form-group row"); output.Attributes.Add("id", "div" + htmlId); var irq = (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) || (IsRequired.HasValue && IsRequired.Value); var requiredHtml = irq ? $"<span class=\"{RequiredCssClass}\">{Symbol}</span>" : ""; var metaHtml = irq ? $"<span class=\"field-validation-valid help-block\" data-valmsg-for=\"{htmlId}\" data-valmsg-replace=\"true\"></span>" : ""; if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <label class=""col-{LabelColCount} col-form-label col-form-label-sm"">{labelText} {requiredHtml}</label> <div class=""col-{EditorColCount}""> <div class=""input-group input-group-sm""> {select.GetString()} <div class=""input-group-append""> <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe'><i class='fas fa-plus'></i></a> </div> </div> {metaHtml} </div> {js}"); } else { output.Content.SetHtmlContent($@" <label class=""col-{LabelColCount} col-form-label col-form-label-sm"">{labelText} {requiredHtml}</label> <div class=""col-{EditorColCount}""> {select.GetString()} {metaHtml} </div> {js}"); } } }
public override void Process(TagHelperContext context, TagHelperOutput output) { (_htmlHelper as IViewContextAware).Contextualize(ViewContext); var prefix = ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix; var htmlId = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : _htmlHelper.IdForModel() + "_" + ModelItem.Metadata.PropertyName; var htmlName = prefix.IsEmpty() ? ModelItem.Metadata.PropertyName : prefix + "." + ModelItem.Metadata.PropertyName; if (ModelParentItem != null) { var parentHtmlId = prefix.IsEmpty() ? ModelParentItem.Metadata.PropertyName : _htmlHelper.IdForModel() + "_" + ModelParentItem.Metadata.PropertyName; ParentComboBox = parentHtmlId; } output.TagName = "div"; output.TagMode = TagMode.StartTagAndEndTag; if (Width > 0) { output.Attributes.Add("style", $"width:{Width}px"); } var labelText = ModelItem.Metadata.DisplayName ?? ModelItem.Metadata.PropertyName ?? htmlId.Split('.').Last(); var selectionJs = string.Empty; if (SelectedValues != null && SelectedValues.Count > 0) { var sb = new StringBuilder(); foreach (var s in SelectedValues) { sb.AppendLine($@"$('#{htmlId}').append(new Option('{s.Name}', {s.Id}, true, true));"); } sb.AppendLine($@"$('#{htmlId}').trigger('change');"); selectionJs = sb.ToString(); } else { var valId = ModelItem != null && ModelItem.Model != null?ModelItem.Model.ToString() : ""; if (!valId.IsEmpty()) { selectionJs = $@" $.ajax('{LoaderUrl}/{valId}', {{dataType: 'json'}}).done(function(data) {{ var option = new Option(data.text, data.id, true, true); $(""#{htmlId}"").append(option).trigger('change'); }});"; } } string js = DisableJs ? "" : $@"<script type=""text/javascript""> $(function () {{ $(""#{htmlId}"").select2({{ placeholder: ""{PlaceHolderText}"", language: ""tr"", {(CanClearSelect ? "allowClear: true, " : "")} {(MultiSelect ? "multiple:true," : "")} {(ReadOnly ? "disabled:true," : "")} minimumInputLength: 1, ajax: {{ url: '{Url}', data: function (params) {{ return {{ q: params.term, {(!ParentComboBox.IsEmpty() ? $"id: $('#{ParentComboBox}').val()," : "")} }}; }}, processResults: function (data) {{ return {{ results: data }}; }} }}, }}); {selectionJs} }}); </script>"; var valHtml = ModelItem != null ? "value='" + ModelItem.Model + "'" : ""; if (DisableEditorTemplate) { if (!DisableColSize) { output.Attributes.Add("class", $"col-sm-{EditorColCount}"); } output.Attributes.Add("id", "div" + htmlId); if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <div class=""input-group input-group-sm""> <select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml}> </select> <div class=""input-group-append""> <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe {AddNewButtonCssClass}'><i class='fas fa-plus'></i></a> </div> {js} </div>"); } else { output.Content.SetHtmlContent($@" <select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml}> </select> {js}"); } } else { output.Attributes.Add("class", "form-group row"); output.Attributes.Add("id", "div" + htmlId); var irq = (!IsRequired.HasValue && ModelItem.Metadata.GetIsRequired()) || (IsRequired.HasValue && IsRequired.Value); var requiredHtml = irq ? $"<span class=\"{RequiredCssClass}\">{Symbol}</span>" : ""; var metaHtml = irq ? $"<span class=\"field-validation-valid help-block\" data-valmsg-for=\"{htmlId}\" data-valmsg-replace=\"true\"></span>" : ""; if (!AddNewUrl.IsEmpty()) { var sep = "?"; if (AddNewUrl.Contains("?")) { sep = "&"; } AddNewUrl += sep + "editorId=" + htmlId; output.Content.SetHtmlContent($@" <label class=""col-{LabelColCount} col-form-label col-form-label-sm"">{labelText} {requiredHtml}</label> <div class=""col-{EditorColCount}""> <div class=""input-group input-group-sm""> <select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml}> </select> <div class=""input-group-append""> <a href='{AddNewUrl}' class='btn btn-sm btn-default btnAddPopup fancyboxAdd fancybox.iframe {AddNewButtonCssClass}'><i class='fas fa-plus'></i></a> </div> </div> {metaHtml} </div> {js}"); } else { output.Content.SetHtmlContent($@" <label class=""col-{LabelColCount} col-form-label col-form-label col-form-label-sm"">{labelText} {requiredHtml}</label> <div class=""col-{EditorColCount}""> <select id='{htmlId}' name='{htmlName}' class='form-control form-control-sm {CssClass}' {valHtml} > </select> {metaHtml} </div> {js}"); } } }