1
0
mirror of synced 2024-11-26 14:56:07 +03:00
select2/select2-test.html

141 lines
5.7 KiB
HTML
Raw Normal View History

2013-02-06 03:24:44 +04:00
<html>
<head>
<script src="js/jquery-1.8.1.js"></script>
<script src="select2-master/select2.js"></script>
<link href="select2-master/select2.css" rel="stylesheet"/>
<style>img.flag { height: 10px; width: 15px; padding-right: 10px; }
</style>
</head>
<body>
<div style="padding: 10px; margin: 10px;">
2013-02-09 00:13:59 +04:00
<br/><br/><br/>
<input type="text" size="30"/><br/>
2013-03-25 20:12:08 +04:00
<label for="e4">Label: </label>
2013-02-06 03:24:44 +04:00
<select id="e4" style="width:300px" class="populate">
2013-02-09 00:13:59 +04:00
<option></option>
2013-02-06 03:24:44 +04:00
<option value="AK">Alaska</option>
<option value="HI">Hawaii</option>
<option value="CA">California</option>
<option value="NV">Nevada</option>
<option value="OR">Oregon</option>
<option value="WA">Washington</option>
2013-03-15 01:01:17 +04:00
<!--
2013-03-01 12:48:41 +04:00
<option value="AK1">Alaska</option>
<option value="HI1">Hawaii</option>
<option value="CA1">California</option>
<option value="NV1">Nevada</option>
<option value="OR1">Oregon</option>
<option value="WA1">Washington</option><option value="AK2">Alaska</option>
<option value="HI2">Hawaii</option>
<option value="CA2">California</option>
<option value="NV2">Nevada</option>
<option value="OR2">Oregon</option>
<option value="WA2">Washington</option>
2013-03-15 01:01:17 +04:00
-->
2013-02-06 03:24:44 +04:00
</select><br/>
2013-02-09 00:13:59 +04:00
<input type="text" size="30"/>
2013-02-06 03:24:44 +04:00
</div>
2013-02-12 12:13:56 +04:00
2013-03-25 20:12:08 +04:00
<label for="e6">Label: </label>
2013-02-12 12:13:56 +04:00
<input type="hidden" class="bigdrop" id="e6" style="width:600px" value="16340"/>
<script>
2013-02-06 03:24:44 +04:00
$(document).ready(function() {
function format(state) {
if (!state.id) return state.text; // optgroup
return "<img class='flag' src='images/flags/" + state.id.toLowerCase() + ".png'/>" + state.text;
}
$("#e4").select2({
formatResult: format,
2013-02-09 00:13:59 +04:00
formatSelection: format,
escapeMarkup: function(m) { return m; },
allowClear:true,
2013-02-20 22:06:11 +04:00
placeholder: "select a state",
maximumInputLength:3
2013-02-06 03:24:44 +04:00
});
});
</script>
2013-02-09 00:13:59 +04:00
<div style="position: absolute; left:0; top:0; background: white; border: 1px solid red;" id="focus-spy">hello there</div>
<script>
$(document).ready(function() {
var el=$("#focus-spy");
$(window).bind("scroll", function(){ el.css({top:$(window).scrollTop()}); });
var update=function() {
var a=document.activeElement;
var b=$(a);
el.html("tag: "+a.tagName+" id:"+a.id+" class:"+b.attr("class")+" val:"+b.val());
window.setTimeout(update, 100);
};
update();
});
</script>
2013-02-12 12:13:56 +04:00
<script id="script_e6">
function movieFormatResult(movie) {
var markup = "<table class='movie-result'><tr>";
if (movie.posters !== undefined && movie.posters.thumbnail !== undefined) {
markup += "<td class='movie-image'><img src='" + movie.posters.thumbnail + "'/></td>";
}
markup += "<td class='movie-info'><div class='movie-title'>" + movie.title + "</div>";
if (movie.critics_consensus !== undefined) {
markup += "<div class='movie-synopsis'>" + movie.critics_consensus + "</div>";
}
else if (movie.synopsis !== undefined) {
markup += "<div class='movie-synopsis'>" + movie.synopsis + "</div>";
}
markup += "</td></tr></table>"
return markup;
}
2013-02-06 03:24:44 +04:00
2013-02-12 12:13:56 +04:00
function movieFormatSelection(movie) {
return movie.title;
}
$(document).ready(function() {
$("#e6").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
2013-03-25 20:12:08 +04:00
multiple:true,
2013-02-12 12:13:56 +04:00
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
dataType: 'jsonp',
data: function (term, page) {
return {
q: term, // search term
page_limit: 10,
apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
};
},
results: function (data, page) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data.movies};
}
},
initSelection: function(element, callback) {
// the input tag has a value attribute preloaded that points to a preselected movie's id
// this function resolves that id attribute to an object that select2 can render
// using its formatResult renderer - that way the movie name is shown preselected
var id=$(element).val();
if (id!=="") {
$.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/"+id+".json", {
data: {
apikey: "ju6z9mjyajq2djue3gbvv26t"
},
dataType: "jsonp"
}).done(function(data) { callback(data); });
}
},
formatResult: movieFormatResult, // omitted for brevity, see the source of this page
formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
2013-03-15 01:01:17 +04:00
formatSearching: function() {return "Searching...";},
2013-02-12 12:13:56 +04:00
escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
});
});
</script>
2013-02-06 03:24:44 +04:00
</body>
</html>