Пример #1
0
        private void AppendInitializeFunction(StringBuilder sb, DataTableContext tableContext,
                                              string initializeFunctionName)
        {
            sb.AppendLine("function " + initializeFunctionName + "(){");
            sb.AppendLine($"$('#{TableId}').DataTable({{");

            var localizationUrl = _localizer["LocalizationUrl"];

            if (!string.IsNullOrEmpty(localizationUrl))
            {
                sb.AppendLine($"language: {{url: \"{localizationUrl}\"}},");
            }

            if (ServerSide)
            {
                AppendServerSideProcessingData(sb);
            }
            else if (!string.IsNullOrEmpty(JsonData))
            {
                AppendData(sb);
            }
            else
            {
                throw new NotImplementedException();
            }

            AppendColumns(sb, tableContext);

            sb.AppendLine("});");
            sb.AppendLine("}");
        }
Пример #2
0
        private void AppendTableHtml(StringBuilder sb, DataTableContext tableContext, TagHelperContext context)
        {
            Contract.Requires(sb != null && tableContext != null && context != null);

            var classes = "";

            if (context.AllAttributes.TryGetAttribute("class", out TagHelperAttribute classTag))
            {
                classes = classTag.Value.ToString();
            }

            sb.AppendLine($"<table id=\"{TableId}\" class=\"{classes}\" cellspacing=\"0\" width=\"100%\">");
            sb.AppendLine("<thead>");
            sb.AppendLine("<tr>");
            if (tableContext.ColumnsProperties != null)
            {
                foreach (var columnProperty in tableContext.ColumnsProperties)
                {
                    sb.AppendLine($"<th>{columnProperty.Metadata?.DisplayName}</th>");
                }
            }
            if (tableContext.ActionDataSet != null && tableContext.ActionDataSet.Any())
            {
                sb.AppendLine("<th></th>");
            }
            sb.AppendLine("</tr>");
            sb.AppendLine("</thead>");
            sb.AppendLine("</table>");
        }
Пример #3
0
        private async Task <DataTableContext> GetTableContextAsync(TagHelperContext context, TagHelperOutput output)
        {
            Contract.Requires(context != null && output != null);

            var tableContext = new DataTableContext {
                ModelExplorer = GetModelExplorer(ModelType)
            };

            context.Items.Add(typeof(DataTableTagHelper), tableContext);

            await output.GetChildContentAsync();

            return(tableContext);
        }
Пример #4
0
        private void AppendColumns(StringBuilder sb, DataTableContext tableContext)
        {
            Contract.Requires(sb != null && tableContext != null);

            sb.AppendLine("columns: [");
            if (tableContext.ColumnsProperties != null)
            {
                foreach (var columnProperty in tableContext.ColumnsProperties)
                {
                    var columnName = Char.ToLowerInvariant(columnProperty.Name[0]) + columnProperty.Name.Substring(1);
                    if (columnProperty.Metadata.ModelType == typeof(DateTime) || columnProperty.Metadata.ModelType == typeof(DateTime?))
                    {
                        sb.AppendLine($"{{ \"data\": \"{columnName}\",");
                        sb.AppendLine("\"render\": function(data){");
                        sb.AppendLine("if (data == null) return data;");
                        sb.AppendLine("var d = new Date(data);");
                        sb.AppendLine("return d.toLocaleString();");
                        sb.AppendLine("}},");
                    }
                    else if (columnProperty.Metadata.ModelType == typeof(decimal) || columnProperty.Metadata.ModelType == typeof(decimal?))
                    {
                        sb.AppendLine($"{{ \"data\": \"{columnName}\",");
                        sb.AppendLine("\"render\": function(data){");
                        sb.AppendLine("return data.toLocaleString();");
                        sb.AppendLine("}},");
                    }
                    else
                    {
                        sb.AppendLine($"{{ \"data\": \"{columnName}\",");
                        sb.AppendLine("render: $.fn.dataTable.render.text()},");
                    }
                }
            }
            if (tableContext.ActionDataSet != null && tableContext.ActionDataSet.Any())
            {
                sb.AppendLine(GetColumnActionContent(tableContext.ActionDataSet));
            }
            sb.AppendLine("],");
        }