asset,
если послать данные в нужном формате не судьба, то отформатировать на месте.
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
select{
width: 200px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0-beta.3/css/select2.min.css" rel="stylesheet" />
<script src="http://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0-beta.3/js/select2.min.js"></script>
<script>
$(function() {
var response = [{ //json
"tag_name": "Tags 1"
}, {
"tag_name": "Tags 2"
}]
function foo(arr) { //format response
arr = $.map(arr, function(el, i) {
return {
id: el["tag_name"],
text: el["tag_name"]
}
})
return arr
}
$.fn.select2.defaults.set("theme", "classic"); //css theme
$('select').select2({
data: foo(response)
})
.val("Tags 2").change(); // test
});
</script>
</head>
<body>
<select name="" id="">
<option value="">item1</option>
<option value="">item2</option>
<option value="">item3</option>
</select>
</body>
</html>