[英]Select2 multiple select in django
我的Select2下拉菜單存在一些問題。 我最初只需要在drop dpwn上選擇一個項目,但是現在我需要將它作為manytomanyfield,所以Select2似乎是最好的選擇。
這是我的原始代碼
JS
$.get('/api/foos', function (response){
$.each(response.foos, function(){
$('#foo').append('<option>'+this+'</option>')
})
})
API
@require_GET
def Foos(request):
return JsonResponse({
'foos':[x.foo_name for x in FOO.objects.all()]
})
{foos: ["shh", "fgdh", "fgb", "ghfs", "sfgh", "sfgh", "srth"]}
這對於單選效果很好。 現在我正在嘗試轉換為Select2,但是我碰到了它,似乎無法在下拉菜單中獲得任何結果
$.get('/api/foos', function (response){
$("#set_foo").select2({
multiple:true,
placeholder: "Select foos"});
('response.foos');
})
並使用相同的api調用
修復
$.get('/api/locations', function (response){
var data = (response.locations)
console.log(data)
$("#set_location").select2({
data: data,
multiple:true,
tags:true,
placeholder: "Select Locations",
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.