FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important
information on the platform change.
02-27-2024 10:08 AM
I've implemented local script which generate table of widgets according to their index.
for (let i = config.count - 1; i >= 0; i--) {
// Clone group config for composite
const myConf = shmi.cloneObject(groupConfig);
// Change the name of the composite, so that they are not all named the same
myConf.name = 'row-' + i;
// Set placeholder values of the composite
myConf.replacers = {
itemIndex: i
};
// Create the composite dynamically
groups.push(shmi.createControl("group", holder.element, myConf, "div", "after"));
}
Now this code insert table dynamicaly after my holder element: list-target container. How to change code to insert table directly to the list-target container?
02-27-2024 11:15 AM
Quick answer: choose the correct widget for "holder". If you post the full code maybe help is easier as exactly the interesting part - how "holder" is defined - has been omitted form the example.
For that using shmi.onReady() is a must, please see the scripting demo for an example on this.
02-27-2024 12:40 PM - edited 02-27-2024 12:41 PM
This is my full code, where `list-target` is a container with specified dimensions:
(function () {
const MODULE_NAME = "jsonList",
ENABLE_LOGGING = false,
RECORD_LOG = false,
logger = shmi.requires("visuals.tools.logging").createLogger(MODULE_NAME, ENABLE_LOGGING, RECORD_LOG),
fLog = logger.fLog,
log = logger.log,
module = shmi.pkg( MODULE_NAME );
// MODULE CODE - START
/* private variables */
/* private functions */
/**
* get the group template id dynamically by the name and category of
* the composite, so you don 't have to enter the grouptemplateid hardcoded
*
* @param {string} name composite name
* @param {string} category composite category
* @returns {string} found grouptemplateID
*/
function getGroupTemplateByName(name, category) {
// Get a reference to the existing group configs
const groupConfig = shmi.requires("shmi.visuals.session.groupConfig"),
iter = shmi.requires("visuals.tools.iterate.iterateObject");
let found = null;
iter(groupConfig, function (group, groupName) {
if (group.label === name && group.category === category) {
found = groupName;
}
});
return found;
}
/**
* Implements local-script run function.
* @param {LocalScript} self instance reference of local-script control
*/
module.run = function (self) {
let groups = [];
const cancelable = shmi.onReady({
controls: {
holder: ".list-target"
}
}, function (resolved) {
// Retrieve reference to widgets and config object from "config.js"
const config = shmi.requires("config").config.list, // gets the config from config.js
holder = resolved.controls.holder,
groupTemplateId = getGroupTemplateByName(config.compositeName, config.compositeCategory),
template = "res://" + groupTemplateId + "/template",
groupConfig = {
"template": template,
"groupId": groupTemplateId,
};
// Creates as many composites as are in "count"
for (let i = 0; i < config.count; i++) {
// Clone group config for composite
const myConf = shmi.cloneObject(groupConfig);
// Change the name of the composite, so that they are not all named the same
myConf.name = 'row-' + i;
// Set placeholder values of the composite
myConf.replacers = {
itemIndex: i
};
// Create the composite dynamically
groups.push(shmi.createControl("group", holder.element, myConf, "div"));
}
});
/* called when this local-script is disabled */
self.onDisable = function () {
self.run = false; /* from original .onDisable function of LocalScript control */
cancelable.cancel()
groups.forEach(function (el) {
shmi.deleteControl(el, true);
});
groups = [];
};
};
// MODULE CODE - END
fLog("module loaded");
})();
02-27-2024 02:12 PM
Please note that holder.element returns the HTML DOMNode, however if you inspect the container HTML element using F12 in your browser you'll find out that there is an additional element - the margin-compensator.
Instead of using the createControl method you should use the widget's own addControl method. This will add it where it belongs.
02-27-2024 04:07 PM
02-29-2024 04:06 PM
A few notes beforehand:
Regarding the issue: when you inspect the generated HTML code you will see that the composite widgets are in there, they are just empty:
I would start from there and see if it works after setting the script up correctly.