From 824779bb1de24863cac89143906bf56f830b647a Mon Sep 17 00:00:00 2001 From: Berkan Sasmaz Date: Thu, 6 May 2021 03:03:12 +0300 Subject: [PATCH] Add pagination with widget on Organization list --- .../Controllers/OrganizationController.cs | 15 +++++ .../OrganizationsArea/Default.cshtml | 22 +++++++ .../OrganizationsAreaViewComponent.cs | 62 +++++++++++++++++++ .../OrganizationsArea/organizations-area.js | 35 +++++++++++ .../Pages/Organizations/Index.cshtml | 42 ++----------- 5 files changed, 139 insertions(+), 37 deletions(-) create mode 100644 src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/Default.cshtml create mode 100644 src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/OrganizationsAreaViewComponent.cs create mode 100644 src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/organizations-area.js diff --git a/src/EventHub.Web/Controllers/OrganizationController.cs b/src/EventHub.Web/Controllers/OrganizationController.cs index 06a9859..582da26 100644 --- a/src/EventHub.Web/Controllers/OrganizationController.cs +++ b/src/EventHub.Web/Controllers/OrganizationController.cs @@ -1,9 +1,11 @@ using System.IO; +using System.Linq; using System.Threading.Tasks; using EventHub.Organizations; using EventHub.Web.Pages.Organizations; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Application.Dtos; using Volo.Abp.AspNetCore.Mvc; namespace EventHub.Web.Controllers @@ -18,6 +20,19 @@ namespace EventHub.Web.Controllers _organizationAppService = organizationAppService; } + [HttpGet] + [Route("get-list")] + public async Task GetList(PagedResultRequestDto input) + { + ViewData.Model = (await _organizationAppService.GetListAsync(input)).Items.ToList(); + + return new PartialViewResult + { + ViewName = "~/Pages/Organizations/Components/OrganizationsArea/_organizationListSection.cshtml", + ViewData = ViewData + }; + } + [HttpPost] [Authorize] [Route("save-profile-picture")] diff --git a/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/Default.cshtml b/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/Default.cshtml new file mode 100644 index 0000000..98fdc3e --- /dev/null +++ b/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/Default.cshtml @@ -0,0 +1,22 @@ +@model EventHub.Web.Pages.Organizations.Components.OrganizationsArea.OrganizationsAreaViewComponent.ListAreaViewComponentModel + +@if (Model.Organizations != null && Model.Organizations.Count > 0) +{ +
+ +
+ + @if (Model.IsPagination && Model.TotalCount > Model.MaxResultCount && Model.TotalCount > Model.Organizations.Count) + { +
+
+ +
+
+ } +} \ No newline at end of file diff --git a/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/OrganizationsAreaViewComponent.cs b/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/OrganizationsAreaViewComponent.cs new file mode 100644 index 0000000..a406aac --- /dev/null +++ b/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/OrganizationsAreaViewComponent.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using EventHub.Organizations; +using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Application.Dtos; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.Widgets; + +namespace EventHub.Web.Pages.Organizations.Components.OrganizationsArea +{ + [Widget( + AutoInitialize = true, + ScriptFiles = new[] {"/Pages/Organizations/Components/OrganizationsArea/organizations-area.js"})] + public class OrganizationsAreaViewComponent : AbpViewComponent + { + private readonly IOrganizationAppService _organizationAppService; + + public OrganizationsAreaViewComponent(IOrganizationAppService organizationAppService) + { + _organizationAppService = organizationAppService; + } + + public async Task InvokeAsync( + int? skipCount, + int maxResultCount = 15, + bool isPagination = true) + { + var result = await _organizationAppService.GetListAsync( + new PagedResultRequestDto + { + SkipCount = skipCount.GetValueOrDefault(), + MaxResultCount = maxResultCount + } + ); + + return View( + "~/Pages/Organizations/Components/OrganizationsArea/Default.cshtml", + new OrganizationsAreaViewComponent.ListAreaViewComponentModel + { + Organizations = result.Items, + TotalCount = result.TotalCount, + SkipCount = skipCount.GetValueOrDefault(), + MaxResultCount = maxResultCount, + IsPagination = isPagination + } + ); + } + + public class ListAreaViewComponentModel + { + public IReadOnlyList Organizations { get; set; } + + public long TotalCount { get; set; } + + public int SkipCount { get; set; } + + public int MaxResultCount { get; set; } + + public bool IsPagination { get; set; } + } + } +} \ No newline at end of file diff --git a/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/organizations-area.js b/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/organizations-area.js new file mode 100644 index 0000000..3d4540d --- /dev/null +++ b/src/EventHub.Web/Pages/Organizations/Components/OrganizationsArea/organizations-area.js @@ -0,0 +1,35 @@ +(function () { + abp.widgets.OrganizationsArea = function ($wrapper) { + var totalCount = Number($wrapper.find('[data-total-count]').attr('data-total-count')) + var skipCount = Number($wrapper.find('[data-skip-count]').attr('data-skip-count')) + var maxResultCount = Number($wrapper.find('[data-max-result-count]').attr('data-max-result-count')) + + function init() { + var loadMoreButton = $wrapper.find('#LoadMoreButton'); + loadMoreButton.click(function (e) { + e.preventDefault(); + skipCount += maxResultCount; + loadMoreButton.buttonBusy(true); + abp.ajax({ + type: 'GET', + dataType: 'html', + contentType: 'text/html; charset=utf-8', + url: (loadMoreButton.attr('data-url') + '&skipCount=' + skipCount) + }).then(function (response) { + var organizationList = $wrapper.find('#OrganizationList') + organizationList.append(response); + }).always(function () { + var organizationCount = $('.organization').length; + if (Number(organizationCount) >= totalCount) { + $('.load-more-section').hide(); + } + loadMoreButton.buttonBusy(false); + }); + }); + } + + return { + init: init + }; + }; +})(); diff --git a/src/EventHub.Web/Pages/Organizations/Index.cshtml b/src/EventHub.Web/Pages/Organizations/Index.cshtml index 3a23366..a179608 100644 --- a/src/EventHub.Web/Pages/Organizations/Index.cshtml +++ b/src/EventHub.Web/Pages/Organizations/Index.cshtml @@ -1,6 +1,7 @@ @page "/organizations" @inject IHtmlLocalizer L @using EventHub.Localization +@using EventHub.Web.Pages.Organizations.Components.OrganizationsArea @using Microsoft.AspNetCore.Mvc.Localization @model EventHub.Web.Pages.Organizations.IndexPageModel @@ -25,41 +26,8 @@
- -
- @foreach (var organization in Model.Organizations) - { -
-
- @if (organization.ProfilePictureContent == null) - { - @organization.Name - } - else - { - @organization.Name - } -
-
-
-
@organization.DisplayName
-

- @organization.Description -

-
- Details -
-
-
-
- } -
- -
-
- -
-
+ @await Component.InvokeAsync(typeof(OrganizationsAreaViewComponent), new + { + maxResultCount = 12 + })
\ No newline at end of file