I want one of my forms to work the same as the admin page, so I decided that I would look at the code and see how it works.
In particular, I want the user to be able to click the β+β sign next to the selection list and go to the pop-up form of the admin page to add a new item.
When they introduce a new item, I want the new item to appear in the selection box and be selected (how and how this function works on the admin pages).
I copied the js-jsys libraries into my own template, and I made the link a link to the same JS function, and the pop-up windows open correctly, but after saving the new object, the pop-up window becomes empty, not closed, and nothing happens on the parent page.
Here is what I put on my page:
... <td> <div class="fieldWrapper"> <select name="form-0-plasmid" id="id_form-0-plasmid"> ... </select> <a href="/admin/VirusTracker/plasmid/add/" class="add-another" id="add_id_plasmid" onclick="return showAddAnotherPopup(this);"> <img src="/media/admin/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a> </div> </td> ...
I tried going through javascript in the admin form to see how it works, but I don't see anything that could close the window or fill the parent window.
Thanks in advance for your help.
Update 3
I get this javascript error when running offsetAddAnotherPopup function
"SelectBox is not defined"
What is indicated on this line in the offsetAddAnotherPopup function
SelectBox.add_to_cache(toId, o);
I thought I knew Javascript, but I do not see where this variable should come from: - (
Update 2
Everything seems to be working correctly. After I click "Save" in the popup window, I get a blank page. This is the source of this page:
<script type="text/javascript">opener.dismissAddAnotherPopup(window, "9", "CMV_flex_myr_GENE1_._._WPRE_BGH");</script>
So it seems that this javascript is not executing or not working.
Update
Here is the corresponding code mentioned by Daniel. Therefore, the only problem is that this code either fails or fails to work correctly.
Django / vno / admin / options.py:
... if request.POST.has_key("_popup"): return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \ # escape() calls force_unicode. (escape(pk_value), escapejs(obj))) ...
/media/admin/js/admin/RelatedObjectLookups.js:
function dismissAddAnotherPopup(win, newId, newRepr) { // newId and newRepr are expected to have previously been escaped by // django.utils.html.escape. newId = html_unescape(newId); newRepr = html_unescape(newRepr); var name = windowname_to_id(win.name); var elem = document.getElementById(name); if (elem) { if (elem.nodeName == 'SELECT') { var o = new Option(newRepr, newId); elem.options[elem.options.length] = o; o.selected = true; } else if (elem.nodeName == 'INPUT') { if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { elem.value += ',' + newId; } else { elem.value = newId; } } } else { var toId = name + "_to"; elem = document.getElementById(toId); var o = new Option(newRepr, newId); SelectBox.add_to_cache(toId, o); SelectBox.redisplay(toId); } win.close(); }