예제 #1
0
        /// <summary>
        /// Sets listing data (search keyword, sorting, advanced filters, pagination, saved search)
        /// </summary>
        /// <param name="listingDataModel"></param>
        /// <param name="forIndex"></param>
        private void SetData(ListingParams <M> listingDataModel, bool forIndex)
        {
            //read data from session only if the page is loaded for the first time (not AJAX search)
            //but only if the page is not a lookup modal (these don't need to retain search parameters)
            //for AJAX search, all parameters are sent by the client
            var searchParams = (forIndex && !this.IsLookup) ? (Session[searchParamsSessionKey] as SearchParams) : null;

            if (searchParams == null)
            {
                searchParams = new SearchParams();
            }

            var pages       = this.IsSubDetailPage ? new string[] { "5", "15", "50" } : new string[] { "15", "25", "50", "100" };
            var minPageSize = this.ListingViewModel.MinPageSize ?? (this.IsLookup ? 10 : (this.IsSubDetailPage ? 5 : 15));

            //if no advanced filters, try to retrieve from value providers
            if (listingDataModel.AdvancedFilters == null || listingDataModel.AdvancedFilters.Count == 0)
            {
                listingDataModel.AdvancedFilters = DataExtensions.JSONToDictionary(this.ValueProvider.GetValue("advancedFilters"));
            }

            //if no advanced filters, try to retrieve from session
            if (listingDataModel.AdvancedFilters == null || listingDataModel.AdvancedFilters.Count == 0)
            {
                listingDataModel.AdvancedFilters = DataExtensions.JSONToDictionary(searchParams.AdvancedFilters);
            }

            //current page index
            listingDataModel.PageIndex = (listingDataModel.PageIndex.HasValue) ? listingDataModel.PageIndex.Value - 1 : Convert.ToInt32(searchParams.PageIndex ?? 0);

            //number of rows per page
            if (listingDataModel.ItemsPerPage == 0)
            {
                //on lookup pages, the items per page is constant
                if (this.IsLookup)
                {
                    listingDataModel.ItemsPerPage = minPageSize;
                }
                else
                {
                    listingDataModel.ItemsPerPage = Convert.ToInt32(searchParams.ItemsPerPage ?? minPageSize);
                }
            }

            //keyword search
            if (listingDataModel.Keyword == null)
            {
                listingDataModel.Keyword = "";
            }

            if (String.IsNullOrEmpty(listingDataModel.Keyword))
            {
                listingDataModel.Keyword = (searchParams.Keyword ?? "").ToString();
            }

            //sorting; if not set from request, retrieve data from session
            if (listingDataModel.Sort == null)
            {
                listingDataModel.Sort = new ListingSortModel
                {
                    Column = (searchParams.SortByColumn ?? "").ToString(),
                    Asc    = Convert.ToBoolean(searchParams.SortAsc ?? true)
                };
            }

            if (listingDataModel.Model == null)
            {
                listingDataModel.Model = new M();
            }

            //save data into session
            if (!this.IsLookup && !this.IsSubDetailPage)
            {
                searchParams.ItemsPerPage    = listingDataModel.ItemsPerPage;
                searchParams.PageIndex       = listingDataModel.PageIndex;
                searchParams.Keyword         = listingDataModel.Keyword;
                searchParams.SortByColumn    = listingDataModel.Sort.Column;
                searchParams.SortAsc         = listingDataModel.Sort.Asc;
                searchParams.AdvancedFilters = listingDataModel.AdvancedFilters.ToJSON();

                //disable session
                //Session[this.searchParamsSessionKey] = searchParams;
            }

            //set view data
            ViewBag.ItemsPerPage = listingDataModel.ItemsPerPage;

            ViewBag.Pager     = pages.ToSelectList(listingDataModel.ItemsPerPage.ToString(), false);
            ViewBag.Sorted    = listingDataModel.Sort.Column;
            ViewBag.SortedAsc = listingDataModel.Sort.Asc;
        }