Page 1 of 1

Create Grid using newElement

Posted: Thu Mar 23, 2017 11:17 am
by pjshuey
Is it possible to create a grid using the newElement API?

Re: Create Grid using newElement

Posted: Thu Mar 23, 2017 1:58 pm
by matt.denninghoff
It is not possible to create a grid this way. The grid is a collection of dozens to hundreds of Document Object Model (DOM) elements.
newElement() creates an HTML DOM element and attaches it to the Rich Display or Genie "container", which is just an element in the DOM tree.

Re: Create Grid using newElement

Posted: Thu Mar 23, 2017 3:13 pm
by pjshuey
Would it be crazy to try to create a custom widget for a grid but do it from scratch rather than based off of another grid widget?

Re: Create Grid using newElement

Posted: Fri Mar 24, 2017 8:27 am
by pjshuey
What I would really like to do is to put a new grid on the screen on conversion from green screen to rich display. Is that possible?

Re: Create Grid using newElement

Posted: Fri Mar 24, 2017 2:57 pm
by Glenn
It may be possible.

Can you tell us what you are trying to do? It may be simpler/cleaner to use a different widget to accomplish what you are trying to do.

Glenn

Re: Create Grid using newElement

Posted: Fri Mar 24, 2017 3:07 pm
by pjshuey
We are trying to show the environment (current library) that the user is in on the screen. I created a custom widget that is based on the grid widget and set the defaults on the widget so it would be database driven and query QSYS2/LIBLIST to get the library list..... We can drag this widget onto the designer screen and it works great. I would like that to be created on conversion so we don't have to remember to add it on each screen we convert. There may be a much better way to do this, but this is what I came up with. Do you have any ideas?

Re: Create Grid using newElement

Posted: Fri Mar 24, 2017 3:32 pm
by Glenn
I just did a quick test and the following code in the "add enhancements" hook of the conversion theme seems to work (I didn't compile or run the screen). Note that you will need to come up with some way to get a unique value for both the "id" and "record format name" properties. Perhaps a combination of the record format name and "SFL" (must be 10 characters or less)?

Code: Select all

    var envGrid = new Object;
    envGrid["id"] = ""; // should be unique at the display file level - can be the same as the "record format name" below
    envGrid["record format name"] = ""; // MUST be unique at the display file level.
    envGrid["field type"] = "grid";
    // Repeat the envGrid[] statement for each property you want set
    format.items.push(envGrid);
Glenn

Re: Create Grid using newElement

Posted: Fri Mar 24, 2017 3:40 pm
by pjshuey
Thanks so much Glenn! I knew there had to be a way!

Re: Create Grid using newElement

Posted: Mon Mar 27, 2017 4:55 pm
by Scott Klement
Hmmm... well, it actually _is_ possible to create a grid with "newElement". But, you would not be able to create a database-driven grid because of security concerns.

Its important to understand that newElement() is designed to create screen elements "on-the-fly" from JavaScript. So it does not create the element when your conversion script runs, but instead, creates the element when your screen is displayed. The "add enhancements" feature that Glenn described is the proper way to add elements during the conversion script.

newElement() creates the elements when the screen is displayed to the user, and it is possible to create a grid at that time. But database-driven data cannot be supplied because we don't want users to be able to use the JavaScript console to run any arbitrary SQL statement at any time. That would be a big security problem if people could do that... and if you could create a grid on-the-fly with database-driven properties, it would indeed allow them to run any SQL statement from the console.

So SQL statements for database-driven grids MUST be coded in the visual designer and saved (...and compiled) into displays on your IBM i. Otherwise the database-driven properties will be ignored.

I won't take the time to show you how to do it, here... since Glenn's solution will work much better for you. ;-) But, I wanted readers of this forum to know that it is, indeed, possible to create grids on-the-fly as long as they are not database-driven.