Page 1 of 1

Modal lookup Select Disable Values

Posted: Sat Sep 14, 2024 7:56 pm
by mpol_ch

Hi,
I am using a modal dialog lookup with select.
The lookup fields are:
Display field #1: ks_stelle
Display field #2: ks_la
Display field #3: Jahr
Display field #4: ks_gueltig

The database values of ks_gueltig are 0 or 1.
The shown values are No for 0 and Yes for 1...

I am trying to disable the options with ks_gueltig == 0.
I should show all options but the options with ks_gueltig == 0 should NOT be selectable.
I used the following code in "startup script" but have no success.
Any idea where to start?

Startup Script

    $(document).on("shown.bs.modal", function(event) {
        var modal = $(event.target); // Get the modal that was just shown
    
        // Check if it's the modal lookup (based on modal title or other unique properties)
        if (modal.find(".modal-title:contains('Lookup')").length) {
    
            // Target the select dropdown inside the modal (adjust #your_field_id to your field's actual ID)
            var select = modal.find("select#x_Kostenstelle");
    
            // Loop through each option in the select dropdown
            select.find("option").each(function() {
                var option = $(this);
                var ks_gueltig_value = option.data("ks_gueltig"); // Assuming ks_gueltig value is passed as data attribute
    
                // Disable option if ks_gueltig is 0
                //if (ks_gueltig_value == 'No') {
                    option.prop("disabled", true); // Disable the option
                    option.css("color", "#999"); // Change color to indicate it's disabled
                //}
            });
        }
});

mpol_ch


Re: Modal lookup Select Disable Values

Posted: Sat Sep 14, 2024 10:20 pm
by arbei

The "shown.bs.modal" is for the modal dialog, the options of the lookup field are not updated yet. You may search "updatedone" in this forum. Also see Client Script -> Example 3.


Re: Modal lookup Select Disable Values

Posted: Sun Sep 15, 2024 12:27 am
by mpol_ch

The below code is also not working:

 $(document).on("updatedone", function(e, args) {
     var $target = $(args.target);
 
     // Check if the lookup field is the one you're interested in (replace "MyLookupField" with your actual field name)
     if ($target.data("field") == "x_MyLookupField") { // Replace with your field name
 
         // Loop through each row in the lookup table
         $target.closest("table").find("tbody tr").each(function() {
             var $row = $(this);

             // Get the value of ks_gueltig from the row (replace 'ks_gueltig' with the actual data-name)
             var ks_gueltig_value = $row.find('td[data-name="ks_gueltig"]').text().trim();
 
             // If ks_gueltig == 0, disable the select or mark as non-selectable
             if (ks_gueltig_value === "0") {
                 $row.css("color", "#999"); // Change color to indicate it's disabled
      
                 // Disable select element or prevent interaction
                 $row.find("select").attr("disabled", true); // Disable the select dropdown element in this row
             }
         });
     }
 });

mpol_ch


Re: Modal lookup Select Disable Values

Posted: Sun Sep 15, 2024 10:16 am
by arbei

mpol_ch wrote:

$target.closest("table")

Are you sure this is correct?


Re: Modal lookup Select Disable Values

Posted: Sun Sep 15, 2024 6:36 pm
by mpol_ch

Hi,
I just checked again with the following code.
It still show me all the options.
The aim is that it shows all the options but the options (select rows) with ks_gueltig ==0 should not be selectable.

The source table called laboratory_equitment.
The lookup field of source table is: Kostenstelle
Edit Tag:
Select
Use lookup table: checked
Use modal dialog for lookup: checked

Lookup Table
Table name: kostenstelle
Link field: ksid
Display field #1: ks_stelle
Display field #2: ks_leitzahl
Display field #3: Jahr
Display field #4: ks_gueltig

           $(document).on("updatedone", function(e, args) {
           var $target = $(args.target);
      // Check if the lookup field is the one you're interested in (replace "MyLookupField" with your actual field name)
      if ($target.data("field") == "x_Kostenstelle") { // Replace with your field name
  
          // Loop through each row in the lookup table
          $target.closest("kostenstelle").find("tbody tr").each(function() {
              var $row = $(this);
 
              // Get the value of ks_gueltig from the row (replace 'ks_gueltig' with the actual data-name)
              var ks_gueltig_value = $row.find('td[data-name="ks_gueltig"]').text().trim();
  
              // If ks_gueltig == 0, disable the select or mark as non-selectable
              if (ks_gueltig_value === "0") {
                  $row.css("color", "#999"); // Change color to indicate it's disabled
       
                  // Disable select element or prevent interaction
                  $row.find("select").attr("disabled", true); // Disable the select dropdown element in this row
              }
          });
      }
  });

mpol_ch


Re: Modal lookup Select Disable Values

Posted: Sun Sep 15, 2024 6:37 pm
by mpol_ch

The code is inserted into
Client Scripts / Global / Page with header/footer / Client Script


Re: Modal lookup Select Disable Values

Posted: Sun Sep 15, 2024 9:11 pm
by arbei

You better debug your code by, e.g. console.log($target.closest("kostenstelle")) and see if you can find or what you found is really want to want to find.


Re: Modal lookup Select Disable Values

Posted: Mon Sep 16, 2024 5:54 pm
by mpol_ch

Hi,
I solved my case with following code by inserting it into
Edit Tag /Option template

<option {{if df4 == 'No'}}disabled style="color: red; font-weight: bold"{{else}}style="color: green; font-weight: bold"{{/if}}>
    {{:df1}} [{{:df2}}] [{{:df3}] {{:df4}}
</option>

Thanks for your input
mpol_ch