Lookup with vchar and separator from Parent record

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
qdrddr
User
Posts: 1

Lookup with vchar and separator from Parent record

Post by qdrddr »

With phpmaker 2024

I have a table called Deployed_apps that has a vchar field "pods_list", each record of that table contains its own comma-separated list of values, for example, odoo, filebrowser, postgresql, also Deployed_apps table has an id field.

| id | pods_list                     |
|----|-------------------------------|
| 19 | odoo, filebrowser, postgresql |
| 20 | postgresql, keycloack         |

I have a second table called DEnvs with the field "key"; also, DEnvs has a deployed_app field. Both are in Master-Detail relations; the Deployed_apps table is the Master, and the DEnvs is the Detail table; they are connected as follows: one Deployed_apps.id to many DEnvs.deployed_app. I am dynamically pulling in the DEnvs.key field value from the master table using this filter
(strval($this->deployed_app->CurrentValue) != "") ? "id = " . $this->deployed_app->CurrentValue : ""
The filter works and is pulling value from the parent record, so the DEnvs.key in this example, when pressing the dropdown modal dialog, shows in the list only one item, odoo, filebrowser, postgresql.

In the PHPMaker, in the Tables left section, I selected table DENvs and selected the tab "Code" for the table in the right section. And on the Code tab, in the Client Scripts > Table Specific > Add/Copy Page > Startup Script, I want to create a custom Ajax to parse values displayed in the lookup field DENvs.key so in the example "odoo, filebrowser, postgresql" should split those by comma separator ", " so the dropdown list would display instead of one item, 3 items odoo, filebrowser, postgresql instead of one odoo, filebrowser, postgresql. So when I select only one, say I selected the odoo value and save the DENvs record, I would end up with a table like this:

| id | key        | deployed_app |
|----|------------|--------------|
| 55 | odoo       | 19           |

The field that needs to be parsed is in the Deployed_apps.pods_list. And that should be displayed in the dropdown menu with split items when you press on the DEnvs.key field with modal dialog lookup enabled.

I was trying this javascript, but it doesn't work as expected. The modal lookup dialog doesn't split my item into 3 items as I want it


//In the PHPMaker Tables left section, I selected table DENvs and selected in the right section Tab "Code" for the table. And on the Code tab, in the Client Scripts > Table Specific > Add/Copy Page > Startup Script
// Custom function to fetch and populate key lookup field with individual items from Deployed_apps.pods_list using the built-in API
currentTable.key.populateLookupFromPodList = function() {
    var deployedAppId = this.getParentRow().deployed_app;
    var apiUrl = ew.API_URL + "/Deployed_apps/data/" + deployedAppId;
    return new Promise(function(resolve, reject) {
      ew.ajax(apiUrl, null, function(response) {
        var deployedAppData = JSON.parse(response);
        var keyValues = deployedAppData.pods_list.split(', ');
        // Transform keyValues into an array of objects with id and text properties
        var data = keyValues.map(function(value, index) {
          return { id: index, text: value };
        });
        currentTable.key.lookupOptions.data = data;
        // Update the select2 dropdown with the new data
        $('#x__key').select2({ data: data });
        resolve();
      });
    });
  }

// Trigger the function when the page loads or when the deployed_app value changes
loadjs.ready("head", function() {
  currentTable.key.populateLookupFromPodList().then(function() {
    // Open the modal dialog here
  });
  currentTable.deployed_app.onChange = function() {
    currentTable.key.populateLookupFromPodList().then(function() {
      // Open the modal dialog here
    });
  };
});

// Custom function to render the key lookup field
currentTable.key.customRender = function(value, rowData) {
  var option = this.options[this.selectedIndex];
  if (option) {
    return option.text;
  }
  return value;
}

// Custom function to get the selected key value
currentTable.key.getSelectedValue = function() {
  var option = this.options[this.selectedIndex];
  return option ? option.value : '';
}

// Override the default lookup value setting
currentTable.key.setLookupValue = function(value) {
  var selectedValue = null;
  for (var i = 0; i < this.lookupOptions.data.length; i++) {
    if (this.lookupOptions.data[i] === value) {
      selectedValue = i;
      break;
    }
  }
  this.selectedIndex = selectedValue !== null ? selectedValue : -1;
}

mobhar
User
Posts: 11727

Post by mobhar »

qdrddr wrote:

I was trying this javascript, but it doesn't work as expected. The modal lookup dialog doesn't split my item into 3 items as I want it

Press F12 from your browser, and check whether any Javascript error message from Console section.


Post Reply