OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#55392] project list: pagination reset by dedicated controller
Tobias Dillmannceabb80c47da
diff --git a/app/components/projects/configure_view_modal_component.html.erb b/app/components/projects/configure_view_modal_component.html.erbindex 691908997ac..1c954bcf209 100644--- a/app/components/projects/configure_view_modal_component.html.erb+++ b/app/components/projects/configure_view_modal_component.html.erb@@ -1,6 +1,7 @@<%= render(Primer::Alpha::Dialog.new(title: t(:'queries.configure_view.heading'),size: :large,id: MODAL_ID,+ data: { controller: "pagination-reset-on-sort", application_target: "dynamic" },# Hack to give the draggable autcompleter (ng-select) bound to the dialog# enough height to display all options.# This is necessary as long as ng-select does not support popovers.@@ -10,11 +11,15 @@<%= primer_form_with(url: projects_path,id: QUERY_FORM_ID,- data: { "turbo-stream" => false },+ data: {+ "turbo-stream" => false,+ "pagination-reset-on-sort-target" => "form",+ action: "pagination-reset-on-sort#submit"+ },method: :get) do |form| %><% helpers.projects_query_params.except(:columns, :sortBy).each do |name, value| %>- <%= hidden_field_tag name, value %>+ <%= hidden_field_tag name, value, data: {"pagination-reset-on-sort-target" => name} %><% end %><%= render(Primer::Alpha::TabPanels.new(label: "label")) do |tab_panel| %><% tab_panel.with_tab(selected: true, id: "tab-selects--columns") do |tab| %>diff --git a/app/components/queries/sort_by_component.html.erb b/app/components/queries/sort_by_component.html.erbindex 041fe0328bf..ff4548f6a53 100644--- a/app/components/queries/sort_by_component.html.erb+++ b/app/components/queries/sort_by_component.html.erb@@ -1,7 +1,9 @@<%= render(Primer::Beta::Heading.new(tag: :h5)) { I18n.t('queries.configure_view.sort_by.automatic.heading') } %><%= render(Primer::Beta::Text.new(font_size: :small, color: :subtle)) { I18n.t('queries.configure_view.sort_by.automatic.description', plural: queried_model_name.plural) } %><div data-controller="sort-by-config" data-application-target="dynamic">- <%= hidden_field_tag :sortBy, current_orders, data: { "sort-by-config-target" => "sortByField" } %>+ <%= hidden_field_tag :sortBy, current_orders, data: {+ "sort-by-config-target" => "sortByField",+ "pagination-reset-on-sort-target" => "sortByField" } %><%= render(Primer::OpenProject::FlexLayout.new(data: { "sort-by-config-target" => "inputRowContainer" })) do |layout| %><% order_limit.times do |i| %><% layout.with_row(mt: 3, data: { "sort-by-config-target" => "inputRow" }) do %>diff --git a/frontend/src/stimulus/controllers/dynamic/pagination-reset-on-sort.controller.ts b/frontend/src/stimulus/controllers/dynamic/pagination-reset-on-sort.controller.tsnew file mode 100644index 00000000000..3654eef6c2c--- /dev/null+++ b/frontend/src/stimulus/controllers/dynamic/pagination-reset-on-sort.controller.ts@@ -0,0 +1,64 @@+/*+ * -- copyright+ * OpenProject is an open source project management software.+ * Copyright (C) the OpenProject GmbH+ *+ * This program is free software; you can redistribute it and/or+ * modify it under the terms of the GNU General Public License version 3.+ *+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+ * Copyright (C) 2006-2013 Jean-Philippe Lang+ * Copyright (C) 2010-2013 the ChiliProject Team+ *+ * This program is free software; you can redistribute it and/or+ * modify it under the terms of the GNU General Public License+ * as published by the Free Software Foundation; either version 2+ * of the License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU General Public License for more details.+ *+ * You should have received a copy of the GNU General Public License+ * along with this program; if not, write to the Free Software+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+ *+ * See COPYRIGHT and LICENSE files for more details.+ * +++ */++import { Controller } from '@hotwired/stimulus';++export default class PaginationResetOnSort extends Controller {+ static targets = [+ 'sortByField',+ 'form',+ 'page',+ ];++ declare readonly sortByFieldTarget:HTMLInputElement;+ declare readonly formTarget:HTMLFormElement;+ declare readonly pageTarget:HTMLInputElement;++ declare sortByFieldInitialSelection:string;++ connect():void {+ this.sortByFieldInitialSelection = this.sortByFieldTarget.value;+ }++ submit(event:Event):void {+ event.preventDefault();+ const currentSelection = this.sortByFieldTarget.value;++ if (this.sortByFieldInitialSelection !== currentSelection) {+ // When the sorting criteria changes, reset the pagination to the first page.+ // This leads to a better UX since the previous position in the pagination becomes+ // obsolete when the underlying collection changes due to sorting adjustments. It is better to start+ // from a fresh perspective on the data.+ this.pageTarget.value = '';+ }++ this.formTarget.submit();+ }+}