Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

Browse sort using another field Topic is solved

Questions related to using nuBuilder Forte.
Post Reply
mih
Posts: 30
Joined: Thu Jan 26, 2023 12:04 pm
Has thanked: 3 times

Browse sort using another field

Unread post by mih »

When clicking on field header in browse form is it possible to use other field (not that from clicked header)?

Example: I have field , call it "Num" with numbers, format of numbers like "1-2022", "2-2022", "12-2023" and so on. Then sorting straight by numbers we receive sequence "1-2022", "12-2023", "2-2022". I can make calculated field in browse query like "2022001", "2022002", "2023012 " and hide it by width 0.
How to use this hidden field to sort rows, when clicking on "Num" header?
kev1n
nuBuilder Team
Posts: 4416
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 74 times
Been thanked: 472 times
Contact:

Re: Browse sort using another field

Unread post by kev1n »

Hi,

The easiest method would probably be to initially display the values in the format 2022001 and then use JavaScript to reformat them as needed. This approach ensures that sorting functions correctly since the formatting aligns with the original value, such as 2022001.

Code to be added in Custom Code / Browse section:

Code: Select all

function convertDateFormat(input) {
	const dateRegex = /^(\d{4})0(\d{2})$/;

	if (!dateRegex.test(input)) {
		return "Invalid input format";
	}

	const [, year, month] = input.match(dateRegex);
	const formattedMonth = month.startsWith('0') ? month.slice(1) : month;

	return `${formattedMonth}-${year}`;
}

function formatColumns() {
  
	const column = 1; // which column to format. 0 = first column, 1 = 2nd column etc.
	const $columnElements = $(`[data-nu-column='${column}']`);

	$columnElements.each(function() {
		const value = $(this).html();
		if (value) {
			$(this).html(convertDateFormat(value));
		}
	});
}

formatColumns();
mih
Posts: 30
Joined: Thu Jan 26, 2023 12:04 pm
Has thanked: 3 times

Re: Browse sort using another field

Unread post by mih »

Thanks, it works!
Those are not months - just number, up to 999.
Also change regexp to /^(\d{4})0*(\d+)$/

Please move topic to "Custom code" so solution was as "Custom code".
Post Reply