# Dialogs and Dynamic

**URL:** <https://community.wolfram.com/t/dialogs-and-dynamic/1080>\
**Category:** Questions\
**Tags:** mathematica, interface-construction\
**Created:** [December 10, 2013, 7:57pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080 "2013-12-10T19:57:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![coreymathers](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@coreymathers](https://community.wolfram.com/u/coreymathers)\
**Post date:** [December 10, 2013, 7:57pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080/1 "2013-12-10T19:57:39Z")

</div>

Initialize global list of {string, number} pairs

```auto
main = {{"apple", 1}, {"banana", 2}, {"carrot", 3},{"donut", 4}

```

Function to edit item number j from the list

```auto
EditItem[j_] := Module[{tx, nm},

```

```auto
  tx = main[[j]][[1]];

```

```auto
  nm = main[[j]][[2]];

```

```auto
  CreateDialog[{

```

```auto
    Panel[

```

```auto
     Column[{

```

```auto
       InputField[Dynamic[tx], String],

```

```auto
       InputField[Dynamic[nm], Number],

```

```auto
       Row[{

```

```auto
         CancelButton[],

```

```auto
         DefaultButton[

```

```auto
          "Save",

```

```auto
          main[[j]][[1]] = tx;

```

```auto
          main[[j]][[2]] = nm;

```

```auto
          DialogReturn[]

```

```auto
          ]

```

```auto
         }]

```

```auto
       }]

```

```auto
     ]

```

```auto
    }, Modal -> True]

```

```auto
  ]

```

If I call the edit function directly, it works fine.

```auto
EditItem[4]

```

```auto
NotebookObject[FrontEndObject[LinkObject["tp8_shm", 3, 1]], 110]

```

```auto
main

```

```auto
{{"apple", 1}, {"banana", 2}, {"carrot", 3}, {"dingbat", 5}}

```

If I call the edit function from the buttons in this dynamic table, it doesn’t work. The fields are not pre-populated and the assignments fail.

```auto
In[11]:= Dynamic[

```

```auto
 Panel[

```

```auto
  Grid[

```

```auto
   Table[{Button[main[[j]][[1]], EditItem[j]], main[[j]][[2]]}, {j, 1, Length[main]}]

```

```auto
   ]

```

```auto
  ]

```

```auto
 ]

```

Why doesn’t the function call work from inside the buttons?

---

<div class="post-metadata">

**Author:** ![coreymathers](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@coreymathers](https://community.wolfram.com/u/coreymathers)\
**Post date:** [December 10, 2013, 7:58pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080/2 "2013-12-10T19:58:54Z")

</div>

I’ve been using Mathematica for 20+ years, but only recently tried to understand Dynamic and UI features.

---

<div class="post-metadata">

**Author:** ![arielsepulveda](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arielsepulveda/32/33893_2.png) [@arielsepulveda](https://community.wolfram.com/u/arielsepulveda)\
**Post date:** [December 11, 2013, 2:12pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080/3 "2013-12-11T14:12:48Z")

</div>

Dynamics can be very tricky. I think that the following changes make your function work the way you want.  
Main changes to note:

- Use of DynamicModule instead of Module
- Use With inside Table

main = {{“apple”, 1}, {“banana”, 2}, {“carrot”, 3}, {“donut”, 4}};  
EditItem := DynamicModule[{tx, nm},  
tx = main[][[1]];  
nm = main[][[2]];  
CreateDialog[{Panel[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Column[{InputField[Dynamic, String],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputField[Dynamic, Number],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Row[{CancelButton[], DefaultButton[“Save”, main[][[1]] = tx;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main[][[2]] = nm;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DialogReturn[]]}]}]]}, Modal → True]  
];

Panel[Grid[Table[  
With[{j = j},  
&nbsp;&nbsp;&nbsp;&nbsp;{Button[main[][[1]], EditItem],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dynamic[main[][[2]]]}], {j, 1, Length}]]]

---

<div class="post-metadata">

**Author:** ![coreymathers](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@coreymathers](https://community.wolfram.com/u/coreymathers)\
**Post date:** [December 11, 2013, 4:23pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080/4 "2013-12-11T16:23:15Z")

</div>

Perfect! Thank you. I’ve run into the need for With before but I still have no instinct for why it’s necessary sometimes.

---

<div class="post-metadata">

**Author:** ![arielsepulveda](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arielsepulveda/32/33893_2.png) [@arielsepulveda](https://community.wolfram.com/u/arielsepulveda)\
**Post date:** [December 11, 2013, 4:59pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080/5 "2013-12-11T16:59:55Z")

</div>

The With basically converts the i index into a number instead of a variable. That way your app is making reference to i as a number instead of a local variable which would be unrecognizable after you use it the first time. This happens because indexes in Table are indeed local variables to the Table. These local variables disapear after the Table is used, thus, without the With your Dynamic app would be making reference to an unexisting variable.

---

<div class="post-metadata">

**Author:** ![coreymathers](https://avatars.discourse-cdn.com/v4/letter/c/9d8465/32.png) [@coreymathers](https://community.wolfram.com/u/coreymathers)\
**Post date:** [December 11, 2013, 5:22pm UTC](https://community.wolfram.com/t/dialogs-and-dynamic/1080/6 "2013-12-11T17:22:09Z")

</div>

After your comment I went back and re-read the “A Good Trick to Know” bit in the Intro to Dynamic help for the 100th time, but this time it made sense. Thanks!
