New Demo – Impromptu with Ajax
03 Mar
Posted by: trent in: Impromptu, Javascript, JsonSQL, Programming
I’ve had several users ask about using Impromptu with ajax to pull in content. I myself far more pull in json to build my content for Impromptu. So I figured I’d kill two birds with one stone by creating an example of using Ajax to pull in JSON content, then build a select statement with that data. Here is the example JSON (nothing tricky here):
[
{ name: 'foo 1', value: 1 },
{ name: 'foo 2', value: 2 },
{ name: 'foo 3', value: 3 },
{ name: 'foo 4', value: 4 },
{ name: 'foo 5', value: 5 },
{ name: 'foo 6', value: 6 },
{ name: 'foo 7', value: 7 },
{ name: 'foo 8', value: 8 },
{ name: 'foo 9', value: 9 },
{ name: 'foo 10', value: 10 },
{ name: 'foo 11', value: 11 },
{ name: 'foo 12', value: 12 }
]
This should be fairly explanatory, name will be the visible text for each select option, and value will of course be that option’s value. To do this we first need to use jQuery’s $.getJSON() method to pull in the JSON, then within that callback we’ll build our string and call the $.prompt. Here is what it looks like:
$.getJSON('demoajax.js',function(json){
var str = 'Test with a select populated by json data:<br />'+
'<select name="myfield" id="myfield">';
$.each(json,function(i,obj){
str += '<option value="'+ obj.value +'">'+ obj.name +'</option>';
});
str += '</select>';
$.prompt(str,{
callback: function(v,m,f){ //just print out the selection
$.prompt('You chose value '+ f.myfield);
}
});
});
Thats all there is to it. Here is the full working example. Don’t forget if you want to do real fun JSON parsing to check out my jsonSQL tool to write sql-like statements to parse your JSON to make combo drop downs more fun!
Related posts:


7 Responses
apot
04|Mar|2009 1dirt simple.. you’ve got ajax json down with impromptu and jsonsql makes it even more fun. for forms impromptu is far ahead of the other solutions
Ben K
05|Mar|2009 2two points questions/
1) in ie6 , if scrolled right, the prompt appears off-screen, this can be fixed by replacing
(line 104) add jqib.css({ top: w.scrollTop(), left:w.scrollLeft() });
(line 128) replace left:0 with left:(ie6)? w.scrollLeft():0
2) a request,if the prompt is large the buttons may appear offscreen. I would like to be able to scroll the screen onto the prompt, while leaving the background fade in position.
I have previously hacked in a “scrolled” flag so that it would leave the dialogue positioned on the screen, while the fader overlay would continue to be “fixed” i’ve been trying to hack at it in the new version with no luck. any help appreciated!
Ben K
05|Mar|2009 3also i ran into a problem in ie7, where the z-index would not apply correctly and elements would appear behind my table etc..
I had to apply the z-index style specifically to the “box” style to get it to work
i.e.
div.jqibox{
z-index: 999; /*stupid ie7 */
}
trent
05|Mar|2009 4Hi Ben, I will look at adding your ie6 fix for the next version. As far as the scrolling issue I’ve seen this before and haven’t thought of a good solution yet, but it’s only once in a blue moon this occurs..
As far as the ie7 thing I’ve not had this happen before, however luckily its easy enough to tweek the css, thats why I tried to do as little styling as possible and leave the rest to css. Thanks for reporting these, I’ll be getting to work :)
Ben K
06|Mar|2009 5Thanks again Trent,
For my own hacking purposes, i’ve managed to almost get the scrolling working but i’m stuck on the last bit.
i’ve swaped the following lines around for my own hack of a site:
jqib.css({ position:”absolute” , height: w.height(), width: “100%”, top: w.scrollTop(), left: w.scrollLeft(), right: 0, bottom: 0 });
jqif.css({ position: (ie6)? “absolute” : “fixed”, height: w.height(), width: “100%”, top: 0, left:0, right: 0, bottom: 0 });
this allows the fader div to be fixed, and the prompt to be absolute.
however i can’t make the fader div to be fixed in ie6,
no matter what i put into the “var ie6scroll = function(){ ” it just don’t work =(
when i do jqib.css({ top: w.scrollTop() }); it seems to be offset by the height of the jqib div
also 1 more note: on line 141 is it supposed to be b.unbind or w.unbind?
Chris C
10|Mar|2009 6It’s worth mentioning that if you have changed the default jquery prefix like I have so I can run jquery alongside prototype you need to modifiy jquery-imprompt.js on line 52 -
from :
var jqib = $(msgbox).appendTo(b);
to :
var jqib = $j(msgbox).appendTo(b);
Where $j is whatever you prefix is set to.
Great script and thanks! :D
trent
10|Mar|2009 7Opps, that was a recent change that slipped by that should have been:
var jqib = jQuery(msgbox).appendTo(b);
Thanks for reporting that!
Leave a reply