protected virtual string BeginForm <TModel>(IHtmlHelper 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());
        }
        public override async Task ExecuteGridRequest <TModel>(RoboUIGridResult <TModel> roboUIGrid, RoboUIGridRequest request,
                                                               ControllerContext controllerContext)
        {
            var formProvider = roboUIGrid.RoboUIFormProvider ?? RoboUI.DefaultRoboUIFormProvider;

            var response = controllerContext.HttpContext.Response;

            response.ContentType = "application/json";

            var data = await roboUIGrid.FetchAjaxSource(request);

            await WriteJsonData(response, 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, roboUIGrid.Editable);
        }
Beispiel #3
0
 public abstract Task ExecuteGridRequest <TModel>(RoboUIGridResult <TModel> roboUIGrid, RoboUIGridRequest request, ControllerContext controllerContext) where TModel : class;
        public override string Render <TModel>(RoboUIGridResult <TModel> roboUIGrid, IHtmlHelper htmlHelper)
        {
            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);
            }

            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, WebUtility.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.Path.ToString()
                : roboUIGrid.GetRecordsUrl;

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

            if (roboUIGrid.EnableCheckboxes)
            {
                columns.Add(new JObject(
                                new JProperty("template", "<input type='checkbox' class='k-checkbox checkbox' name='selectedIds' />"),
                                new JProperty("width", 30)
                                ));
            }

            foreach (var column in roboUIGrid.Columns)
            {
                var fieldType = GetFieldType(column.CustomType == false ? column.PropertyType : column.TypeFormat);

                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("sortable", column.Sortable),
                    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);
                }

                var fieldModel = new JObject {
                    { "type", fieldType }
                };

                if (fieldType == "date")
                {
                    fieldModel.Add("parse", new JRaw("function(data){ if(!data || data.length == 0) return null; return new Date(data); }"));
                    options.Add("template", new JRaw(string.Format("function(dataItem){{ return dataItem.{0}Formated; }}", column.PropertyName)));

                    if (!string.IsNullOrEmpty(column.DisplayFormat))
                    {
                        options.Add("format", string.Format("{{0:{0}}}", column.DisplayFormat));
                    }
                    else
                    {
                        options.Add("format", string.Format("{{0:{0}}}", column.DisplayFormat));
                    }
                }
                else if (fieldType == "boolean")
                {
                    fieldModel.Add("parse", new JRaw("function(data){ return data; }"));
                }

                columns.Add(options);
                modelFields.Add(column.PropertyName, fieldModel);
            }
            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 }
                            }
                        },
                        {
                            "update", new JObject
                            {
                                { "url", getRecordsUrl },
                                { "dataType", "json" },
                                { "type", "POST" },
                                { "data", new JObject {
                                      { "Save", 1 }
                                  } }
                            }
                        }
                    }
                },
                {
                    "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 }
                            }
                        },
                        {
                            "errors", new JRaw("function(response){if(response.errors){ if(response.redirectUrl){window.location = response.redirectUrl;}else{ return response.errors; }} return null;}")
                        }
                    }
                },
                {
                    "error", new JRaw("function(e){ alert(e.errors); }")
                },
                { "serverPaging", roboUIGrid.EnablePaginate },
                { "pageSize", roboUIGrid.EnablePaginate ? roboUIGrid.DefaultPageSize : int.MaxValue },
                { "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; overflow: visible;" }
                }));

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

                columns.Add(options);
            }

            var dataTableOptions = new JObject
            {
                { "dataSource", dataSourceOptions },
                { "sortable", roboUIGrid.EnableSortable },
                { "resizable", false },
                { "scrollable", true },
                { "columnMenu", false },
                { "columns", columns }
            };

            if (roboUIGrid.EnableFilterable && roboUIGrid.Columns.Any(x => x.Filterable))
            {
                dataTableOptions["filterable"] = new JObject
                {
                    {
                        "operators", new JObject
                        {
                            {
                                "string", new JObject
                                {
                                    { "contains", "Contains" },
                                    { "doesnotcontain", "Does not contain" },
                                    { "eq", "Is equal to" },
                                    { "neq", "Is not equal to" },
                                    { "startswith", "Starts with" },
                                    { "endswith", "Ends with" }
                                }
                            }
                        }
                    }
                };
            }

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

            if (!string.IsNullOrEmpty(roboUIGrid.Height))
            {
                dataTableOptions.Add("height", roboUIGrid.Height);
            }

            // Toolbar
            if (roboUIGrid.Actions.Count > 0)
            {
                var toolbarBuilder = new StringBuilder();
                foreach (var action in roboUIGrid.Actions.OrderBy(x => x.Order))
                {
                    action.HasButtonSize(ButtonSize.Small);
                    toolbarBuilder.Append(RenderAction(htmlHelper, action));
                }
                dataTableOptions.Add("toolbar", toolbarBuilder.ToString().Replace("#", "\\#"));
            }

            var scriptRegister = roboUIGrid.ControllerContext.HttpContext.RequestServices.GetService <IScriptRegister>();
            scriptRegister.AddInlineScript(string.Format("var grid = $('#{0}').kendoGrid({1}).data('kendoGrid');", roboUIGrid.ClientId, dataTableOptions.ToString(Formatting.None)));

            if (roboUIGrid.EnableCheckboxes)
            {
                scriptRegister.AddInlineScript(string.Format("grid.table.on('click', '.k-checkbox' , function(){{ var checked = this.checked, row = $(this).closest('tr'), grid = $('#{0}').data('kendoGrid'), dataItem = grid.dataItem(row); if(checked){{ row.addClass('k-state-selected'); this.value = dataItem.id; }}else{{ row.removeClass('k-state-selected'); }} }});", roboUIGrid.ClientId));
            }

            if (roboUIGrid.ReloadEvents.Count > 0)
            {
                scriptRegister.AddInlineScript(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());
        }
Beispiel #5
0
 public abstract string Render <TModel>(RoboUIGridResult <TModel> roboUIGrid, IHtmlHelper htmlHelper) where TModel : class;