简体   繁体   English

Select2在Django中进行多次选择

[英]Select2 multiple select in django

I'm having a few issues with my Select2 drop-down. 我的Select2下拉菜单存在一些问题。 I originally only needed to select one item on my drop dpwn but I now need to have it as a manytomanyfield so Select2 seemed like the best option. 我最初只需要在drop dpwn上选择一个项目,但是现在我需要将它作为manytomanyfield,所以Select2似乎是最好的选择。

Here is my original code 这是我的原始代码

JS JS

 $.get('/api/foos', function (response){
        $.each(response.foos, function(){
            $('#foo').append('<option>'+this+'</option>')
        })
    })

API 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"]}

This worked nicely for single selection. 这对于单选效果很好。 now I am trying to convert to Select2, but I'm hitting a wall with it I cant seem to get any results into the drop down 现在我正在尝试转换为Select2,但是我碰到了它,似乎无法在下拉菜单中获得任何结果

$.get('/api/foos', function (response){
        $("#set_foo").select2({
            multiple:true,
            placeholder: "Select foos"});
            ('response.foos');
    })

and using the same api call 并使用相同的api调用

fixed it 修复

$.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM