Filter Lookup Value

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
hernan
Posts: 17
Joined: 2020-06-22 18:56

Filter Lookup Value

Post by hernan » 2021-06-02 11:04

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!

pfrumkin
Veteran Member
Posts: 131
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: Filter Lookup Value

Post by pfrumkin » 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

hernan
Posts: 17
Joined: 2020-06-22 18:56

Re: Filter Lookup Value

Post by hernan » 2021-06-07 14:14

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!

User avatar
fbrano
Veteran Member
Posts: 65
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: Filter Lookup Value

Post by fbrano » 2021-06-08 14:09

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();
}
}
}
BF - AppGini Pro V 5.94 rev.1132 Great!
Plugins: Mass Update + Search Page Maker + Summary Reports + Udemy Course
Partizanske, SK

pfrumkin
Veteran Member
Posts: 131
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: Filter Lookup Value

Post by pfrumkin » 2021-06-10 15:19

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;
	}
}

User avatar
fbrano
Veteran Member
Posts: 65
Joined: 2018-03-19 10:39
Location: Slovakia
Contact:

Re: Filter Lookup Value

Post by fbrano » 2021-06-11 06:29

Great advice, it works as needed. Thank you.
BF - AppGini Pro V 5.94 rev.1132 Great!
Plugins: Mass Update + Search Page Maker + Summary Reports + Udemy Course
Partizanske, SK

Post Reply