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!
Filter Lookup Value
Re: Filter Lookup Value
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.
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
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();
}
}
}
~Paul
Re: Filter Lookup Value
Thank you Paul.pfrumkin wrote: ↑2021-06-03 18:58Hi,
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.
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.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(); } } }
~Paul
I'll try your suggestion!
Re: Filter Lookup Value
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();
}
}
}
ver 23.15 1484
Re: Filter Lookup Value
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)
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;
}
}