0
votes

I'm writing a form using Zend_Dojo_Form.

Everything goes fine, unless I dynamically insert elements into the form (using ajax, the user can add more elements by clicking a [+] button).

I managed to insert my new Zend_Dojo_Form_Element_FilteringSelect into the page, but the element isn't dojo-enabled (no auto-completion, or tundra styling).

I'm guessing dojo transforms existing form elements once the page is loaded the first time, and doesn't parse them again when a new node is added... But I can't find how to tell dojo there's new elements in town.

What I've tried:

  • Executing dojo.parser.parse(); or dojo.parser.parse('id_of_new_element'); after the insertion of a new element
  • Adding Zend_Dojo_View_Helper_Dojo::setUseDeclarative(); in my Dojo_Form's init() method (as seen on nabble's forums)

My method for adding new elements is the one described by Jeremy Kendall.

I don't know if my problem comes from an incompatibility with Zend, Dojo, or if I'm missing something...

1

1 Answers

1
votes

After some fiddling I finally managed to get it running.

In Jeremy Kendall's javascript code you have to insert the following script in the function ajaxAddField()

// Insert new element before the Add button
$("#addElement-label").before(newElement);

// Parse the element to receive Dojo style
var n = dojo.byId("newName" + id);
n.outerHTML = newElement;
dojo.parser.parse(n);

and replace

searchString = '*[id^=newName' + lastId + '-]';
$(searchString).remove()

in the function removeField() by

// Destroy dijits before destroying the node
var n = dojo.byId("newName" + lastId);
var dijits = dijit.findWidgets(n);
if (dijits) {
    for (var i = 0, n = dijits.length; i < n; i++) {
        dijits[i].destroyRecursive();
    }
}
dijits = dojo.parser.parse("newName" + lastId);

// Destroy the node
dojo.destroy("newName" + lastId);

Maybe there is still a better way to do it, but it worked for me. When removing the node, the dijits have to be destroyed before since otherwise the HTML fragment will not get parsed again.