public override void ExecuteGridRequest <TModel>(RoboUIGridResult <TModel> roboUIGrid, RoboUIGridRequest request, ControllerContext controllerContext)
        {
            var formProvider = roboUIGrid.RoboUIFormProvider ?? RoboSettings.DefaultFormProvider;

            var response = controllerContext.HttpContext.Response;

            response.ContentType     = "application/json";
            response.ContentEncoding = Encoding.UTF8;

            if (controllerContext.HttpContext.Request.QueryString["subGrid"] == "1")
            {
                var id   = controllerContext.HttpContext.Request.Form["id"];
                var data = roboUIGrid.SubGrid.GetSubGridData(id);
                WriteJsonData(response, request, data,
                              data.Count(),
                              formProvider,
                              roboUIGrid.SubGrid.Columns,
                              roboUIGrid.SubGrid.GetRowActions(),
                              x => roboUIGrid.SubGrid.GetModelId(x), true, false, null, null);
            }
            else
            {
                var data = roboUIGrid.FetchAjaxSource(request);
                WriteJsonData(response, request, data,
                              data.TotalRecords > 0 ? data.TotalRecords : data.Count,
                              formProvider,
                              roboUIGrid.Columns.Select(x => (RoboUIGridColumn)x).ToList(),
                              roboUIGrid.RowActions.Count > 0 && !roboUIGrid.HideActionsColumn
                        ? roboUIGrid.RowActions.Select(x => (IRoboUIGridRowAction)x).ToList()
                        : new List <IRoboUIGridRowAction>(),
                              roboUIGrid.GetModelId, false, roboUIGrid.TreeGridEnabled, roboUIGrid.TreeGridParentId, roboUIGrid.TreeGridHasChildren);
            }
        }
示例#2
0
        protected virtual string BeginForm <TModel>(HtmlHelper htmlHelper, RoboUIGridResult <TModel> roboGrid) where TModel : class
        {
            if (string.IsNullOrEmpty(roboGrid.FormActionUrl))
            {
                return(string.Empty);
            }

            var form = new FluentTagBuilder("form", TagRenderMode.StartTag)
                       .MergeAttribute("action", roboGrid.FormActionUrl)
                       .MergeAttribute("method", "post");

            if (roboGrid.IsAjaxSupported)
            {
                form = form.MergeAttribute("data-ajax", "true");
            }

            return(form.ToString());
        }
示例#3
0
 public abstract void ExecuteGridRequest <TModel>(RoboUIGridResult <TModel> roboUIGrid, RoboUIGridRequest request, ControllerContext controllerContext) where TModel : class;
示例#4
0
 public abstract string Render <TModel>(RoboUIGridResult <TModel> roboUIGrid, HtmlHelper htmlHelper) where TModel : class;
        public override string Render <TModel>(RoboUIGridResult <TModel> roboUIGrid, HtmlHelper htmlHelper)
        {
            var workContext    = EngineContext.Current.Resolve <IWebWorkContext>();
            var scriptRegister = new ScriptRegister(workContext);
            var sb             = new StringBuilder(2048);

            if (!string.IsNullOrEmpty(roboUIGrid.GridWrapperStartHtml))
            {
                Write(sb, roboUIGrid.GridWrapperStartHtml);
            }

            // Start div container
            Write(sb, string.Format("<div class=\"robo-grid-container\" id=\"{0}_Container\">", roboUIGrid.ClientId));

            var form = BeginForm(htmlHelper, roboUIGrid);

            Write(sb, form);

            if (roboUIGrid.FilterForm != null)
            {
                var filterForm = roboUIGrid.FilterForm.GenerateView();
                Write(sb, filterForm);
            }

            WriteActions(sb, htmlHelper, roboUIGrid.Actions);

            Write(sb, string.Format("<table class=\"{0}\" id=\"{1}\"></table>", roboUIGrid.CssClass, roboUIGrid.ClientId));

            Write(sb, string.Format("<div id=\"{0}_Pager\"></div>", roboUIGrid.ClientId));

            // Hidden values
            foreach (var hiddenValue in roboUIGrid.HiddenValues)
            {
                Write(sb, string.Format("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\"/>", hiddenValue.Key, HttpUtility.HtmlEncode(hiddenValue.Value)));
            }

            if (!string.IsNullOrEmpty(form))
            {
                Write(sb, "</form>");
            }

            if (!roboUIGrid.DisableBlockUI && roboUIGrid.IsAjaxSupported)
            {
                // Block UI
                Write(sb, "<div class=\"blockUI\" style=\"display:none; z-index: 100; border: none; margin: 0; padding: 0; width: 100%; height: 100%; top: 0; left: 0; background-color: #000000; opacity: 0.05; filter: alpha(opacity = 5); cursor: wait; position: absolute;\"></div>");

                // Block Msg
                Write(sb, "<div class=\"blockUIMsg\" style=\"display:none;\">Processing...</div>");

                scriptRegister.IncludeInline("$(document).bind(\"ajaxSend\", function(){ $(\".blockUI, .blockUIMsg\").show(); }).bind(\"ajaxComplete\", function(){ $(\".blockUI, .blockUIMsg\").hide(); });", true);
            }

            // End div container
            Write(sb, "</div>");

            if (!string.IsNullOrEmpty(roboUIGrid.GridWrapperEndHtml))
            {
                Write(sb, roboUIGrid.GridWrapperEndHtml);
            }

            #region jqGrid Options

            var dataTableOptions = new JObject
            {
                { "rowNum", roboUIGrid.EnablePaginate ? roboUIGrid.DefaultPageSize : int.MaxValue },
                { "autowidth", true },
                { "caption", roboUIGrid.Title },
                { "viewrecords", true },
                { "loadonce", false },
                { "userDataOnFooter", true },
                { "hidegrid", roboUIGrid.EnableShowHideGrid },
                { "height", "100%" },
                { "recordpos", roboUIGrid.RecordsInfoPosition },
                { "multiselect", roboUIGrid.EnableCheckboxes },
                { "loadComplete", new JRaw(string.Format("function(data){{ if({1} && data.records === 0) {{ $('#{0}_Pager_center').hide(); }} else {{ $('#{0}_Pager_center').show(); }} var width = $('#{0}_Container').width(); $('#{0}').setGridWidth(width); if(data.callback){{ eval(data.callback); }} }}", roboUIGrid.ClientId, roboUIGrid.HidePagerWhenEmpty ? "true" : "false")) }
            };

            if (!roboUIGrid.DisableBlockUI)
            {
                dataTableOptions.Add("loadui", "disable");
            }

            if (roboUIGrid.EnablePaginate)
            {
                dataTableOptions.Add("pager", string.Format("#{0}_Pager", roboUIGrid.ClientId));
            }

            if (roboUIGrid.RowsList != null)
            {
                dataTableOptions.Add("rowList", roboUIGrid.RowsList);
            }

            if (roboUIGrid.ShowFooterRow)
            {
                dataTableOptions.Add("footerrow", true);
            }

            var colNames = new JArray();
            var colModel = new JArray();

            foreach (var column in roboUIGrid.Columns)
            {
                colNames.Add(column.HeaderText);
                var options = new JObject(
                    new JProperty("name", column.PropertyName),
                    new JProperty("index", column.PropertyName),
                    new JProperty("align", column.Align),
                    new JProperty("sortable", roboUIGrid.EnableSortable && column.Sortable));

                if (column.Width.HasValue)
                {
                    options.Add(new JProperty("width", column.Width));
                    options.Add(new JProperty("fixed", true));
                }

                if (!string.IsNullOrEmpty(column.CssClass))
                {
                    options.Add(new JProperty("classes", column.CssClass));
                }

                if (column.Filterable)
                {
                    options.Add(new JProperty("search", true));
                    var typeCode = Type.GetTypeCode(column.PropertyType);

                    switch (typeCode)
                    {
                    case TypeCode.Boolean:
                        options.Add(new JProperty("stype", "select"));
                        options.Add(new JProperty("editoptions", new JObject(new JProperty("value", ":All;true:Yes;false:No"))));
                        break;

                    case TypeCode.String:
                        options.Add(new JProperty("stype", "text"));
                        options.Add(new JProperty("searchoptions", new JObject(new JProperty("sopt", new JArray("cn")))));
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    options.Add(new JProperty("search", false));
                }

                colModel.Add(options);
            }

            if (roboUIGrid.RowActions.Count > 0)
            {
                if (!roboUIGrid.HideActionsColumn)
                {
                    colNames.Add(roboUIGrid.ActionsHeaderText);
                    var options = new JObject(
                        new JProperty("name", "_RowActions"),
                        new JProperty("align", "center"),
                        new JProperty("index", "_RowActions"),
                        new JProperty("cellattr", new JRaw("function(){ return 'title=\"\"'; }")),
                        new JProperty("search", false),
                        new JProperty("sortable", false));

                    if (roboUIGrid.ActionsColumnWidth.HasValue)
                    {
                        options.Add(new JProperty("width", roboUIGrid.ActionsColumnWidth.Value));
                        options.Add(new JProperty("fixed", true));
                    }

                    colModel.Add(options);
                }
            }

            dataTableOptions.Add("colNames", colNames);
            dataTableOptions.Add("colModel", colModel);

            if (roboUIGrid.CustomVariables.Count > 0)
            {
                var postData = new JObject();

                foreach (var customrVar in roboUIGrid.CustomVariables)
                {
                    postData.Add(customrVar.Key, new JRaw(customrVar.Value));
                }

                dataTableOptions.Add("postData", postData);
            }

            dataTableOptions.Add("datatype", "json");
            dataTableOptions.Add("jsonReader", new JObject(new JProperty("id", "_id"), new JProperty("subgrid", new JObject(new JProperty("repeatitems", false)))));
            dataTableOptions.Add("mtype", "POST");
            dataTableOptions.Add("url", string.IsNullOrEmpty(roboUIGrid.GetRecordsUrl)
                ? roboUIGrid.ControllerContext.HttpContext.Request.RawUrl
                : roboUIGrid.GetRecordsUrl);

            // Sub Grid
            if (roboUIGrid.SubGrid != null)
            {
                var subGridNames    = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.HeaderText));
                var subGridWidths   = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.Width.HasValue ? x.Width.Value : 100));
                var subGridAligns   = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.Align));
                var subGridMappings = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.PropertyName));

                var subRowActions = roboUIGrid.SubGrid.GetRowActions();
                if (subRowActions.Any())
                {
                    subGridNames.Add(roboUIGrid.SubGrid.ActionsColumnText);
                    subGridWidths.Add(roboUIGrid.SubGrid.ActionsColumnWidth);
                    subGridAligns.Add("center");
                    subGridMappings.Add("_RowActions");
                }

                var subGridModel = new JObject
                {
                    { "name", subGridNames },
                    { "width", subGridWidths },
                    { "align", subGridAligns },
                    { "mapping", subGridMappings }
                };

                var queryString  = string.Join(string.Empty, roboUIGrid.ControllerContext.HttpContext.Request.RawUrl.Split('?').Skip(1));
                var queryStrings = HttpUtility.ParseQueryString(queryString);
                queryStrings["subGrid"] = "1";

                dataTableOptions.Add("subGrid", true);
                dataTableOptions.Add("subGridUrl", roboUIGrid.ControllerContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Path) + "?" + string.Join("&", queryStrings.AllKeys.Select(x => x + "=" + HttpUtility.UrlEncode(queryStrings[x]))));
                dataTableOptions.Add("subGridModel", new JArray(subGridModel));

                if (roboUIGrid.SubGrid.Width.HasValue)
                {
                    dataTableOptions.Add("subGridWidth", roboUIGrid.SubGrid.Width.Value);
                }

                if (roboUIGrid.SubGrid.AjaxOptions != null)
                {
                    dataTableOptions.Add("ajaxSubgridOptions", roboUIGrid.SubGrid.AjaxOptions);
                }
            }

            // Tree grid
            if (roboUIGrid.TreeGridEnabled)
            {
                var treeReader = new JObject
                {
                    { "level_field", "_level" },
                    { "parent_id_field", "_parentId" },
                    { "leaf_field", "_isLeaf" },
                    { "expanded_field", "_isExpanded" }
                };

                dataTableOptions.Add("treeGrid", true);
                dataTableOptions.Add("treeGridModel", "adjacency");
                dataTableOptions.Add("treeReader", treeReader);
            }

            scriptRegister.IncludeInline(string.Format("$('#{0}').jqGrid({1});", roboUIGrid.ClientId, dataTableOptions.ToString(Formatting.None)));

            if (roboUIGrid.EnableSearch && roboUIGrid.Columns.Any(x => x.Filterable))
            {
                scriptRegister.IncludeInline(string.Format("$('#{0}').jqGrid('filterToolbar', {{ stringResult: true }});", roboUIGrid.ClientId));
            }

            if (roboUIGrid.ReloadEvents.Count > 0)
            {
                scriptRegister.IncludeInline(string.Format("$('body').bind('SystemMessageEvent', function(event){{ var events = [{1}]; if(events.indexOf(event.SystemMessage) > -1){{ $('#{0}').jqGrid().trigger('reloadGrid'); }} }});", roboUIGrid.ClientId, string.Join(", ", roboUIGrid.ReloadEvents.Select(x => "'" + x + "'"))));
            }

            // Resize window event
            scriptRegister.IncludeInline(string.Format("$(window).resize(function(){{ var width = $('#{0}_Container').width(); $('#{0}').setGridWidth(width); }});", roboUIGrid.ClientId));

            #endregion jqGrid Options

            return(sb.ToString());
        }
示例#6
0
        public override string Render <TModel>(RoboUIGridResult <TModel> roboUIGrid, HtmlHelper htmlHelper)
        {
            var workContext    = EngineContext.Current.Resolve <IWebWorkContext>();
            var scriptRegister = new ScriptRegister(workContext);
            var sb             = new StringBuilder(2048);

            if (!string.IsNullOrEmpty(roboUIGrid.GridWrapperStartHtml))
            {
                Write(sb, roboUIGrid.GridWrapperStartHtml);
            }

            // Start div container
            Write(sb, string.Format("<div class=\"robo-grid-container\" id=\"{0}_Container\">", roboUIGrid.ClientId));

            var form = BeginForm(htmlHelper, roboUIGrid);

            Write(sb, form);

            if (roboUIGrid.FilterForm != null)
            {
                var filterForm = roboUIGrid.FilterForm.GenerateView();
                Write(sb, filterForm);
            }

            WriteActions(sb, htmlHelper, roboUIGrid.Actions);

            Write(sb, string.Format("<div class=\"{0}\" id=\"{1}\"></div>", roboUIGrid.CssClass, roboUIGrid.ClientId));

            // Hidden values
            foreach (var hiddenValue in roboUIGrid.HiddenValues)
            {
                Write(sb, string.Format("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\"/>", hiddenValue.Key, HttpUtility.HtmlEncode(hiddenValue.Value)));
            }

            if (!string.IsNullOrEmpty(form))
            {
                Write(sb, "</form>");
            }

            // End div container
            Write(sb, "</div>");

            if (!string.IsNullOrEmpty(roboUIGrid.GridWrapperEndHtml))
            {
                Write(sb, roboUIGrid.GridWrapperEndHtml);
            }

            #region Kendo UI Options

            var getRecordsUrl = string.IsNullOrEmpty(roboUIGrid.GetRecordsUrl)
                                ? roboUIGrid.ControllerContext.HttpContext.Request.RawUrl
                                : roboUIGrid.GetRecordsUrl;

            var columns     = new JArray();
            var modelFields = new JObject();

            foreach (var column in roboUIGrid.Columns)
            {
                var options = new JObject(
                    new JProperty("field", column.PropertyName),
                    new JProperty("filterable", column.Filterable),
                    new JProperty("encoded", false),
                    new JProperty("title", column.HeaderText),
                    new JProperty("hidden", column.Hidden),
                    new JProperty("attributes", new JObject
                {
                    { "style", "text-align:" + column.Align + ";" }
                }),
                    new JProperty("headerAttributes", new JObject
                {
                    { "style", "text-align:" + column.Align + ";" }
                }));

                if (column.Width.HasValue)
                {
                    options.Add("width", column.Width.Value);
                }

                columns.Add(options);

                var fieldType = GetFieldType(column.PropertyType);
                modelFields.Add(column.PropertyName, new JObject {
                    { "type", fieldType }
                });
            }
            var postData = new JObject();

            if (roboUIGrid.CustomVariables.Count > 0)
            {
                foreach (var customrVar in roboUIGrid.CustomVariables)
                {
                    postData.Add(customrVar.Key, new JRaw(customrVar.Value));
                }
            }

            var dataSourceOptions = new JObject
            {
                {
                    "type",
                    new JRaw(
                        "(function(){if(kendo.data.transports['aspnetmvc-ajax']){return 'aspnetmvc-ajax';} else{throw new Error('The kendo.aspnetmvc.min.js script is not included.');}})()")
                },
                {
                    "transport", new JObject
                    {
                        {
                            "read", new JObject
                            {
                                { "url", getRecordsUrl },
                                { "dataType", "json" },
                                { "type", "POST" },
                                { "data", postData }
                            }
                        }
                    }
                },
                { "requestEnd", new JRaw("function(e){ if(e.response.callback){ eval(e.response.callback); } }") },
                {
                    "schema", new JObject
                    {
                        { "data", "results" },
                        { "total", "__count" },
                        {
                            "model", new JObject
                            {
                                { "id", "_id" },
                                { "fields", modelFields }
                            }
                        },
                    }
                },
                { "pageSize", roboUIGrid.EnablePaginate ? roboUIGrid.DefaultPageSize : int.MaxValue },
                { "serverPaging", roboUIGrid.EnablePaginate },
                { "serverFiltering", true },
                { "serverSorting", true }
            };

            if (roboUIGrid.RowActions.Count > 0 && !roboUIGrid.HideActionsColumn)
            {
                var options = new JObject(
                    new JProperty("field", "_RowActions"),
                    new JProperty("filterable", false),
                    new JProperty("sortable", false),
                    new JProperty("groupable", false),
                    new JProperty("encoded", false),
                    new JProperty("menu", false),
                    new JProperty("title", roboUIGrid.ActionsHeaderText),
                    new JProperty("headerAttributes", new JObject
                {
                    { "style", "text-align: center;" }
                }),
                    new JProperty("attributes", new JObject
                {
                    { "style", "text-align: center;" }
                }));

                if (roboUIGrid.ActionsColumnWidth.HasValue)
                {
                    options.Add("width", roboUIGrid.ActionsColumnWidth.Value);
                }

                columns.Add(options);
            }

            var dataTableOptions = new JObject
            {
                { "dataSource", dataSourceOptions },
                { "filterable", roboUIGrid.EnableFilterable&& roboUIGrid.Columns.Any(x => x.Filterable) },
                { "sortable", roboUIGrid.EnableSortable },
                { "resizable", true },
                { "columnMenu", true },
                { "columns", columns }
            };

            if (roboUIGrid.EnablePaginate)
            {
                dataTableOptions.Add("pageable", new JObject
                {
                    { "refresh", true },
                    { "pageSize", roboUIGrid.DefaultPageSize },
                    { "buttonCount", 10 }
                });
            }
            else
            {
                dataTableOptions.Add("pageable", false);
            }

            //if (roboUIGrid.RowsList != null)
            //{
            //    dataTableOptions.Add("rowList", roboUIGrid.RowsList);
            //}

            //if (roboUIGrid.ShowFooterRow)
            //{
            //    dataTableOptions.Add("footerrow", true);
            //}

            //if (roboUIGrid.CustomVariables.Count > 0)
            //{
            //    var postData = new JObject();

            //    foreach (var customrVar in roboUIGrid.CustomVariables)
            //    {
            //        postData.Add(customrVar.Key, new JRaw(customrVar.Value));
            //    }

            //    dataTableOptions.Add("postData", postData);
            //}

            //dataTableOptions.Add("jsonReader", new JObject(new JProperty("id", "_id"), new JProperty("subgrid", new JObject(new JProperty("repeatitems", false)))));

            // Sub Grid
            if (roboUIGrid.SubGrid != null)
            {
                var subGridNames    = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.HeaderText));
                var subGridWidths   = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.Width.HasValue ? x.Width.Value : 100));
                var subGridAligns   = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.Align));
                var subGridMappings = new JArray(roboUIGrid.SubGrid.Columns.Select(x => x.PropertyName));

                var subRowActions = roboUIGrid.SubGrid.GetRowActions();
                if (subRowActions.Any())
                {
                    subGridNames.Add(roboUIGrid.SubGrid.ActionsColumnText);
                    subGridWidths.Add(roboUIGrid.SubGrid.ActionsColumnWidth);
                    subGridAligns.Add("center");
                    subGridMappings.Add("_RowActions");
                }

                var subGridModel = new JObject
                {
                    { "name", subGridNames },
                    { "width", subGridWidths },
                    { "align", subGridAligns },
                    { "mapping", subGridMappings }
                };

                var queryString  = string.Join(string.Empty, roboUIGrid.ControllerContext.HttpContext.Request.RawUrl.Split('?').Skip(1));
                var queryStrings = HttpUtility.ParseQueryString(queryString);
                queryStrings["subGrid"] = "1";

                dataTableOptions.Add("subGrid", true);
                dataTableOptions.Add("subGridUrl", roboUIGrid.ControllerContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Path) + "?" + string.Join("&", queryStrings.AllKeys.Select(x => x + "=" + HttpUtility.UrlEncode(queryStrings[x]))));
                dataTableOptions.Add("subGridModel", new JArray(subGridModel));

                if (roboUIGrid.SubGrid.Width.HasValue)
                {
                    dataTableOptions.Add("subGridWidth", roboUIGrid.SubGrid.Width.Value);
                }

                if (roboUIGrid.SubGrid.AjaxOptions != null)
                {
                    dataTableOptions.Add("ajaxSubgridOptions", roboUIGrid.SubGrid.AjaxOptions);
                }
            }

            // Tree grid
            if (roboUIGrid.TreeGridEnabled)
            {
                var treeReader = new JObject
                {
                    { "level_field", "_level" },
                    { "parent_id_field", "_parentId" },
                    { "leaf_field", "_isLeaf" },
                    { "expanded_field", "_isExpanded" }
                };

                dataTableOptions.Add("treeGrid", true);
                dataTableOptions.Add("treeGridModel", "adjacency");
                dataTableOptions.Add("treeReader", treeReader);
            }

            scriptRegister.IncludeInline(string.Format("$('#{0}').kendoGrid({1});", roboUIGrid.ClientId, dataTableOptions.ToString(Formatting.None)));

            if (roboUIGrid.ReloadEvents.Count > 0)
            {
                scriptRegister.IncludeInline(string.Format("$('body').bind('SystemMessageEvent', function(event){{ var events = [{1}]; if(events.indexOf(event.SystemMessage) > -1){{ $('#{0}').data('kendoGrid').dataSource.read(); }} }});", roboUIGrid.ClientId, string.Join(", ", roboUIGrid.ReloadEvents.Select(x => "'" + x + "'"))));
            }

            #endregion Kendo UI Options

            return(sb.ToString());
        }