Пример #1
0
        public static object GetItemOrValueFromProperty(object item, string property)
        {
            if (item == null)
            {
                return(null);
            }

            if (Convert.GetTypeCode(item) != TypeCode.Object || string.IsNullOrEmpty(property))
            {
                return(item);
            }

            return(PropertyAccess.GetValue(item, property));
        }
Пример #2
0
        protected async System.Threading.Tasks.Task SelectAll()
        {
            if (Disabled)
            {
                return;
            }

            if (selectedItems.Count != View.Cast <object>().Count())
            {
                selectedItems.Clear();
                selectedItems = View.Cast <object>().ToList();
            }
            else
            {
                selectedItems.Clear();
            }

            if (!string.IsNullOrEmpty(ValueProperty))
            {
                System.Reflection.PropertyInfo pi = PropertyAccess.GetElementType(Data.GetType()).GetProperty(ValueProperty);
                Value = selectedItems.Select(i => PropertyAccess.GetItemOrValueFromProperty(i, ValueProperty)).AsQueryable().Cast(pi.PropertyType);
            }
            else
            {
                var type = typeof(T).IsGenericType ? typeof(T).GetGenericArguments()[0] : typeof(T);
                Value = selectedItems.AsQueryable().Cast(type);
            }

            await ValueChanged.InvokeAsync((T)Value);

            if (FieldIdentifier.FieldName != null)
            {
                EditContext?.NotifyFieldChanged(FieldIdentifier);
            }
            await Change.InvokeAsync(Value);

            StateHasChanged();
        }
Пример #3
0
        protected virtual void SelectItemFromValue(object value)
        {
            if (value != null && View != null)
            {
                if (!Multiple)
                {
                    if (!string.IsNullOrEmpty(ValueProperty))
                    {
                        if (typeof(EnumerableQuery).IsAssignableFrom(View.GetType()))
                        {
                            SelectedItem = View.OfType <object>().Where(i => object.Equals(PropertyAccess.GetValue(i, ValueProperty), value)).FirstOrDefault();
                        }
                        else
                        {
                            SelectedItem = View.AsQueryable().Where($@"{ValueProperty} == @0", value).FirstOrDefault();
                        }
                    }
                    else
                    {
                        selectedItem = Value;
                    }

                    SetSelectedIndexFromSelectedItem();

                    SelectedItemChanged?.Invoke(selectedItem);
                }
                else
                {
                    var values = value as dynamic;
                    if (values != null)
                    {
                        if (!string.IsNullOrEmpty(ValueProperty))
                        {
                            foreach (object v in values)
                            {
                                dynamic item;

                                if (typeof(EnumerableQuery).IsAssignableFrom(View.GetType()))
                                {
                                    item = View.OfType <object>().Where(i => object.Equals(PropertyAccess.GetValue(i, ValueProperty), v)).FirstOrDefault();
                                }
                                else
                                {
                                    item = View.AsQueryable().Where($@"{ValueProperty} == @0", v).FirstOrDefault();
                                }

                                if (!object.Equals(item, null) && selectedItems.IndexOf(item) == -1)
                                {
                                    selectedItems.Add(item);
                                }
                            }
                        }
                        else
                        {
                            selectedItems = ((IEnumerable)values).Cast <object>().ToList();
                        }
                    }
                }
            }
            else
            {
                selectedItem = null;
            }
        }
Пример #4
0
        protected async System.Threading.Tasks.Task SelectItem(object item, bool raiseChange = true)
        {
            if (!Multiple)
            {
                if (object.Equals(item, selectedItem))
                {
                    return;
                }

                selectedItem = item;
                if (!string.IsNullOrEmpty(ValueProperty))
                {
                    Value = PropertyAccess.GetItemOrValueFromProperty(item, ValueProperty);
                }
                else
                {
                    Value = item;
                }

                SetSelectedIndexFromSelectedItem();

                SelectedItemChanged?.Invoke(selectedItem);
            }
            else
            {
                if (selectedItems.IndexOf(item) == -1)
                {
                    selectedItems.Add(item);
                }
                else
                {
                    selectedItems.Remove(item);
                }

                if (!string.IsNullOrEmpty(ValueProperty))
                {
                    System.Reflection.PropertyInfo pi = PropertyAccess.GetElementType(Data.GetType()).GetProperty(ValueProperty);
                    Value = selectedItems.Select(i => PropertyAccess.GetItemOrValueFromProperty(i, ValueProperty)).AsQueryable().Cast(pi.PropertyType);
                }
                else
                {
                    var firstElement = Data.Cast <object>().FirstOrDefault();
                    var elementType  = firstElement != null?firstElement.GetType() : null;

                    if (elementType != null)
                    {
                        Value = selectedItems.AsQueryable().Cast(elementType);
                    }
                    else
                    {
                        Value = selectedItems;
                    }
                }
            }
            if (raiseChange)
            {
                await ValueChanged.InvokeAsync(object.Equals(Value, null)?default(T) : (T)Value);

                if (FieldIdentifier.FieldName != null)
                {
                    EditContext?.NotifyFieldChanged(FieldIdentifier);
                }
                await Change.InvokeAsync(Value);
            }
            StateHasChanged();
        }
Пример #5
0
        private static string GetColumnFilter <T>(RadzenGridColumn <T> column, bool second = false)
        {
            var property = PropertyAccess.GetProperty(column.GetFilterProperty());

            if (property.IndexOf(".") != -1)
            {
                property = $"({property})";
            }

            if (column.Type == "string" && string.IsNullOrEmpty(column.Format))
            {
                property = $@"({property} == null ? """" : {property})";
            }

            var columnFilterOperator = !second ? column.FilterOperator : column.SecondFilterOperator;

            var linqOperator = FilterOperators[columnFilterOperator];

            if (linqOperator == null)
            {
                linqOperator = "==";
            }

            var value = !second ? (string)Convert.ChangeType(column.FilterValue, typeof(string)) :
                        (string)Convert.ChangeType(column.SecondFilterValue, typeof(string));

            var columnType   = column.Type;
            var columnFormat = column.Format;

            if (columnType == "string")
            {
                string filterCaseSensitivityOperator = column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ? ".ToLower()" : "";

                if (columnFormat == "date-time" || columnFormat == "date")
                {
                    var dateTimeValue = DateTime.Parse(value, null, System.Globalization.DateTimeStyles.RoundtripKind);
                    var finalDate     = dateTimeValue.TimeOfDay == TimeSpan.Zero ? dateTimeValue.Date : dateTimeValue;
                    var dateFormat    = dateTimeValue.TimeOfDay == TimeSpan.Zero ? "yyyy-MM-dd" : "yyyy-MM-ddTHH:mm:ssZ";

                    return($@"{property} {linqOperator} DateTime(""{finalDate.ToString(dateFormat)}"")");
                }
                else if (columnFormat == "time")
                {
                    return($"{property} {linqOperator} duration'{value}'");
                }
                else if (columnFormat == "uuid")
                {
                    return($@"{property} {linqOperator} Guid(""{value}"")");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == "contains")
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.Contains(""{value}""{filterCaseSensitivityOperator})");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == "startswith")
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.StartsWith(""{value}""{filterCaseSensitivityOperator})");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == "endswith")
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.EndsWith(""{value}""{filterCaseSensitivityOperator})");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == "eq")
                {
                    return($@"{property} == null ? """" : {property}{filterCaseSensitivityOperator} == {value}{filterCaseSensitivityOperator}");
                }
            }
            else if (columnType == "number" || columnType == "integer")
            {
                return($"{property} {linqOperator} {value}");
            }
            else if (columnType == "boolean")
            {
                return($"{property} == {value}");
            }

            return("");
        }
Пример #6
0
        public static IQueryable <T> Where <T>(this IQueryable <T> source, IEnumerable <RadzenGridColumn <T> > columns)
        {
            Func <RadzenGridColumn <T>, bool> canFilter = (c) => c.Filterable && !string.IsNullOrEmpty(c.Type) &&
                                                          !(c.FilterValue == null || c.FilterValue as string == string.Empty) && c.GetFilterProperty() != null;

            if (columns.Where(canFilter).Any())
            {
                var index     = 0;
                var whereList = new Dictionary <string, IEnumerable <object> >();
                foreach (var column in columns.Where(canFilter))
                {
                    var property = PropertyAccess.GetProperty(column.GetFilterProperty());

                    if (property.IndexOf(".") != -1)
                    {
                        property = $"({property})";
                    }

                    if (column.Type == "string" && string.IsNullOrEmpty(column.Format))
                    {
                        property = $@"({property} == null ? """" : {property})";
                    }

                    string filterCaseSensitivityOperator = column.Type == "string" && string.IsNullOrEmpty(column.Format) &&
                                                           column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ? ".ToLower()" : "";

                    var comparison = FilterOperators[column.FilterOperator];

                    var booleanOperator = column.LogicalFilterOperator == LogicalFilterOperator.And ? "and" : "or";

                    if (column.SecondFilterValue == null)
                    {
                        if (comparison == "StartsWith" || comparison == "EndsWith" || comparison == "Contains")
                        {
                            whereList.Add($@"{property}{filterCaseSensitivityOperator}.{comparison}(@{index}{filterCaseSensitivityOperator})", new object[] { column.FilterValue });
                            index++;
                        }
                        else
                        {
                            whereList.Add($@"{property}{filterCaseSensitivityOperator} {comparison} @{index}{filterCaseSensitivityOperator}", new object[] { column.FilterValue });
                            index++;
                        }
                    }
                    else
                    {
                        var firstFilter = comparison == "StartsWith" || comparison == "EndsWith" || comparison == "Contains" ?
                                          $@"{property}{filterCaseSensitivityOperator}.{comparison}(@{index}{filterCaseSensitivityOperator})" :
                                          $@"{property}{filterCaseSensitivityOperator} {comparison} @{index}{filterCaseSensitivityOperator}";
                        index++;

                        var secondComparison = FilterOperators[column.SecondFilterOperator];
                        var secondFilter     = secondComparison == "StartsWith" || secondComparison == "EndsWith" || secondComparison == "Contains" ?
                                               $@"{property}{filterCaseSensitivityOperator}.{secondComparison}(@{index}{filterCaseSensitivityOperator})" :
                                               $@"{property}{filterCaseSensitivityOperator} {secondComparison} @{index}{filterCaseSensitivityOperator}";
                        index++;

                        whereList.Add($@"({firstFilter} {booleanOperator} {secondFilter})", new object[] { column.FilterValue, column.SecondFilterValue });
                    }
                }

                return(source.Where(string.Join(" and ", whereList.Keys), whereList.Values.SelectMany(i => i.ToArray()).ToArray()));
            }

            return(source);
        }
        private static string GetColumnODataFilter <T>(RadzenDataGridColumn <T> column, bool second = false)
        {
            var property = column.GetFilterProperty().Replace('.', '/');

            var columnFilterOperator = !second?column.GetFilterOperator() : column.GetSecondFilterOperator();

            var value = !second ? (string)Convert.ChangeType(column.GetFilterValue(), typeof(string)) :
                        (string)Convert.ChangeType(column.GetSecondFilterValue(), typeof(string));

            if (column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive && column.FilterPropertyType == typeof(string))
            {
                property = $"tolower({property})";
            }

            if (column.FilterPropertyType == typeof(string))
            {
                if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.Contains)
                {
                    return(column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ?
                           $"contains({property}, tolower('{value}'))" :
                           $"contains({property}, '{value}')");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.StartsWith)
                {
                    return(column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ?
                           $"startswith({property}, tolower('{value}'))" :
                           $"startswith({property}, '{value}')");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.EndsWith)
                {
                    return(column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ?
                           $"endswith({property}, tolower('{value}'))" :
                           $"endswith({property}, '{value}')");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.Equals)
                {
                    return(column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ?
                           $"{property} eq tolower('{value}')" :
                           $"{property} eq '{value}'");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.NotEquals)
                {
                    return(column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ?
                           $"{property} ne tolower('{value}')" :
                           $"{property} ne '{value}'");
                }
            }
            else if (PropertyAccess.IsNumeric(column.FilterPropertyType))
            {
                return($"{property} {ODataFilterOperators[columnFilterOperator]} {value}");
            }
            else if (column.FilterPropertyType == typeof(bool) || column.FilterPropertyType == typeof(bool?))
            {
                return($"{property} eq {value.ToLower()}");
            }
            else if (column.FilterPropertyType == typeof(DateTime) ||
                     column.FilterPropertyType == typeof(DateTime?) ||
                     column.FilterPropertyType == typeof(DateTimeOffset) ||
                     column.FilterPropertyType == typeof(DateTimeOffset?))
            {
                return($"{property} {ODataFilterOperators[columnFilterOperator]} {DateTime.Parse(value, null, System.Globalization.DateTimeStyles.RoundtripKind).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")}");
            }
            else if (column.FilterPropertyType == typeof(Guid) || column.FilterPropertyType == typeof(Guid?))
            {
                return($"{property} {ODataFilterOperators[columnFilterOperator]} {value}");
            }

            return("");
        }
        private static string GetColumnFilter <T>(RadzenDataGridColumn <T> column, bool second = false)
        {
            var property = PropertyAccess.GetProperty(column.GetFilterProperty());

            if (property.IndexOf(".") != -1)
            {
                property = $"({property})";
            }

            var columnFilterOperator = !second?column.GetFilterOperator() : column.GetSecondFilterOperator();

            var linqOperator = LinqFilterOperators[columnFilterOperator];

            if (linqOperator == null)
            {
                linqOperator = "==";
            }

            var value = !second ? (string)Convert.ChangeType(column.GetFilterValue(), typeof(string)) :
                        (string)Convert.ChangeType(column.GetSecondFilterValue(), typeof(string));

            if (column.FilterPropertyType == typeof(string))
            {
                string filterCaseSensitivityOperator = column.Grid.FilterCaseSensitivity == FilterCaseSensitivity.CaseInsensitive ? ".ToLower()" : "";

                if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.Contains)
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.Contains(""{value}""{filterCaseSensitivityOperator})");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.StartsWith)
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.StartsWith(""{value}""{filterCaseSensitivityOperator})");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.EndsWith)
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator}.EndsWith(""{value}""{filterCaseSensitivityOperator})");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.Equals)
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator} == ""{value}""{filterCaseSensitivityOperator}");
                }
                else if (!string.IsNullOrEmpty(value) && columnFilterOperator == FilterOperator.NotEquals)
                {
                    return($@"({property} == null ? """" : {property}){filterCaseSensitivityOperator} != ""{value}""{filterCaseSensitivityOperator}");
                }
            }
            else if (PropertyAccess.IsNumeric(column.FilterPropertyType))
            {
                return($"{property} {linqOperator} {value}");
            }
            else if (column.FilterPropertyType == typeof(DateTime) ||
                     column.FilterPropertyType == typeof(DateTime?) ||
                     column.FilterPropertyType == typeof(DateTimeOffset) ||
                     column.FilterPropertyType == typeof(DateTimeOffset?))
            {
                var dateTimeValue = DateTime.Parse(value, null, System.Globalization.DateTimeStyles.RoundtripKind);
                var finalDate     = dateTimeValue.TimeOfDay == TimeSpan.Zero ? dateTimeValue.Date : dateTimeValue;
                var dateFormat    = dateTimeValue.TimeOfDay == TimeSpan.Zero ? "yyyy-MM-dd" : "yyyy-MM-ddTHH:mm:ssZ";

                return($@"{property} {linqOperator} DateTime(""{finalDate.ToString(dateFormat)}"")");
            }
            else if (column.FilterPropertyType == typeof(bool) || column.FilterPropertyType == typeof(bool?))
            {
                return($"{property} == {value}");
            }
            else if (column.FilterPropertyType == typeof(Guid) || column.FilterPropertyType == typeof(Guid?))
            {
                return($@"{property} {linqOperator} Guid(""{value}"")");
            }

            return("");
        }
Пример #9
0
        public static string ToFilterString <T>(this IEnumerable <RadzenDataGridColumn <T> > columns)
        {
            Func <RadzenDataGridColumn <T>, bool> canFilter = (c) => c.Filterable && c.FilterPropertyType != null &&
                                                              !(c.GetFilterValue() == null || c.GetFilterValue() as string == string.Empty) && c.GetFilterProperty() != null;

            if (columns.Where(canFilter).Any())
            {
                var gridLogicalFilterOperator = columns.FirstOrDefault()?.Grid?.LogicalFilterOperator;
                var gridBooleanOperator       = gridLogicalFilterOperator == LogicalFilterOperator.And ? "and" : "or";

                var whereList = new List <string>();
                foreach (var column in columns.Where(canFilter))
                {
                    string value       = "";
                    string secondValue = "";

                    if (PropertyAccess.IsDate(column.FilterPropertyType))
                    {
                        var v  = column.GetFilterValue();
                        var sv = column.GetSecondFilterValue();
                        if (v != null)
                        {
                            value = v is DateTime ? ((DateTime)v).ToString("yyyy-MM-ddTHH:mm:ss.fffZ") : v is DateTimeOffset ? ((DateTimeOffset)v).UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") : "";
                        }
                        if (sv != null)
                        {
                            secondValue = sv is DateTime ? ((DateTime)sv).ToString("yyyy-MM-ddTHH:mm:ss.fffZ") : sv is DateTimeOffset ? ((DateTimeOffset)sv).UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ") : "";
                        }
                    }
                    else
                    {
                        value       = (string)Convert.ChangeType(column.GetFilterValue(), typeof(string));
                        secondValue = (string)Convert.ChangeType(column.GetSecondFilterValue(), typeof(string));
                    }

                    if (!string.IsNullOrEmpty(value))
                    {
                        var linqOperator = LinqFilterOperators[column.GetFilterOperator()];
                        if (linqOperator == null)
                        {
                            linqOperator = "==";
                        }

                        var booleanOperator = column.LogicalFilterOperator == LogicalFilterOperator.And ? "and" : "or";

                        if (string.IsNullOrEmpty(secondValue))
                        {
                            whereList.Add(GetColumnFilter(column, value));
                        }
                        else
                        {
                            whereList.Add($"({GetColumnFilter(column, value)} {booleanOperator} {GetColumnFilter(column, secondValue, true)})");
                        }
                    }
                }

                return(string.Join($" {gridBooleanOperator} ", whereList.Where(i => !string.IsNullOrEmpty(i))));
            }

            return("");
        }