Page 1 of 1

Filter Lookup Value

Posted: 2021-06-02 11:04
by hernan
Hi all!

I'm trying to filter a lookup field according to its own value...
I'll try to explain:
- There's a table called "Projects".
- And a field in that table called "type", that is a lookup from another table: "project_types" with all the available options.

So, the logic is this:
-> when the user creates a new Project, the only option available in "Type" should be: "Study"
-> when the user modifies an existing project, the options should vary depending on the current "type"...
---> if the type is currently "Study", the options should be "Study", "Lost", "Standby", "Won".
---> if the type is currently "Won", the options should be "Finished", "Closed".
and other combinations but with the same logic.
Also some users, in Admin groups should see an "Admin" type.

But I couldn't manage to find a way of modifying the Dropdown options... I understand it's a Select2 and that the options are loaded from an Ajax call to ajax_combo.php ....
Where and when could I change the options shown to the user depending on the logic explained?

Does anybody had a similar issue or could throw some light?

I very much appreciate your help!

Thank you!

Re: Filter Lookup Value

Posted: 2021-06-03 18:58
by pfrumkin
Hi,

This is a klunky way to do it but I didn't have any other ways. I put this code in my tablename-dv.js file.

Code: Select all

	var status = $j("#Status option:selected" ).text();
	if((status == "Donation Pending") || (status == "Rejected") || (status == "Canceled")) {
		// Hide the Submitted option, can't get there from here
		for (var i = 0; i < $j('select#Status option').length; i++) {
			if ($j('select#Status option')[i].text == "Submitted") {
				$j('select#Status option')[i].remove();
			}
		}	
	}
Status is my lookup value. If the value is one of the list, then I iterate through the list to remove the option I want to hide. You could add such code blocks for each (in my case) Status option.

~Paul

Re: Filter Lookup Value

Posted: 2021-06-07 14:14
by hernan
pfrumkin wrote:
2021-06-03 18:58
Hi,

This is a klunky way to do it but I didn't have any other ways. I put this code in my tablename-dv.js file.

Code: Select all

	var status = $j("#Status option:selected" ).text();
	if((status == "Donation Pending") || (status == "Rejected") || (status == "Canceled")) {
		// Hide the Submitted option, can't get there from here
		for (var i = 0; i < $j('select#Status option').length; i++) {
			if ($j('select#Status option')[i].text == "Submitted") {
				$j('select#Status option')[i].remove();
			}
		}	
	}
Status is my lookup value. If the value is one of the list, then I iterate through the list to remove the option I want to hide. You could add such code blocks for each (in my case) Status option.

~Paul
Thank you Paul.
I'll try your suggestion!

Re: Filter Lookup Value

Posted: 2021-06-08 14:09
by fbrano
Great, I wonder how to implement also check for user's group into condition wtith status of lookup field. Thank you in advance


var stav = $j("#Stav_pristupu option:selected" ).text();
if((stav == "ziadany")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "aktivny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "odoberany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "blokovany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zruseny") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}
if((stav == "schvaleny")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "ziadany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zamietnuty") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "odoberany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "blokovany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zruseny") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}
if((stav == "zamietnuty")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "ziadany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "schvaleny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "aktivny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "odoberany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "blokovany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zruseny") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}
if((stav == "aktivny")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "ziadany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "schvaleny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zamietnuty") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}
if((stav == "odoberany")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "ziadany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "schvaleny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zamietnuty") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "aktivny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "blokovany") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}
if((stav == "zruseny")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "ziadany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "schvaleny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zamietnuty") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "aktivny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "odoberany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "blokovany") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}
if((stav == "blokovany")) {
for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
if ($j('select#Stav_pristupu option')[i].text == "ziadany") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "schvaleny") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "zamietnuty") {
$j('select#Stav_pristupu option')[i].remove();
}
if ($j('select#Stav_pristupu option')[i].text == "odoberany") {
$j('select#Stav_pristupu option')[i].remove();
}
}
}

Re: Filter Lookup Value

Posted: 2021-06-10 15:19
by pfrumkin
I think the problem is that the user part is server side and we're handling the dropdown on the client.

A way could be to implement in the tablename_dv hook. You can have different blocks in the hook for each user type, something like (not tested)

Code: Select all

function tablename_dv($selectedID, $memberInfo, &$html, &$args)
{
	$memberInfo = getMemberInfo();
	if ($memberInfo['group'] == 'ziadany') {
		ob_start();
?>
		<script>
  		   $j(function() {
			for (var i = 0; i < $j('select#Stav_pristupu option').length; i++) {
				if ($j('select#Stav_pristupu option')[i].text == "aktivny") {
					$j('select#Stav_pristupu option')[i].remove();
				}
				if ($j('select#Stav_pristupu option')[i].text == "odoberany") {
					$j('select#Stav_pristupu option')[i].remove();
				}
				if ($j('select#Stav_pristupu option')[i].text == "blokovany") {
					$j('select#Stav_pristupu option')[i].remove();
				}
				if ($j('select#Stav_pristupu option')[i].text == "zruseny") {
					$j('select#Stav_pristupu option')[i].remove();
				}
			}
		  });
		</script>
<?php
		$new_layout = ob_get_contents();
		ob_end_clean();
		$html .= $new_layout;
	}
}

Re: Filter Lookup Value

Posted: 2021-06-11 06:29
by fbrano
Great advice, it works as needed. Thank you.