Adding a default item to TWebDBLookupComboBox's list

This for a client table with listsource for field Agency_Ref pointing to agency table.

  • The field client.Agency_Ref can be left null
  • I have a triggers in client table to make it null if it is 0.

Questions

  1. Is there a way I can insert an item at the top of the list which has a value 0 and the Name "Not an Agency"? (I am just recalling the issue I had with Ref=0 a while ago)
  2. Where would be a good place to do this?
  3. If I cant have it 0, is there a way to have the value null?

On a related note, for a different part of the program, its is not possible to have a placeholder for <select>, the common thing to do is

<select>
  <option value="" disabled selected hidden>Choose an option...</option>
  <option value="option1">Option 1</option>
  <option value="option2">Option 2</option>
</select>

How can add a disabled option value

What kind of tables are this? Is this something server side or client side?
In the client, I would assume you have a TDataset?

I also do not understand your second question.
What kind of placeholder do you look for?
You indicate yourseff in the HTML shown the disabled attribute exists, so, that is what you use to make an item disabled.

Hi Bruno, as the title says we are talking about TWebDBLookupComboBox, which is on the client side. The tables are fetched as Entities through XData.

I cant add the disabled option in the html file, as the TWebDBLookupComboBox overwrites it.

At this moment, there is no built-in support at Pascal class interface for the TWebDBLookupComboBox to set an item as disabled.
For now, you'd need to loop through the child OPTION elements of the WebDBLookupComboBox.ElementHandle at perform a child.SetAtttribute('disabled') for the items you want to set as disabled.

We've added it on the feature request list to allow access of this via the Pascal class.

1 Like

I have created this helper which appears to do the job. For others to use. I am open to improvements, my javascript skills are not hot.

unit PBS.WEB.Data;

interface

uses
  System.SysUtils, WEBLib.DBCtrls;

type
  TWebDBEditHelper = class helper for TWebDBEdit
  private
    function  GetInt : Int64;
    procedure SetInt (AValue : Int64);
    function  GetChar : char;
    procedure SetChar (AValue : char);
  public
    property AsInteger : Int64 read GetInt  write SetInt;
    property AsChar    : char  read GetChar write SetChar;
  end;

  TWebDBLookupHelper = class helper for TWebDBLookupComboBox
  private
    function  GetChar : char;
    procedure SetChar (AValue : char);
  public
    property AsChar    : char  read GetChar write SetChar;
    procedure Add_Disabled_Option (AText     : string  = '';
                                   AValue    : Integer = 0;
                                   ADisabled : Boolean = True;
                                   ASelected : Boolean = True);
  end;



implementation

{ TWebDBEditHelper }

function TWebDBEditHelper.GetInt : Int64;
begin
  TryStrToInt64 (Text, Result);
end;

procedure TWebDBEditHelper.SetInt (AValue : Int64);
begin
  Text := IntToStr (AValue);
end;

function TWebDBEditHelper.GetChar : char;
begin
  if Text = ''
  then Result := ' '
  else Result := Text[1];
end;

procedure TWebDBEditHelper.SetChar (AValue : char);
begin
  Text := AValue;
end;

{ TWebDBLookupHelper }

function TWebDBLookupHelper.GetChar : char;
begin
  if Value = ''
  then Result := ' '
  else Result := Value[1];
end;

procedure TWebDBLookupHelper.SetChar (AValue : char);
begin
  Value := AValue;
end;

procedure TWebDBLookupHelper.Add_Disabled_Option
          (AText     : string = '';
           AValue    : Integer = 0;
           ADisabled : Boolean = True;
           ASelected : Boolean = True);
var
  LText: string;
begin
  if AText = ''
  then LText := 'Please Select'
  else LText := AText;

  asm
    var selectElement = this.GetElementHandle();
    if (selectElement && selectElement.tagName === 'SELECT') {
      var newOption = document.createElement('option');
      newOption.text = LText;
      newOption.value = AValue;
      newOption.disabled = ADisabled;
      newOption.selected = ASelected;

      // Insert at the beginning
      if (selectElement.options.length > 0) {
        selectElement.insertBefore(newOption, selectElement.options[0]);
      } else {
        selectElement.appendChild(newOption);
      }
    }
  end;
end;

end.

Usage examples

Usage examples:

Add_Disabled_Option() - disabled "Please Select" option, auto-selected
Add_Disabled_Option('Choose Item', -1, True, False) - disabled option, not selected
Add_Disabled_Option('Available Option', 1, False, True) - enabled option, selected

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.