Skip to content

Firefly III: a personal finances manager

Improve search with table identifier for nav.

James Cole0974d6fc50d0
diff --git a/app/View/Components/Elements/Alpine/PageNavigation.php b/app/View/Components/Elements/Alpine/PageNavigation.php
index 9e36b8b9aa..e1d5a49b23 100644
--- a/app/View/Components/Elements/Alpine/PageNavigation.php
+++ b/app/View/Components/Elements/Alpine/PageNavigation.php
@@ -13,7 +13,7 @@ class PageNavigation extends Component
/**
* Create a new component instance.
*/
- public function __construct() {}
+ public function __construct(public string $sortIdentifier) {}
/**
* Get the view / contents that represent the component.
diff --git a/resources/assets/v3/js/pages/accounts/index.js b/resources/assets/v3/js/pages/accounts/index.js
index 1c4615754a..bb8ff25a30 100644
--- a/resources/assets/v3/js/pages/accounts/index.js
+++ b/resources/assets/v3/js/pages/accounts/index.js
@@ -55,8 +55,10 @@ let index = function () {
// default settings for sortable tables.
storageKey: "",
- defaultSortColumn: "order",
- defaultSortDirection: "asc",
+ defaultSortColumn: "order", // TODO obsolete?
+ defaultSortDirection: "asc", // TODO obsolete?
+ defaultSortColumns: {"main": "order"},
+ defaultSortDirections: {"main": "asc"},
// variables necessary for sortable tables.
pageNavUrl: "./accounts/",
@@ -73,7 +75,7 @@ let index = function () {
init() {
// initialize sortable table.
this.storageKey = "accounts-" + this.objectType + (this.active ? "-active" : "-inactive");
- this.defaultSortColumn = "expense" === this.objectType || "revenue" === this.objectType ? "name" : this.defaultSortColumn;
+ this.defaultSortColumns = "expense" === this.objectType || "revenue" === this.objectType ? {"main": "name"} : this.defaultSortColumns;
this.initFromLocalStorage();
this.initFromQueryParams();
@@ -94,8 +96,8 @@ let index = function () {
this.active = "inactive-accounts" !== address[address.length - 2];
// configure sortable table with final result (not filter).
- this.sortableTable.sortColumn = this.defaultSortColumn;
- this.sortableTable.sortDirection = this.defaultSortDirection;
+ this.sortableTable.sortColumns = this.defaultSortColumns;
+ this.sortableTable.sortDirections = this.defaultSortDirections;
this.sortableTable.storageKey = this.storageKey;
this.sortableTable.page = this.page;
@@ -166,7 +168,12 @@ let index = function () {
// since this function only downloads for table "main", it is safe to extract only "main" from the filter object.
this.sortableTable.loadingNewSort = true;
- let sort = "asc" === this.sortableTable.sortDirection ? this.sortableTable.sortColumn : "-" + this.sortableTable.sortColumn;
+ let sort = "asc" === this.sortableTable.sortDirections["main"] ? this.sortableTable.sortColumns["main"] : "-" + this.sortableTable.sortColumns["main"];
+
+ // catch undefined stuff.
+ if('-undefined' === sort) {
+ sort = "asc" === this.defaultSortDirections["main"] ? this.defaultSortColumns["main"] : "-" + this.defaultSortColumns["main"];
+ }
let start = window.store.get("start");
let end = window.store.get("end");
this.convertToPrimary = window.store.get("convert_to_primary");
@@ -188,6 +195,10 @@ let index = function () {
this.differences = {};
this.totalPages = parseInt(response.data.meta.pagination.total_pages);
+ if(this.totalPages > this.page) {
+ this.page = this.totalPages;
+ this.sortableTable.page = this.totalPages;
+ }
for (let i = 0; i < response.data.data.length; i++) {
if (Object.hasOwn(response.data.data, i)) {
let current = this.filterAmounts(response.data.data[i]);
diff --git a/resources/assets/v3/js/pages/shared/sortable-tables.js b/resources/assets/v3/js/pages/shared/sortable-tables.js
index eecfb3ba25..4577288d5a 100644
--- a/resources/assets/v3/js/pages/shared/sortable-tables.js
+++ b/resources/assets/v3/js/pages/shared/sortable-tables.js
@@ -23,6 +23,9 @@ import getNearestParent from '../shared/get-nearest-parent.js'
export class sortableTable {
tableIds = {};
+ sortColumns = {};
+ sortDirections = {};
+ pages = {};
sortColumn = "order"; // todo forbidden to use!
sortDirection = "asc"; // todo forbidden to use!
storageKey = "";
@@ -36,9 +39,12 @@ export class sortableTable {
//this.tableId = tableId;
for (let i = 0; i < tableIds.length; i++) {
let tableId = tableIds[i];
+ this.sortColumns[tableId] = "order";
+ this.sortDirections[tableId] = "asc";
this.tableIds[tableId] = {
sortColumn: "order",
sortDirection: "asc",
+ page: 1,
};
console.log('Initialized sort table "' + tableId + '"');
}
@@ -48,6 +54,7 @@ export class sortableTable {
this.handleFunc = this.handlePageClick.bind(this);
this.parent = parent;
queueMicrotask(() => {
+ console.log('Now calling update header classes from init-queuemicrotask');
this.updateHeaderClasses();
this.addClickEvents();
});
@@ -59,12 +66,12 @@ export class sortableTable {
get: (searchParams, prop) => searchParams.get(prop),
});
for (let tableId in this.tableIds) {
- console.log(tableId, this.tableIds[tableId]);
+ // console.log(tableId, this.tableIds[tableId]);
document
.querySelectorAll('table[data-sort-identifier="' + tableId + '"] thead th[data-filter-column]')
.forEach((th) => {
let column = th.getAttribute("data-filter-column");
- console.log('[class] Will watch "'+tableId+'" filter for column "' + column + '"');
+ // console.log('[class] Will watch "'+tableId+'" filter for column "' + column + '"');
// also grab current filter value from store AND from url and push back to the parent whichever is set.
const key = "filter[" + tableId + "][" + column + "]";
@@ -75,7 +82,7 @@ export class sortableTable {
// we do this in the parent because Alpine has the watch function that is needed.
this.parent.$watch("filter." + tableId + "." + column, (newValue, oldValue) => {
- console.log('filter for "'+tableId+'" column "' + column + '" changed from "' + oldValue + '" to "' + newValue + '"',);
+ //console.log('filter for "'+tableId+'" column "' + column + '" changed from "' + oldValue + '" to "' + newValue + '"',);
this.fireSortOrDataChangeEvent(tableId);
if ("" !== newValue) {
showSearchBox(th);
@@ -108,28 +115,52 @@ export class sortableTable {
const parentTable = getNearestParent(parent, "table");
const tableId = parentTable.dataset.sortIdentifier;
let newColumn = parent.dataset.sortColumn;
+
+ // old
this.sortDirection = typeof this.sortDirection !== "undefined" ? this.sortDirection : "asc";
+ // new: sort direction and sort column to be put in tableIds[tableId].
+ this.sortDirections[tableId] = typeof this.sortDirections[tableId] !== "undefined" ? this.sortDirections[tableId] : "asc";
+
+ // old:
if (newColumn === this.sortColumn) {
this.sortDirection = "asc" === this.sortDirection ? "desc" : "asc";
}
+ // new:
+ if (newColumn === this.sortColumns[tableId]) {
+ this.sortDirections[tableId] = "asc" === this.sortDirections[tableId] ? "desc" : "asc";
+ }
+
+ // old:
if (newColumn !== this.sortColumn) {
this.sortColumn = newColumn;
}
+ // new:
+ if (newColumn !== this.sortColumns[tableId]) {
+ this.sortColumns[tableId] = newColumn;
+ }
this.fireSortOrDataChangeEvent(tableId);
}
handlePageClick(e) {
let link = e.currentTarget;
+ let nav = getNearestParent(link, "nav");
+ const tableId = nav.dataset.sortIdentifier;
let page = parseInt(link.dataset.page);
if (isNaN(page)) {
e.preventDefault();
return false;
}
+ // old:
this.page = page;
+ // new:
+ this.pages[tableId] = page;
+
+ // should be removed:
this.parent.page = page;
+
// some sort of trigger to parent?
- this.parent.updateHistory(page, this.sortColumn, this.sortDirection);
+ this.parent.updateHistory(page, this.sortColumns, this.sortDirections); // TODO give all pages, sort columns and sort directions to parent
this.parent.downloadObjects();
e.preventDefault();
queueMicrotask(() => {
@@ -139,24 +170,25 @@ export class sortableTable {
}
fireSortOrDataChangeEvent(tableId) {
+ console.log('Now calling update header classes from fireSortOrDataChangeEvent');
this.updateHeaderClasses();
let obj = {
tableId: tableId,
- sortColumn: this.sortColumn,
- sortDirection: this.sortDirection,
+ sortColumn: this.sortColumns[tableId],
+ sortDirection: this.sortDirections[tableId],
};
console.log("Change sort instructions", obj);
let event = new CustomEvent("sortable-table-sort-change", {
detail: obj,
});
if (!this.disableRefresh) {
- console.log("Fire event!");
+ // console.log("Fire event!");
document.dispatchEvent(event);
}
- if (this.disableRefresh) {
- console.log("Do NOT fire event!");
- this.disableRefresh = false;
- }
+ // if (this.disableRefresh) {
+ // console.log("Do NOT fire event!");
+ // this.disableRefresh = false;
+ // }
}
clickCloseButton(event) {
@@ -252,20 +284,23 @@ export class sortableTable {
updateHeaderClasses() {
for (let tableId in this.tableIds) {
+ console.log('Update headers for table "' + tableId + '"');
+ console.log('Sort directions', JSON.parse(JSON.stringify(this.sortDirections)));
+ console.log('Sort columns', JSON.parse(JSON.stringify(this.sortColumns)));
document.querySelectorAll('table[data-sort-identifier="' + tableId + '"] thead th[data-sort-column]')
.forEach((th) => {
- if (this.sortColumn === th.dataset.sortColumn) {
+ if (this.sortColumns[tableId] === th.dataset.sortColumn) {
th.classList.add("sortable_sorted");
- if ("asc" === this.sortDirection) {
+ if ("asc" === this.sortDirections[tableId]) {
th.classList.add("sortable_sorted_asc");
th.classList.remove("sortable_sorted_desc");
}
- if ("desc" === this.sortDirection) {
+ if ("desc" === this.sortDirections[tableId]) {
th.classList.add("sortable_sorted_desc");
th.classList.remove("sortable_sorted_asc");
}
}
- if (this.sortColumn !== th.dataset.sortColumn) {
+ if (this.sortColumns[tableId] !== th.dataset.sortColumn) {
th.classList.remove("sortable_sorted_asc");
th.classList.remove("sortable_sorted_desc");
th.classList.remove("sortable_sorted");
@@ -320,9 +355,8 @@ export function initFromLocalStorage() {
// grab state from localStorage.
let fromStore = window.store.get(this.storageKey);
if (fromStore) {
- this.defaultSortColumn = fromStore.column;
- this.defaultSortDirection = fromStore.direction;
- this.defaultFilter = fromStore.filter;
+ this.defaultSortColumns = fromStore.column;
+ this.defaultSortDirections = fromStore.direction;
}
}
@@ -339,12 +373,15 @@ export function initFromQueryParams() {
export function respondToSortChange(event) {
console.log("sortable-table-sort-change", event.detail);
- this.sortableTable.tableIds[event.detail.tableId].sortColumn = event.detail.sortColumn;
- this.sortableTable.tableIds[event.detail.tableId].sortDirection = event.detail.sortDirection;
+ const tableId = event.detail.tableId;
+ this.sortableTable.sortColumns[tableId] = event.detail.sortColumn;
+ this.sortableTable.sortDirections[tableId] = event.detail.sortDirection;
+ // this.sortableTable.tableIds[event.detail.tableId].sortColumn = event.detail.sortColumn;
+ // this.sortableTable.tableIds[event.detail.tableId].sortDirection = event.detail.sortDirection;
//this.sortableTable.sortColumn = event.detail.sortColumn;
//this.sortableTable.sortDirection = event.detail.sortDirection;
- this.updateHistory(this.page, event.detail.sortColumn, event.detail.sortDirection);
- this.storeFilterAndSort(this.storageKey, event.detail.sortColumn, event.detail.sortDirection, this.filter);
+ this.updateHistory(this.page, this.sortableTable.sortColumns, this.sortableTable.sortDirections); // TODO Need fix.
+ this.storeFilterAndSort(this.storageKey, this.sortableTable.sortColumns, this.sortableTable.sortDirections, this.filter); // TODO store for ALL.
this.downloadObjects();
}
diff --git a/resources/views/accounts/index.blade.php b/resources/views/accounts/index.blade.php
index c4f09ae691..4b1e1fd428 100644
--- a/resources/views/accounts/index.blade.php
+++ b/resources/views/accounts/index.blade.php
@@ -18,7 +18,7 @@
:linkTitle="__('firefly.make_new_'. $objectType . '_account')"/>
<div class="card-body p-0">
<template x-if="totalPages > 1">
- <x-elements.alpine.page-navigation/>
+ <x-elements.alpine.page-navigation sort-identifier="main"/>
</template>
<table data-sort-identifier="main" class="table table-valign-middle table-sm table-hover sortable">
@@ -368,7 +368,7 @@
</table>
<template x-if="totalPages > 1">
- <x-elements.alpine.page-navigation/>
+ <x-elements.alpine.page-navigation sort-identifier="main"/>
</template>
</div>
<x-elements.card-footer-with-menu
diff --git a/resources/views/components/elements/alpine/page-navigation.blade.php b/resources/views/components/elements/alpine/page-navigation.blade.php
index dca716aba9..a25766ddfd 100644
--- a/resources/views/components/elements/alpine/page-navigation.blade.php
+++ b/resources/views/components/elements/alpine/page-navigation.blade.php
@@ -1,6 +1,6 @@
<template x-if="totalPages > 1" x-effect="sortableTable ? sortableTable.capturePageNavigation() : null">
<div class="m-2">
- <nav>
+ <nav data-sort-identifier="{{ $sortIdentifier }}">
<ul class="pagination">
<li :class="{'page-item': true, 'disabled': 1 === page}" :aria-disabled="1 === page ? 'true' : 'false'" aria-label="{!! __('pagination.previous') !!}">
<template x-if="1 === page">