May you please help me with the next concern, what I'm doing is to convert the cells from a Power Table into dropdown lists to show the materials available by using the event configureEditor():, all the information the script I used is the following:
if colName == 'Material':
# Consulta SQL para obtener los materiales
#Use SQL Query and get the materials' list
query = """
SELECT DISTINCT Material
FROM Almacen_Indirectos_Catalogo_ZM
WHERE Material IS NOT NULL
"""
try:
# Ejecutar la consulta
#Executes the query
resultados = system.db.runQuery(query)
# Imprimir los resultados del query para depuración
#Printing the result for debugging
print "Resultados del query:", resultados
# Convertir los resultados en una lista de opciones (solo los valores de 'Material')
#The options from lists are printed(just the values from 'Material'column
options = [fila["Material"] for fila in resultados]
# Imprimir algunas opciones generadas para el dropdown para depuración
#Printing dropdown options for debugging
print "Opciones generadas para el dropdown:", options[:10] # Imprime las primeras 10 opciones
#Print the first ten options
# Retornar las opciones del dropdown
#Returning the dropdown options
return {'options': options}
except Exception, e:
# Manejo de errores con mensaje en la consola
#Error handling by showing issues in the message box of console
print "Error ejecutando la consulta:", str(e)
# En caso de error o sin resultados, retornar lista vacía
#If options are not found, a empty list will be returned
return {'options': []}
By the way, in the process debugging as well as running the query on SQL, the elements are shown correctly so I do not know if the issue is in the options list
I used a query to show all the elements because the list contains 1876 elements, I checked the query on Microsoft SQL Server Management Studio and eveything seems to be fine
Running a query is a very fragile operation that is subject to a minimum of four IPC calls, each almost always over the network:
Your client/designer -> your gateway
Your gateway -> your DB
Your DB -> your gateway (with the results)
Your gateway -> the client/designer
Because you're in the configureEditor extension function, that query operation is running on the primary GUI painting thread (known as the 'event dispatch thread' or EDT). If any of those operations take longer than a few hundred milliseconds (and inevitably they will), users will report that your window is laggy, get frustrated, and start to hate your project/Ignition/you personally . They also will not understand why.
What you need to do is run your query in a binding, in an additional custom property on the table, where it will automatically run asynchronously. Then use scripting to refer to that table to bring in your options list.
Understood, Paul, and thank you for your fast quick response, I just created a custom property directly in the Power Table, which datatype is Dataset and is named MaterialOptions which is bound to the query
SELECT DISTINCT Material
FROM Almacen_Indirectos_Catalogo_ZM
WHERE Material IS NOT NULL
How do I call this custom property within the script? Is correct to use a dataset type for the binding or should I use another one?
You just need to 'map' the dataset into the list-of-tuples format the options property expects. With a list comprehension it's pretty simple:
if colName == 'Material':
# Fix this name to be correct; use the property selector in the script area to help (the chain icon)
ds = event.source.nameOfYourDatasetCustomProperty
# For each row in the dataset, repeat the one column's value into a two-item tuple (required by the power table)
options = [
(ds.getValueAt(row, 0), ds.getValueAt(row, 0))
for row in xrange(ds.rowCount)
]
return {
"options": options
}
The dropdown shows the list of materials I needed, thank you so much for your help. I have 2 more questions(if i need to create another post, please let me know) I am planning to create another table for materials approvals requested in the warehouse for different areas, how do I maintain the text inside every cell and then send it to the other table, the .text will still be working? and the second one, I'm converting the cells from another column into dropdown ones, is there a way to connect the cells matching per position, by this I mean, I have a list of materials but everyone matches with a ZM code, I'll use another custom property bound to a query so every option selected from the "Material" column will change automatically the cell from column "Codificacion ZM" according to match.
I'm not quite sure what you're asking with the first question, but:
If you have two components on the same window they can absolutely refer to each other's properties (though at scale this can be an antipattern; it often makes sense to make additional custom properties on the root container and bind bidirectionally to that central property from both components as needed).
If you have two components on different windows, then you need to obtain a reference to the actual window object (e.g. system.gui.getWindow), or use a different strategy to share the data (e.g. client tags) or one of a few other possible options, but the exact best approach will vary a bit.
For your second question, if I'm understanding correctly, take a look at this post/project; I think it covers what you're asking for:
To be more specific with my first question and second question, let's say all the data and details from the table will be registered per row, basically one item selected from the first row in column "Material" will cause a value that matches the first row in the column "ZM"
row, in the following example, imagine I selected one item, in this case, the wooden box so I just chose the option from the dropdown list.
Let's say I've selected the big wooden box from the dropdown just generated, when the item is selected I expect the ZM code ZM0140289 appearing in the column "Codificacion ZM' automatically. I don't know if it's better to use an empty container to store the details by using a button component to register all the line or if there is way of registering all the rows in another table.
I found that to change the values from a Power Table it is better to use the event onCellEdited():
My main objective for this application is to stop using paper for material's request submissions, so when a value is selected from the dropdown cell, the cell's value from column ZM should be change and then I will use another table or power table to receive the information and wait for approvals from supervisors.