Exemplo n.º 1
0
 private void checkBoxFiliter4_CheckedChanged(object sender, EventArgs e)
 {
     Filter4?.Invoke();
 }
Exemplo n.º 2
0
 private static string RenderFilterRowFooter(Filter4 filter, int colCount)
 {
     return(filter.ActiveFilter(colCount));
 }
Exemplo n.º 3
0
        public static HtmlString DataGrid4 <T>(this IHtmlHelper helper, IEnumerable <T> data, DataColumn[] columns, DataGridOptions4 option)
            where T : class
        {
            var _query           = helper.ViewContext.HttpContext.Request.QueryString;
            var _queryDictionary = QueryHelpers.ParseQuery(_query.ToString());
            var _queryItems      = _queryDictionary.SelectMany(x => x.Value, (col, value) => new KeyValuePair <string, string>(col.Key, value)).ToList();
            var _queryParams     = _query.ToUriComponent();

            if (!option.PagerAndShortAction.IsEmpty())
            {
                var psss = option.PagerAndShortAction.Split('?');
                if (psss.Length > 1)
                {
                    _queryParams = _queryParams.Replace("?" + psss[1], "");
                }
            }

            var _colCount = 0;
            var items     = data;

            if (items == null)
            {
                var content = new HtmlContentBuilder().AppendHtml(option.EmptyData);
                return(new HtmlString(content.GetString()));
            }

            var _sorter = new Sorter(_queryItems, option.SortKeyword, option.PagerAndShortAction);
            var _filter = new Filter4(_queryDictionary, _queryItems, option.PagerKeyword,
                                      helper.ViewContext.RouteData, option.PagerAndShortAction,
                                      helper, option.PagerJsFunction, option.GridDiv);

            if (option.FilteringEnabled && !(data is PagedList <T>) && !(data is StaticPagedList <T>))
            {
                var kp = _filter.GetWhereString();
                if (kp.Where != null)
                {
                    items = items.AsQueryable().Where(kp.Where, kp.Params.ToArray());
                }
            }

            #region SORT DATA
            if (option.Sortable && !(data is PagedList <T>) && !(data is StaticPagedList <T>))
            {
                if (_sorter.List.Count == 0 && option.DefaultSorting)
                {
                    if (!option.SortDefaultFieldName.IsEmpty())
                    {
                        _sorter.List.Add(new SorterInfo()
                        {
                            Direction = option.SortDefaultDirection, Keyword = option.SortDefaultFieldName
                        });
                    }
                    //if (option.SortDefaultFieldNames != null)
                    //{
                    //    foreach (string sortinfodefaultname in option.SortDefaultFieldNames)
                    //    {
                    //        _sorter.List.Add(new SorterInfo()
                    //        {
                    //            Direction = option.SortDefaultDirection,
                    //            Keyword = sortinfodefaultname
                    //        });
                    //    }
                    //}
                }
                var sorting = string.Empty;
                foreach (var info in _sorter.List)
                {
                    if (!sorting.IsEmpty())
                    {
                        sorting += ",";
                    }
                    sorting += String.Format("{0} {1}", info.Keyword, info.Direction);
                }
                if (!sorting.IsEmpty())
                {
                    items = items.AsQueryable().OrderBy(sorting);
                }
            }
            #endregion

            #region DO PAGING
            var currentPageSize      = option.DefaultPageSize;
            var currentPageIndexItem = _queryItems.FirstOrDefault(d => d.Key == option.PagerKeyword);
            var pageIndex            = 0;
            if (!currentPageIndexItem.Value.IsEmpty())
            {
                int.TryParse(currentPageIndexItem.Value, out pageIndex);
            }
            if (option.PagerEnabled && !(data is PagedList <T>) && !(data is StaticPagedList <T>))
            {
                var currentPageSizeStr = _queryItems.FirstOrDefault(d => d.Key == option.PageSizeKeyword);
                if (!currentPageSizeStr.Value.IsEmpty())
                {
                    int.TryParse(currentPageSizeStr.Value, out currentPageSize);
                }
                items          = new PagedList <T>(items, pageIndex, currentPageSize);
                TotalRowCount  = (items as PagedList <T>).TotalItemCount;
                TotalPageCount = (items as PagedList <T>).PageCount;
            }
            #endregion

            T firstItem = default;
            if (items != null && items.Any())
            {
                firstItem = items.First();
            }
            Type firstItemType = null;
            if (firstItem != null)
            {
                firstItemType = firstItem.GetType();
            }

            var writer      = new HtmlContentBuilder();
            var baseUrl     = LibGeneral.GetContentUrl(helper.ViewContext.RouteData);
            var fs          = $@"/{baseUrl}/{option.PagerAndShortAction}{_queryParams}";
            var _refreshUrl = $"{option.PagerJsFunction}(\"{fs}\",\"{option.GridDiv}\");";

            if (option.PagerEnabled && (option.PagerLocation == PagerLocationTypes.Top || option.PagerLocation == PagerLocationTypes.TopAndBottom))
            {
                if (data is PagedList <T> || data is StaticPagedList <T> )
                {
                    var pager = RenderPager(helper, data, option, currentPageSize, _queryItems);
                    writer.AppendHtmlLine(pager);
                }
                else
                {
                    var pager = RenderPager(helper, items, option, currentPageSize, _queryItems);
                    writer.AppendHtmlLine(pager);
                }
            }

            var cssDiv = string.Empty;
            if (option.Responsive)
            {
                cssDiv = $" class=\"table-responsive\"";
            }
            writer.AppendHtmlLine($"<div id='dataGrid-{option.GridDiv}' {cssDiv} data-refreshurl='{_refreshUrl}'> ");

            var cssTable = string.Empty;
            if (!option.CssTable.IsEmpty())
            {
                cssTable = $" class=\"{option.CssTable}\"";
            }
            var idTable = string.Empty;
            if (!option.TableId.IsEmpty())
            {
                idTable = $" id=\"{option.TableId}\"";
            }
            writer.AppendHtmlLine(RenderHeaderBar2(option));
            writer.AppendHtmlLine($"<table{idTable}{cssTable}>");

            if (option.ShowHeader)
            {
                writer.AppendHtmlLine($"<thead>");
                _colCount = columns.Count(d => d.Visible);
                if (option.ButtonDeleteEnabled || option.ButtonEditEnabled || option.ButtonRefreshEnabled)
                {
                    _colCount++;
                }
                if (option.FilteringEnabled)
                {
                    writer.AppendHtmlLine(RenderFilterRow(helper, columns, firstItemType, _filter));
                }
                writer.AppendHtmlLine(RenderHeader <T>(helper, columns, option, firstItemType, _refreshUrl, _sorter, _filter));
                writer.AppendHtmlLine($"</thead>");
            }

            writer.AppendHtmlLine($"<tbody>");
            foreach (var item in items)
            {
                writer.AppendHtmlLine(RenderRow(helper, columns, item, option));
            }

            writer.AppendHtmlLine($"</tbody>");
            writer.AppendHtmlLine("</table>");
            writer.AppendHtmlLine("</div>");

            if (option.FilteringEnabled && columns.Any(d => !d.FilterValue.IsEmpty()))
            {
                writer.AppendHtmlLine(RenderFilterRowFooter(_filter, _colCount));
            }

            if (option.PagerEnabled && (option.PagerLocation == PagerLocationTypes.Bottom || option.PagerLocation == PagerLocationTypes.TopAndBottom))
            {
                if (data is PagedList <T> || data is StaticPagedList <T> )
                {
                    var pager = RenderPager(helper, data, option, currentPageSize, _queryItems);
                    writer.AppendHtmlLine(pager);
                }
                else
                {
                    var pager = RenderPager(helper, items, option, currentPageSize, _queryItems);
                    writer.AppendHtmlLine(pager);
                }
            }

            return(new HtmlString(writer.GetString()));
        }
Exemplo n.º 4
0
        private static string RenderFilterRow(IHtmlHelper helper, IEnumerable <DataColumn> columns, Type firstItemType, Filter4 filter)
        {
            var builder = new HtmlContentBuilder();

            foreach (var field in columns.Where(d => d.Visible))
            {
                if (!string.IsNullOrWhiteSpace(field.FieldName) || !string.IsNullOrWhiteSpace(field.FilterField))
                {
                    builder.AppendHtml(filter.GetFilterDiv(field, firstItemType));
                }
            }
            return(builder.GetString());
        }
Exemplo n.º 5
0
        private static string RenderHeader <T>(IHtmlHelper helper, IEnumerable <DataColumn> columns, DataGridOptions4 option, Type firstItemType,
                                               string refreshUrl, Sorter sorter, Filter4 filter)
        {
            var builder = new HtmlContentBuilder();

            builder.AppendHtml("<tr>");
            var urlOperator = option.PagerAndShortAction.IndexOf("?") > -1 ? "&" : "?";

            if (option.ButtonShowEnabled || option.ButtonDeleteEnabled || option.ButtonEditEnabled || option.Buttons.Count > 0 || option.ButtonRefreshEnabled)
            {
                var width = string.Empty;
                if (option.ButtonColumnWidth > 0)
                {
                    width = " width='" + option.ButtonColumnWidth + "'";
                }
                builder.AppendHtml($"<th{width}>");
                builder.AppendHtml("<div class='btn-group'>");
                if (option.ButtonInsertEnabled)
                {
                    var action = option.ButtonInsertAction;
                    if (option.GridDiv != "divList")
                    {
                        if (action.Contains("?"))
                        {
                            action += "&";
                        }
                        else
                        {
                            action += "?";
                        }
                        action += "grid=" + option.GridDiv;
                    }
                    var buttonClass = option.InsertPopup ? option.ButtonInsertPopupCss : option.ButtonInsertCss;
                    var pop         = option.InsertPopup ? option.Popup : "";
                    builder.AppendHtml(string.Format("<a href='{0}/{1}' class='{2}' {4}>{3}</a>",
                                                     $"/{LibGeneral.GetContentUrl(helper.ViewContext.RouteData)}",
                                                     action,
                                                     buttonClass,
                                                     "<i class=\"fas fa-plus\"></i>",
                                                     pop));
                }
                if (option.ButtonRefreshEnabled)
                {
                    builder.AppendHtml($"<a onClick='{refreshUrl}' id='btnGridRefresh-{option.GridDiv}' class='{option.ButtonRefreshCss}'><i class=\"fas fa-sync-alt\"></i></a>");
                }
                builder.AppendHtml("</div>");
                builder.AppendHtml("</th>");
            }
            foreach (var field in columns.Where(d => d.Visible && d.DataColumnType == DataColumnTypes.CommandColumn))
            {
                var cssClass = string.Empty;
                if (!field.HeaderCssClass.IsEmpty())
                {
                    cssClass = $" class='{field.HeaderCssClass}'";
                }
                var width = string.Empty;
                if (field.Width > 0)
                {
                    width = $" width='{field.Width}'";
                }
                if (field.Width > 0)
                {
                    builder.AppendHtml($"<th{width}{cssClass}>");
                }
                else
                {
                    builder.AppendHtml($"<th{cssClass}>");
                }
                builder.AppendHtml($"<span class='textHeader'>{field.Caption}</span>");
                builder.AppendHtml("</th>");
            }
            foreach (var field in columns.Where(d => d.Visible && d.DataColumnType != DataColumnTypes.CommandColumn))
            {
                var cssClass = string.Empty;
                if (!field.HeaderCssClass.IsEmpty())
                {
                    cssClass = $" class='{field.HeaderCssClass}'";
                }
                var width = string.Empty;
                if (field.Width > 0)
                {
                    width = $" width='{field.Width}'";
                }
                if (field.Width > 0)
                {
                    builder.AppendHtml($"<th{width}{cssClass}>");
                }
                else
                {
                    builder.AppendHtml($"<th{cssClass}>");
                }
                if (field.DataColumnType == DataColumnTypes.BoundColumn)
                {
                    if (field.Caption.IsEmpty())
                    {
                        field.Caption = Digger.GetDisplayName(typeof(T), field.FieldName);
                    }
                    builder.AppendHtml("<div class='tableDiv'>");
                    if (option.Sortable && field.Sortable)
                    {
                        var sortFieldName = field.SortField.IsEmpty() ? field.FieldName : field.SortField;
                        var sortImg       = sorter.GetSortImg4(sortFieldName);
                        var sortParam     = sorter.GetSortParam(sortFieldName, option.SortDefaultFieldName, option.SortDefaultDirection);

                        var url = $@"/{LibGeneral.GetContentUrl(helper.ViewContext.RouteData)}/{option.PagerAndShortAction}{urlOperator}{option.SortKeyword}={sortParam}&{sorter.CleanQueryString}";

                        builder.AppendHtml($"<a class='textHeader' href=\"javascript:{option.PagerJsFunction}('{url}', '{option.GridDiv}');\">{field.Caption}</a>");
                        builder.AppendHtml(sortImg);
                    }
                    else
                    {
                        builder.AppendHtml($"<span class='textHeader'>{field.Caption}</span>");
                    }

                    if (option.FilteringEnabled && field.Filterable && (!field.FieldName.IsEmpty() || !field.FilterField.IsEmpty()))
                    {
                        var fieldType = field.FilterField.IsEmpty()
                                       ? Digger.GetType(firstItemType, field.FieldName)
                                       : Digger.GetType(firstItemType, field.FilterField);
                        if (filter.CanFilterable(fieldType))
                        {
                            var fReplace = field.FilterField.IsEmpty()
                                               ? field.FieldName.Replace(".", "__")
                                               : field.FilterField.Replace(".", "__");
                            builder.AppendHtml(string.Format("<a href='#filter_{0}' data-toggle='modal' class='textHeader filtre-image'><img class='tableImg' src='" +
                                                             @"/assets/zero/mvcgrid/images/filter.png"
                                                             + "' /></a>", fReplace));
                        }
                    }
                    builder.AppendHtml("</div>");
                }
                else if (field.DataColumnType == DataColumnTypes.SelectColumn)
                {
                    builder.AppendHtml("<input type='checkbox' id='chkGridSelectAllRow' />");
                }
                else
                {
                    builder.AppendHtml($"<span class='textHeader'>{field.Caption}</span>");
                }
                builder.AppendHtml("</th>");
            }
            builder.AppendHtml("</tr>");
            return(builder.GetString());
        }