PDA

View Full Version : [CLOSED] Dynamically adding



cslocus
Oct 24, 2019, 9:50 AM
Hi!

I'm trying to dynamically add items to a panel using my controller (it needs to happen when I push a button).

I've got it adding to the panel in the code, but it doesn't appear on screen once the result is returned.

Here's some code:



public ActionResult LoadStyles()
{

List<vw_ClothingStyles> styles = (from x in db.vw_ClothingStyles
select x).ToList();

Panel mainStylesPanel = X.GetCmp<Panel>("StyleRowPanel");

foreach (vw_ClothingStyles style in styles)
{

Panel panel = new Panel()
{
FactoryAlias = "StyleCardPanel"
};


mainStylesPanel.Items.Add(panel);

}

mainStylesPanel.DoAutoRender();

return this.Direct();
}


I can also give you the code for my main panel if needed :)

fabricio.murta
Oct 24, 2019, 5:04 PM
Hello @cslocus!

Please refer to this example for proper dynamic components adding:
- XRender > Basic > Add Items (https://mvc5.ext.net/#/XRender_Basic/Add_Items/)

You may also be interested in dynamic partial rendering, a technique that scales up the complexity of the code, yet helps in reusage:
- Dynamic Partial Rendering > Add Tab (https://mvc5.ext.net/#/Dynamic_Partial_Rendering/Add_Tab/) example in MVC Examples Explorer

Basically, you should not, from the rendered component, add an item to it. You should, from the item you want, add it to the rendered panel. This logic may sound strange at first, but it changes how scripts are actually called and built when the response is sent.

While the first approach works during initial page load (where the components' configuration objects are being populated, before page rendering), the second approach ("add it to") is appropriate to change already rendered components, taking advantage of pontual update of the component (instead of re-rendering the whole component).

Hope this helps!

cslocus
Oct 25, 2019, 10:51 AM
Thanks!

I've decided to use a dataview and instead store my data inside the tpl, as I'm not sure I was clear enough in my original question in order for you to understand the specs.

Please close the thread :)

fabricio.murta
Oct 25, 2019, 8:41 PM
Hello! And thanks for the feedback!

Okay, closing; and yes, I thought of "dynamically adding components" and not "items". I hope the solution with dataview works well for you; it is very flexible.