Sort fields in Admin view #43

Closed
opened 2017-02-23 15:04:55 +00:00 by Peeripapo · 12 comments
Peeripapo commented 2017-02-23 15:04:55 +00:00 (Migrated from github.com)

When editing an Admin View, it would be nice that after the first save, when we go to the fields section (where we can add/remove fields) they would appear ordered by "Tab" then "Order in Edit".

That would make it MUCH easier to make any changes to add /remove/ edit fields in a view.

When editing an Admin View, it would be nice that after the first save, when we go to the fields section (where we can add/remove fields) they would appear ordered by "Tab" then "Order in Edit". That would make it MUCH easier to make any changes to add /remove/ edit fields in a view.

Hmmm if you write the JavaScript of it I will add it 👍

Hmmm if you write the JavaScript of it I will add it :+1:
Peeripapo commented 2017-02-23 17:23:21 +00:00 (Migrated from github.com)

This could probably be done with an "ORDER BY" clause in the code that gets the fields. I don't know how/where to access that part of the code.

I think this is only available to you since you have the source JCB component that was created on JCB.

Otherwise If you point me out the right path, I'll change it gladly. :)

This could probably be done with an "ORDER BY" clause in the code that gets the fields. I don't know how/where to access that part of the code. I think this is only available to you since you have the source JCB component that was created on JCB. Otherwise If you point me out the right path, I'll change it gladly. :)

Hmmm I don't think so.... the data is stored as a repeatable field so that mean in the DB it is in json. Best do it with JavaScript on the page...

Or reorder it on saving the data before giving it to the DB on line 1458 of the model on the var $data['addfields'] do a var_dump to see the structure... or open the xml of the form to see the structure.

Just post the code here that makes it work for you, then I will add it to the JCB UI on my system to test, and compile it into this repo... since you do realize the JCB is build with JCB right...

Hmmm I don't think so.... the data is stored as a repeatable field so that mean in the DB it is in json. Best do it with JavaScript on the page... Or reorder it on saving the data before giving it to the DB on line [1458](https://github.com/vdm-io/Joomla-Component-Builder/blob/master/admin/models/admin_view.php#L1458) of the model on the var `$data['addfields']` do a var_dump to see the structure... or open the [xml of the form](https://github.com/vdm-io/Joomla-Component-Builder/blob/master/admin/models/forms/admin_view.xml#L500) to see the structure. Just post the code here that makes it work for you, then I will add it to the JCB UI on my system to test, and compile it into this repo... since you do realize the JCB is build with JCB right...
Peeripapo commented 2017-02-24 11:38:33 +00:00 (Migrated from github.com)

There you go, just add this after line 1458

	// Sort fields by "Tab" ASC, "Order in Edit" ASC
	$addfields = json_decode($data["addfields"], true);

	$out = array();

	foreach ($addfields as $key => $subarr) {
		foreach ($subarr as $subkey => $subvalue) {
			$out[$subkey][$key] = $subvalue;
		}
	}
	usort($out, function ($a, $b) {
		$val_a = $a['tab'].$a['order_edit'];
		$val_b = $b['tab'].$b['order_edit'];

		return strcmp($val_a, $val_b);
	});

	$addfields = array();
	foreach ($out as $key => $subarr) {
		foreach ($subarr as $subkey => $subvalue) {
			$addfields[$subkey][$key] = $subvalue;
		}
	}
	$data["addfields"] = json_encode($addfields, true);
There you go, just add this after line 1458 // Sort fields by "Tab" ASC, "Order in Edit" ASC $addfields = json_decode($data["addfields"], true); $out = array(); foreach ($addfields as $key => $subarr) { foreach ($subarr as $subkey => $subvalue) { $out[$subkey][$key] = $subvalue; } } usort($out, function ($a, $b) { $val_a = $a['tab'].$a['order_edit']; $val_b = $b['tab'].$b['order_edit']; return strcmp($val_a, $val_b); }); $addfields = array(); foreach ($out as $key => $subarr) { foreach ($subarr as $subkey => $subvalue) { $addfields[$subkey][$key] = $subvalue; } } $data["addfields"] = json_encode($addfields, true);

Hi @Peeripapo I just found that this new sort could cause some headache...

So I am wondering if you can adapt your code to keep the left 1-2-3-4 and the right 1-2-3-4 field in order instead of the left-1 right-1 then left-2 right-2 to get mixed into get the order L-1,R-1,L-2,R-2,L-3,R-3,L-4,R-4 which basically makes it hard to follow the order of one area/column of fields. IT should be L-1,L-2,L-3,L-4,R-1,R-2,R-3,R-4 this way they are sorted in order but also combined in position.

Let me know 👍

Hi @Peeripapo I just found that this new sort could cause some headache... So I am wondering if you can adapt your code to keep the left 1-2-3-4 and the right 1-2-3-4 field in order instead of the left-1 right-1 then left-2 right-2 to get mixed into get the order L-1,R-1,L-2,R-2,L-3,R-3,L-4,R-4 which basically makes it hard to follow the order of one area/column of fields. IT should be L-1,L-2,L-3,L-4,R-1,R-2,R-3,R-4 this way they are sorted in order but also combined in position. Let me know :+1:
Peeripapo commented 2017-03-03 08:44:41 +00:00 (Migrated from github.com)

Oh, it thought it was working that way. Gonna check into that.

Oh, it thought it was working that way. Gonna check into that.
Peeripapo commented 2017-03-03 08:58:57 +00:00 (Migrated from github.com)

Actually, it was just ordering by Tab, then by Field position. Should it be Tab, Position, Field then ?

Actually, it was just ordering by Tab, then by Field position. Should it be Tab, Position, Field then ?

I have views with many fields and many tabs, so best you setup some view like that, and then test your code until it produce the result of bundling the tab fields and then ordering the fields in the tabs by position and then in each position by order.

I have views with many fields and many tabs, so best you setup some view like that, and then test your code until it produce the result of bundling the tab fields and then ordering the fields in the tabs by position and then in each position by order.
Peeripapo commented 2017-03-03 15:33:02 +00:00 (Migrated from github.com)

Ok, here's the fix just replace models/admin_view.php around line 1475:

$val_a = $a['tab'].$a['order_edit'];
$val_b = $b['tab'].$b['order_edit'];

with

$val_a = $a['tab'].$a['alignment'].$a['order_edit'];
$val_b = $b['tab'].$b['alignment'].$b['order_edit'];

Wasn't taking the alignment in consideration, not it is. Tested on Sermon admin view and seem to be working great. Of course you have to save the admin view for this to work, since it is implemented on save().

Ok, here's the fix just replace models/admin_view.php around line 1475: ``` $val_a = $a['tab'].$a['order_edit']; $val_b = $b['tab'].$b['order_edit']; ``` with ``` $val_a = $a['tab'].$a['alignment'].$a['order_edit']; $val_b = $b['tab'].$b['alignment'].$b['order_edit']; ``` Wasn't taking the alignment in consideration, not it is. Tested on Sermon admin view and seem to be working great. Of course you have to save the admin view for this to work, since it is implemented on save().
Peeripapo commented 2017-03-03 15:34:33 +00:00 (Migrated from github.com)
$val_a = $a['tab'] . $a['alignment'] . $a['order_edit'];
$val_b = $b['tab'] . $b['alignment'] . $b['order_edit'];

Added spacing, prettier code :)

``` $val_a = $a['tab'] . $a['alignment'] . $a['order_edit']; $val_b = $b['tab'] . $b['alignment'] . $b['order_edit']; ``` Added spacing, prettier code :)
Peeripapo commented 2017-03-03 15:35:16 +00:00 (Migrated from github.com)

You should close the issue after confirming its working ok, duh.

You should close the issue after confirming its working ok, duh.

I've made a little change

$val_a = sprintf('%02u', $a['tab']) . sprintf('%02u', $a['alignment']) . sprintf('%03u', $a['order_edit']);
$val_b = sprintf('%02u', $b['tab']) . sprintf('%02u', $b['alignment']) . sprintf('%03u', $b['order_edit']);

IT works very well now 👍

I've made a little change ``` $val_a = sprintf('%02u', $a['tab']) . sprintf('%02u', $a['alignment']) . sprintf('%03u', $a['order_edit']); $val_b = sprintf('%02u', $b['tab']) . sprintf('%02u', $b['alignment']) . sprintf('%03u', $b['order_edit']); ``` IT works very well now :+1:
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#43
No description provided.