示例#1
0
        public override RoboUIGridRequest CreateGridRequest(ControllerContext controllerContext)
        {
            var result  = new RoboUIGridRequest();
            var request = controllerContext.HttpContext.Request;

            var page = request.Form["page"];

            if (page != null)
            {
                result.PageIndex = Convert.ToInt32(page);
            }

            var pageSize = request.Form["pageSize"];

            if (pageSize != null)
            {
                result.PageSize = Convert.ToInt32(pageSize);
            }

            var bindingContext = new ModelBindingContext
            {
                ValueProvider = controllerContext.Controller.ValueProvider
            };

            string value;

            if (TryGetValue(bindingContext, "sort", out value))
            {
                result.Sorts = GridDescriptorSerializer.Deserialize <SortDescriptor>(value);
            }

            if (TryGetValue(bindingContext, "filter", out value))
            {
                if (!string.IsNullOrEmpty(value))
                {
                    // Process [today], [beginWeek], [endWeek], [beginMonth], [endMonth], [beginPrevMonth] tokens
                    var dtNow = DateTime.UtcNow.Date;
                    int startIndex;
                    var endIndex = 0;
                    while ((startIndex = value.IndexOf("[today", endIndex, StringComparison.Ordinal)) != -1)
                    {
                        endIndex = value.IndexOf("]", startIndex, StringComparison.Ordinal);
                        var days = value.Substring(startIndex + 6, endIndex - startIndex - 6);
                        value = value.Replace("[today" + days + "]", dtNow.AddDays(Convert.ToInt32(days)).ToString("O"));
                    }

                    value = value.Replace("[beginWeek]", dtNow.StartOfWeek(DayOfWeek.Monday).ToString("O"));
                    value = value.Replace("[endWeek]", dtNow.EndOfWeek(DayOfWeek.Sunday).AddDays(1).ToString("O"));
                    value = value.Replace("[beginMonth]", new DateTime(dtNow.Year, dtNow.Month, 1, 0, 0, 0, DateTimeKind.Utc).ToString("O"));
                    value = value.Replace("[endMonth]", new DateTime(dtNow.Year, dtNow.Month + 1, 1, 0, 0, 0, DateTimeKind.Utc).ToString("O"));
                    value = value.Replace("[beginPrevMonth]", new DateTime(dtNow.Year, dtNow.Month - 1, 1, 0, 0, 0, DateTimeKind.Utc).ToString("O"));
                    value = value.Replace("[beginYear]", new DateTime(dtNow.Year, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToString("O"));
                    value = value.Replace("[endYear]", new DateTime(dtNow.Year + 1, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToString("O"));

                    result.Filters = FilterDescriptorFactory.Create(value);
                }
            }

            return(result);
        }
示例#2
0
 public abstract void ExecuteGridRequest <TModel>(RoboUIGridResult <TModel> roboUIGrid, RoboUIGridRequest request, ControllerContext controllerContext) where TModel : class;
        private static void WriteJsonData <TModelRecord>(
            HttpResponseBase response,
            RoboUIGridRequest request,
            RoboUIGridAjaxData <TModelRecord> data,
            int totalRecords,
            IRoboUIFormProvider formProvider,
            IEnumerable <RoboUIGridColumn> columns,
            IEnumerable <IRoboUIGridRowAction> rowActions,
            Func <TModelRecord, object> getModelId,
            bool isSubGrid,
            bool isTreeGrid,
            Func <TModelRecord, dynamic> getParentId,
            Func <TModelRecord, bool> hasChildren)
        {
            using (var writer = new JsonTextWriter(response.Output)
            {
                Formatting = Formatting.None
            })
            {
                writer.WriteStartObject();

                writer.WritePropertyName("page");
                writer.WriteValue(request.PageIndex);

                writer.WritePropertyName("records");
                writer.WriteValue(totalRecords);

                writer.WritePropertyName("total");
                writer.WriteValue((int)Math.Ceiling((totalRecords * 1d) / request.PageSize));

                if (data.Callbacks.Count > 0)
                {
                    writer.WritePropertyName("callback");
                    writer.WriteValue(string.Join("", data.Callbacks));
                }

                writer.WritePropertyName("rows");

                writer.WriteStartArray();

                var needWriteValueDelimiter = false;
                foreach (TModelRecord item in data)
                {
                    var jsonObject = new JObject {
                        { "_id", Convert.ToString(getModelId(item)) }
                    };

                    foreach (var column in columns)
                    {
                        jsonObject.Add(column.PropertyName, Convert.ToString(column.BuildHtml(item)));
                    }

                    if (isTreeGrid)
                    {
                        jsonObject.Add("_level", request.NodeLevel + 1);
                        jsonObject.Add("_parentId", getParentId(item));
                        jsonObject.Add("_isLeaf", !hasChildren(item));
                        jsonObject.Add("_isExpanded", false);
                    }

                    if (rowActions.Any())
                    {
                        var sb = new StringBuilder();
                        sb.Append("<table style=\"margin-left: auto; margin-right: auto; border: none; padding: 0;\">");
                        sb.Append("<tr>");

                        foreach (var action in rowActions)
                        {
                            if (!action.IsVisible(item))
                            {
                                continue;
                            }

                            var enabled    = action.IsEnabled(item);
                            var attributes = new RouteValueDictionary(action.GetAttributes(item));

                            sb.Append("<td style=\"border: none; background-color: transparent; padding: 0 2px;\">");
                            if (action.IsSubmitButton)
                            {
                                var value = action.GetValue(item);

                                var cssClass =
                                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                                     formProvider.GetButtonStyleCssClass(action.ButtonStyle) + " " + action.CssClass).Trim();

                                if (!string.IsNullOrEmpty(cssClass))
                                {
                                    attributes.Add("class", cssClass);
                                }

                                if (!enabled)
                                {
                                    attributes.Add("disabled", "disabled");
                                }

                                if (!string.IsNullOrEmpty(action.ClientClickCode))
                                {
                                    attributes.Add("onclick", action.ClientClickCode);
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(action.ConfirmMessage))
                                    {
                                        attributes.Add("onclick", string.Format("return confirm('{0}');", action.ConfirmMessage));
                                    }
                                }

                                attributes.Add("id", "btn" + Guid.NewGuid().ToString("N").ToLowerInvariant());
                                attributes.Add("style", "white-space: nowrap;");

                                var tagBuilder = new TagBuilder("button");
                                tagBuilder.MergeAttribute("type", "submit");
                                tagBuilder.MergeAttribute("name", action.Name);
                                tagBuilder.InnerHtml = action.Text;
                                tagBuilder.MergeAttribute("value", Convert.ToString(value));
                                tagBuilder.MergeAttributes(attributes, true);
                                sb.Append(tagBuilder.ToString(TagRenderMode.Normal));
                            }
                            else
                            {
                                var href = action.GetUrl(item);

                                var cssClass =
                                    (formProvider.GetButtonSizeCssClass(action.ButtonSize) + " " +
                                     formProvider.GetButtonStyleCssClass(action.ButtonStyle) + " " + action.CssClass).Trim();

                                if (!string.IsNullOrEmpty(cssClass))
                                {
                                    if (enabled)
                                    {
                                        attributes.Add("class", cssClass);
                                    }
                                    else
                                    {
                                        attributes.Add("class", cssClass + " disabled");
                                    }
                                }
                                else
                                {
                                    if (!enabled)
                                    {
                                        attributes.Add("class", "disabled");
                                    }
                                }

                                attributes.Add("style", "white-space: nowrap;");

                                if (action.IsShowModalDialog && enabled)
                                {
                                    attributes.Add("data-toggle", "fancybox");
                                    attributes.Add("data-fancybox-type", "iframe");
                                    attributes.Add("data-fancybox-width", action.ModalDialogWidth);
                                }

                                var tagBuilder = new TagBuilder("a");
                                if (enabled)
                                {
                                    tagBuilder.MergeAttribute("href", href ?? "javascript:void(0)");
                                }
                                else
                                {
                                    tagBuilder.MergeAttribute("href", "javascript:void(0)");
                                }
                                tagBuilder.InnerHtml = action.Text;
                                tagBuilder.MergeAttributes(attributes, true);
                                sb.Append(tagBuilder.ToString(TagRenderMode.Normal));
                            }
                            sb.Append("</td>");
                        }

                        sb.Append("</tr>");
                        sb.Append("</table>");

                        jsonObject.Add("_RowActions", sb.ToString());
                    }
                    else
                    {
                        jsonObject.Add("_RowActions", null);
                    }

                    if (needWriteValueDelimiter)
                    {
                        writer.WriteRaw(",");
                    }

                    writer.WriteRaw(jsonObject.ToString());
                    needWriteValueDelimiter = true;
                }

                if (isSubGrid && data.UserData.ContainsKey("_RowActions"))
                {
                    if (needWriteValueDelimiter)
                    {
                        writer.WriteRaw(",");
                    }

                    writer.WriteStartObject();
                    writer.WritePropertyName("_RowActions");
                    writer.WriteValue(data.UserData["_RowActions"]);
                    writer.WriteEndObject();

                    data.UserData.Remove("_RowActions");
                }

                writer.WriteEndArray();

                if (data.UserData.Count > 0)
                {
                    writer.WritePropertyName("userdata");

                    writer.WriteStartObject();

                    foreach (var item in data.UserData)
                    {
                        writer.WritePropertyName(item.Key);
                        writer.WriteValue(item.Value);
                    }

                    writer.WriteEndObject();
                }

                writer.WriteEndObject();
                writer.Flush();
            }
        }
        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);
            }
        }
        public override RoboUIGridRequest CreateGridRequest(ControllerContext controllerContext)
        {
            var result  = new RoboUIGridRequest();
            var request = controllerContext.HttpContext.Request;

            var rows = request.Form["rows"];

            if (rows != null)
            {
                result.PageSize = Convert.ToInt32(rows);
            }

            var page = request.Form["page"];

            if (page != null)
            {
                result.PageIndex = Convert.ToInt32(page);
            }

            if (result.PageIndex < 1)
            {
                result.PageIndex = 1;
            }

            result.NodeId = request.Form["nodeid"];
            if (request.Form.AllKeys.Contains("n_level"))
            {
                var level = request.Form["n_level"];
                if (string.IsNullOrEmpty(level))
                {
                    result.NodeLevel = -1;
                }
                else
                {
                    result.NodeLevel = Convert.ToInt32(request.Form["n_level"]);
                }
            }
            else
            {
                result.NodeLevel = -1;
            }

            if (!string.IsNullOrEmpty(request.Form["sidx"]))
            {
                result.Sorts = new List <SortDescriptor>
                {
                    new SortDescriptor
                    {
                        Member        = request.Form["sidx"],
                        SortDirection = request.Form["sord"] == "asc" ? ListSortDirection.Ascending : ListSortDirection.Descending
                    }
                };
            }

            var isSearch = request.Form["_search"];

            if (isSearch == "true")
            {
                result.Filters = new List <IFilterDescriptor>();

                var filters = JObject.Parse(request.Form["filters"]);
                var rules   = (JArray)filters["rules"];
                foreach (var rule in rules)
                {
                    switch (rule["op"].Value <string>())
                    {
                    case "eq":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsEqualTo,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "ne":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsNotEqualTo,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "lt":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsLessThan,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "le":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsLessThanOrEqualTo,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "gt":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsGreaterThan,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "ge":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsGreaterThanOrEqualTo,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "bw":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.StartsWith,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "bn":

                        break;

                    case "in":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.IsContainedIn,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "ni":

                        break;

                    case "ew":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.EndsWith,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "en":

                        break;

                    case "cn":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.Contains,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    case "nc":
                        result.Filters.Add(new FilterDescriptor
                        {
                            Member   = rule["field"].Value <string>(),
                            Operator = FilterOperator.DoesNotContain,
                            Value    = rule["data"].Value <string>()
                        });
                        break;

                    default: throw new ArgumentOutOfRangeException();
                    }
                }
            }

            return(result);
        }