Milonic provide full featured pull down web menus for some of the worlds largest companies
click here to see what it can do for you

Download Milonic DHTML Menu
Buy Milonic DHTML Menu

Back To Start Of Archive
Taken From The Forum: Help & Support for DHTML Menu Version 5+
Forum Topic: Click to view post
Last Updated: Saturday July 14 2012 - 06:07:20

{SOLVED} Dynamic menu creation at runtime


Poster: mtd_conseil
Dated: Wednesday December 28 2005 - 20:07:19 GMT

Hi,

I want to create dynamic menus when some event happens.

If I do this:
Code:
<script>
with(milonic=new menuname("test")){
            margin=3;
            style=contextStyle;
            alwaysvisible=true;
            aI("type=draggable;text=test;url=javascript:alert('hello');");
}
drawMenus();
</script>

it works.

But if I do this:
Code:
<script>
function test() {
      with(milonic=new menuname("test")){
            margin=3;
            style=contextStyle;
            alwaysvisible=true;
            aI("type=draggable;text=test;url=javascript:alert('hello');");
      }
}
drawMenus();
</script>
<a href="#" onclick="javascript:test();return false">test</a>

I get errors. But that is exactly what I need.

Any idea? :)


Poster: Ruth
Dated: Saturday December 31 2005 - 2:26:33 GMT

Hi,

Can you maybe give a page with this on it? There doesn't seem to be anyone who can understand from what you posted, maybe if there is a page someone could get the files and figure it out?

The only thing that came to mind was a function Kevin wrote called setTimeOut something about checking menus were loaded or something before running a function. It's in the demos.

http://support.milonic.com/demos/

Ruth


Poster: mtd_conseil
Dated: Saturday December 31 2005 - 5:53:01 GMT

What I say is that you can't use "with(milonic=new menuname(..." method when they are embeded inside a function. It does not seems to be understood or it's something like out of scope.

Let's say you want to create a new menu (not a menu item) when you click on an hyperlink. on an onlick event of the hyperlink, I would call a function where I have a "with(milonic=new menuname(..." method.

I don't have the time at the moment I write this message, but I could write a sample. My example above is the exact sample though.

;)


Poster: mtd_conseil
Dated: Tuesday January 3 2006 - 15:00:52 GMT

here is a sample:

each time you click on the link create menu, it should create a new dragable menu with one item. Not a menu item only.

http://www.virtuelcom.net/sample/index.html


Poster: Andy
Dated: Thursday January 5 2006 - 12:43:25 GMT

I have now created a function that will create a new menu from scratch.

Here are the functions required:



Code:
function _excMD($dta){
_it=$dta.split(":");
_DoP=_it[1]
return _DoP.replace(/;/g,"")
}

function mm_createNewMenus()
{
for(_y=_mcnt;_y<_m.length;_y++)
{
_nM=_d.createElement("div")
_nM.id="menu"+_y
_nM.onmouseout=new Function("startClose()");
_nM.onmouseover=new Function("stopClose("+_y+")");
_nM.onselectstart=new Function("return _f");
if(_dB.appendChild)_dB.appendChild(_nM)
_drawMenu(_y,0)
_nM.className=_cls
_nS=_nM.style
if(_padd)_nS.padding=_excMD(_padd)
if(_ofb)_nS.background=_excMD(_ofb)
if(_brd)_nS.border=_excMD(_brd)
if(_wid)_nS.width=_excMD(_wid)
_nM.style.zindex=499
_nM.style.visibility=_visi
if(_posi)_nS.position=_excMD(_posi)
if(_top)_nS.top=_excMD(_top)
if(_left)_nS.left=_excMD(_left)
if(_bgimg)_nS.backgroundImage="url("+_excMD(_bgimg)+")";
if(_mbgc)_nS.background=_excMD(_mbgc)
_m[_y][23]=0
}
}




And here is how you can call it:



Code:
with(milonic=new menuname("test")){
margin=3;
style=menuStyle;
alwaysvisible=true;
aI("type=draggable;text=test;url=javascript:alert('hello');");
}
mm_createNewMenus()


The docs and a sample are enroute.


Poster: mtd_conseil
Dated: Thursday January 5 2006 - 13:57:24 GMT

Where should I put the function? because it does not seems to work for me.


Best regards,


Poster: Andy
Dated: Wednesday January 11 2006 - 10:20:15 GMT

Here is a new updated version of the above code:


Code:
function _excMD($dta) {
   _it=$dta.split(":");
   _DoP=_it[1]
   return _DoP.replace(/;/g,Z$)
}

function mm_createNewMenus() {
   $r();
   _startM=0
   for(_y=_mcnt;_y<_m.length;_y++) {
      _NM=_m[_y]
      _nM=_d.createElement("div")
      _nM.id="menu"+_y
      _nM.onmouseout=new Function("$I()");
      _nM.onmouseover=new Function("$J("+_y+")");
      _nM.onselectstart=new Function("return _f");
      
      if(_dB.appendChild)
      {
         _dB.appendChild(_nM)
         o$(_y,0)
         _nM.className=_cls
         _nS=_nM.style
         if($1B)_nS.padding=_excMD($1B)
         if(_NM[17])_nS.width=$pX(_NM[17])
         if(_NM[24])_nS.height=$pX(_NM[24])
         if(_ofb)_nS.background=_excMD(_ofb)
         if(p_)_nS.border=_excMD(p_)
         if(_wid)_nS.width=_excMD(_wid)
         _nM.style.zindex=499
         _nM.style.visibility=_visi
         if(n_)_nS.position=_excMD(n_)
         if($k)_nS.top=_excMD($k)
         if($l)_nS.left=_excMD($l)
         if(_bgimg)_nS.backgroundImage="url("+_excMD(_bgimg)+")";
         //if(_mbgc)_nS.background=_excMD(_mbgc)
         _NM[23]=0
      }
   }
}


Poster: mtd_conseil
Dated: Monday January 16 2006 - 18:40:49 GMT

Wonderful... It works like a charm!

Thank you very much!

Kind regards,

Michel


Poster: erickmiller
Dated: Thursday January 19 2006 - 6:08:49 GMT

Just checking to see if there were complete examples of this yet? I haven't been able to make this code work.

Thanks!

Eric


Poster: mtd_conseil
Dated: Thursday January 19 2006 - 19:32:22 GMT

Well, actually, not from me.

Our actual project is on a private intranet for now.
Menus will be created from AJAX-like commands.
For example, to create user menus ater the user has login, without reloading the page.

If you are not using the dragable stuff, just remove the type=draggable and be certain to have the latest version 5.740 or later.

Regards,


Poster: erickmiller
Dated: Friday January 27 2006 - 2:54:35 GMT

That's exactly what we're doing! :) I have a system with draggable objects that I'd like to have contextual menus show up on the objects based on what is in a database. I'm using AJAX to pull the information to the browser on-the-fly when a right-click occurs on an object. I'd like to integrate the contextual menus into this so they have updated information based on the changes made in the database.

I was wondering if Andy had any examples using the code shown in this post. You must have gotten it to work. I created a relatively simple example and couldn't get a new menu created. I haven't had time to come back to it until now, but I'll see what I can make work. If I can come up with a simple example, I'll post it.


Poster: Andy
Dated: Monday January 30 2006 - 12:46:39 GMT

Hi,

There is a demo of this at http://www.milonic.com/createmenu.htm

Note that you will need version 5.743 (Pre Release) along with the updated mm_menueditapi.js file from http://www.milonic.com/menuversions/mm_menueditapi.js

Hope this helps,
Andy


Poster: JasperJames
Dated: Tuesday January 31 2006 - 20:02:52 GMT

Could you show an example of inserting a dynamically created menu into an existing menu?
Is it possible to substitute a new menu for an existing menu item?

Also, I've been trying to use mm_insertItem method and haven't had much luck. I get a bunch of '_gm has no properties' errors. deleteItem and editItem do seem to work.

Thanks for your help,

Jasper


Poster: gelid
Dated: Monday February 6 2006 - 13:48:21 GMT

JasperJames wrote:
Could you show an example of inserting a dynamically created menu into an existing menu?
Is it possible to substitute a new menu for an existing menu item?

Also, I've been trying to use mm_insertItem method and haven't had much luck. I get a bunch of '_gm has no properties' errors. deleteItem and editItem do seem to work.

Thanks for your help,

Jasper

I am also trying to use the mm_insertItem without any luck. Are there any samples of this anywhere? Any help would be appreciated.


Poster: Andy
Dated: Monday February 27 2006 - 15:41:04 GMT

Hi,

There is a sample at http://www.milonic.com/menueditapi.php

Hope this helps,
Andy


Poster: mtd_conseil
Dated: Monday February 27 2006 - 15:47:08 GMT

hi Andy,

You changed completely the concept, or the other method is also still working ? mm_createNewMenus();

Michel


Poster: Andy
Dated: Monday February 27 2006 - 15:53:10 GMT

It should be backward compatible.

mm_createNewMenus(); is a new function so wont have any effect on previous versions.


Poster: mtd_conseil
Dated: Monday February 27 2006 - 15:56:08 GMT

ok,

I was not aware through out all the tests we made together of those new functions in the last demo you placed above.

They look easier to use then the previous way we made it.

At least we will have the time reprogramme our page.

thank you,

Best regards,

Michel