Start a conversation

Creating a dynamic menu

On this article we'll see how to create a menu with dynamic itens, saved on a table.

1 - Creating the table:

  • The first step is create the table that will save the values of menu itens, for this example we're using a MySQL database. Check out the table structure below:

TABLE: dynamic_menu
FIELDS: id, master_item, description, link, item_number.

Ps: The field "master_item" should accept null values.

2 - Creating the project and the form to insert itens:

  • On Scriptcase, create a project and connect it with the database, where the table dynamic_menu is there.

  • Now create a form application of type Multiple rows, onthe table dynamic_menu.

3 - Configurating the form:

  • Go to field master_item on settings and choose "Select" on option "Data Type":

  • On the option "Lookup Settings -> Use title" choose "Yes" and put a title on the input beside, like the image below:

  • Still on the block "Lookup Settings" use the following select statement, on the option "SQL Select Statement":

SELECT id, description
FROM dynamic_menu
WHERE id <> {id}
ORDER BY description

Ps: This field will be used to create menu sub-itens.

  • Then, create an ajax event, onChange, on the field master_item and insert the following code:

{numero_item} = {item_mestre};

  • Use also the event onLoadRecord, of the form, to fill the field master_item automatically, like the image:

SOURCE CODE:

$sql = "SELECT master_item, description FROM dynamic_menu WHERE id = {id}";
sc_lookup(ds, $sql);

if(empty({ds[0][0]}))
{
    {item_number} = 0;
}

  • To finish the form creation, create a javascript method, by the menu "Programming -> JavaScript Methods", with the name reload. This method will be responsible to reload the menu application always that was inserted, updated and deleted some item/sub-item of the menu. Use the following code on the method:

sc_ajax_javascript("reload");

4 - Creating the menu application:

  • Create the application with the name "app_menu" and then add the following code in the event onLoad:

SOURCE CODE:

sc_appmenu_reset("app_menu");
sc_appmenu_create("app_menu");

$sql = "SELECT id, master_item, description, link FROM dynamic_menu ORDER BY master_item, item_number";
sc_lookup(ds, $sql);

foreach({ds} as $arr_menu_item)
{
    $menu_item = "item_".$arr_menu_item[0];
   
    if($arr_menu_item[1] == 0)
    {
        $master_id = "";
    }else{
        $master_id = "item_".$arr_menu_item[1];
    }
   
    sc_appmenu_add_item("app_menu", $menu_item, $master_id, $arr_menu_item[2], $arr_menu_item[3], "", "", "", "");
}

  • Choose a connection to the menu, by the option "Application -> Settings -> Connection"

5 - Running:

  • Run the form and insert the itens that you want:

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Caio Guerra (Migrated deleted Agent)

  2. Posted
  3. Updated

Comments