This is a migrated thread and some comments may be shown as answers.

[Solved] Problems Using Persisted Filter Settings

2 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jordan
Top achievements
Rank 1
Jordan asked on 21 Sep 2011, 05:29 PM
I'm having an issue restoring the Filter settings for a grid on the loading of a View.  My current method almost works perfectly.  Upon any change to the grid (sorting, filtering, re-ordering, etc.) I am passing the current settings to the server using the following code:

function save_grid_settings() {
    var grid = $('#table').data('tGrid');
 
    if (grid) {
        function get_GridSettings() {
            var index = 0, settings = [];
 
            for (index = 0; index < grid.columns.length; index++) {
                var column = $('#table').find('thead tr th').eq(index);
 
                settings.push({
                    hidden: grid.columns[index].hidden | false,
                    member: grid.columns[index].member,
                    title: grid.columns[index].title,
                    template: grid.columns[index].template
                });
            }
 
            $.ajax({
                data: {
                    filterBy: grid.filterBy,
                    orderBy: grid.orderBy,
                    columns: $.toJSON(settings)
                },
                dataType: 'json',
                type: 'POST',
                url: '@Url.RouteUrl("work_settings_ajax", new { })'
            });
        }
 
        setTimeout(get_GridSettings, 50);
    }
}

This provides me with a nice simple way to serialize and persist the settings for users that customize the grid to fit their needs.  I followed the custom binding model on the loading of the data for the grid with the following code:

[HttpGet]
public ActionResult List() {
    // If the user has any stored column settings for the work list.
    if (CurrentUser.WithSettings.ContainsKey("Work_List.Columns")) {
        // Retrieve the serialized column settings for the user's work list.
        var serialized = CurrentUser.WithSettings["Work_List.Columns"];
 
        // Retrieve the deserialized version of the users column settings.
        var serializer = new JavaScriptSerializer();
        var deserialized = serializer.Deserialize<IDictionary<string, object>[]>(serialized);
 
        // Initialize a collection to contain the column settings for the grid.
        var settings = new List<GridColumnSettings>();
 
        // Iterate through the collection of retrieved settings.
        foreach (var setting in deserialized) {
            // Add the new column setting to the collection of settings.
            settings.Add(new GridColumnSettings {
                ClientTemplate = setting["template"].ToString(),
                Hidden = Convert.ToBoolean(setting["hidden"]),
                Member = setting["member"].ToString(),
                Title = setting["title"].ToString()
            });
        }
 
        // Store the initialized column settings in the view state.
        ViewData["columns"] = settings;
    }
 
    // Initialize the values for the filtering, grouping, and ordering of the grid.
    string groupBy = string.Empty, filterBy = string.Empty, orderBy = string.Empty;
             
    // Retrieve any values that might exist for the settings.
    CurrentUser.WithSettings.TryGetValue("Work_List.OrderBy", out orderBy);
    CurrentUser.WithSettings.TryGetValue("Work_List.FilterBy", out filterBy);
 
    // Initialize the command object using the retrieved values.
    var command = GridCommand.Parse(1, 10, orderBy, groupBy, filterBy);
            
    // Store the current filter and sort settings for the grid.
    ViewData["order"] = command.SortDescriptors;
    ViewData["orderBy"] = orderBy;
    ViewData["filter"] = command.FilterDescriptors;
    ViewData["filterBy"] = filterBy;
 
    // Initialize the variable to contain the count for the current query.
    int count = 0;
 
    // Retrieve the data for the list using the current command
    IEnumerable<SummaryModel> data = GetData(command, out count);
 
    // Store the total count for the view.
    ViewData["count"] = count;
 
    // Return the current view with the initialized data.
    return View(data);
}
 
private IEnumerable<SummaryModel> GetData(GridCommand command, out int total) {
    // Execute the base query for the retrieval of the data for the user.
    var details =
                from assignment in ActiveRecordLinq.AsQueryable<WorkAssignment>()
                from detail in ActiveRecordLinq.AsQueryable<DetailedWorkDTO>()
                where assignment.ForUser == CurrentUser &&
                        assignment.Status == AssignmentStatusTypes.Active &&
                        (assignment.ForWork.Id == detail.TaskId ||
                        (assignment.ForWork.Id == detail.ProjectId && detail.TaskId == null))
                select detail;
 
    // Apply the filtering that was returned by the user.
    details = details.ApplyFiltering(command.FilterDescriptors);
 
    // Retrieve the number of items that was returned by the query.
    total = details.Count();
 
    // Apply the paging, sorting, and grouping.
    details = details.ApplySorting(command.GroupDescriptors, command.SortDescriptors);
    details = details.ApplyPaging(command.Page, command.PageSize);
 
    // Retrieve the model data from the set of results.
    var data = details.AsEnumerable().Select(detail => new SummaryModel {
        Project = new WorkSharedModel {
            Id = detail.ProjectId.Value.Shrink(),
            Name = detail.ProjectName
        },
        Task = new WorkSharedModel {
            Id = detail.TaskId == null ? string.Empty : detail.TaskId.Value.Shrink(),
            Name = detail.TaskName
        },
        Coordinator = new UserSharedModel {
            Id = detail.CoordinatorId,
            AvatarId = (detail.CoordinatorAvatarId ?? Guid.Empty),
            Name = detail.CoordinatorName
        },
        Actual = new PropertySharedModel<int> {
            Value = detail.Actual,
            Display = TimeSpan.FromMinutes(detail.Actual).ToShortManHourString()
        },
        Estimate = new PropertySharedModel<int> {
            Value = detail.Estimate,
            Display = TimeSpan.FromMinutes(detail.Estimate).ToShortManHourString()
        },
        EndDate = new PropertySharedModel<DateTime?> {
            Value = detail.EndDate,
            Display = detail.EndDate == null ? "NA" : detail.EndDate.Value.AddMinutes(TimeZoneOffset).ToShortDateString()
        },
        StartDate = new PropertySharedModel<DateTime?> {
            Value = detail.StartDate,
            Display = detail.StartDate == null ? "NA" : detail.StartDate.Value.AddMinutes(TimeZoneOffset).ToShortDateString()
        },
        Status = new PropertySharedModel<WorkStatusTypes> {
            Value = detail.Status,
            Display = detail.Status.GetDescription()
        },
        Percent = new PropertySharedModel<decimal> {
            Value = detail.Percent,
            Display = detail.Percent.ToString("P")
        }
    });
 
    return data;
}

Within the view I'm setting up things as needed to provide the GridBuilder with the information as needed.  The dynamic column selection and sort order work great and display correctly.  The only problem I'm having is that the filter configuration doesn't seem to be initializing correctly.  I'm using the following code to initialize my filter:

.Filterable(builder => {
    builder.Enabled(true);
 
    builder.Filters(config => {
        if (ViewData.ContainsKey("filter")) {
            var filters = (IList<Telerik.Web.Mvc.IFilterDescriptor>)ViewData["filter"];
                             
            if (filters != null && filters.Count > 0) {
                var expression = Telerik.Web.Mvc.ExpressionBuilder.Expression<SandCastle.Web.Areas.Work.Models.Work.SummaryModel>(filters);
                config.Add(expression);
            }
        }
    });
})

In theory this should work; unfortunately, in practice it doesn't.  The page loads fine, but none of the filter settings (i.e., icon states and values within the filter dialog) show for the grid.  What am I missing/doing wrong?

2 Answers, 1 is accepted

Sort by
0
Jordan
Top achievements
Rank 1
answered on 26 Sep 2011, 09:48 PM
I hope you guys had a happy Independence Day.  Now, about that filter issue . . .
0
Jordan
Top achievements
Rank 1
answered on 13 Oct 2011, 04:42 PM
(bump)
Tags
Grid
Asked by
Jordan
Top achievements
Rank 1
Answers by
Jordan
Top achievements
Rank 1
Share this question
or