admin管理员组文章数量:1426049
I've got select2 set up and working using a JS variable to load in the data, however because my data has a lot of options (countries), I'd like to load them from a rather than have that in my JS.
I've created a countries.json file with all the countries in it, and I'm using the below code to try and pull them in. However I get the error The results could not be loaded
.
Code:
$('#allowedCountries').select2({
placeholder: 'Select allowed countries',
selectOnClose: false,
tags: false,
tokenSeparators: [',', ' '],
ajax: {
dataType : "json",
url : "includes/countries.json",
processResults: function (data) {
return {
results: $.map(data, function(obj) {
return { id: obj.ime, text: obj.ime };
})
};
}
},
});
countries.json
[{id:1, text: 'Afghanistan'},
{id:2, text: 'Aland Islands'},
{id:3, text: 'Albania'},
...
{id:245, text: 'Yemen'},
{id:246, text: 'Zambia'},
{id:247, text: 'Zimbabwe'}]
I've got select2 set up and working using a JS variable to load in the data, however because my data has a lot of options (countries), I'd like to load them from a rather than have that in my JS.
I've created a countries.json file with all the countries in it, and I'm using the below code to try and pull them in. However I get the error The results could not be loaded
.
Code:
$('#allowedCountries').select2({
placeholder: 'Select allowed countries',
selectOnClose: false,
tags: false,
tokenSeparators: [',', ' '],
ajax: {
dataType : "json",
url : "includes/countries.json",
processResults: function (data) {
return {
results: $.map(data, function(obj) {
return { id: obj.ime, text: obj.ime };
})
};
}
},
});
countries.json
[{id:1, text: 'Afghanistan'},
{id:2, text: 'Aland Islands'},
{id:3, text: 'Albania'},
...
{id:245, text: 'Yemen'},
{id:246, text: 'Zambia'},
{id:247, text: 'Zimbabwe'}]
Share
Improve this question
asked Nov 28, 2017 at 10:29
d.abyssd.abyss
2121 gold badge5 silver badges26 bronze badges
4
-
You dont need to reproccess you country result, select2 wait id and text. Same as your json file. If you anyway have to do it, do
return { id: obj.id, text: obj.text };
– Camille Commented Nov 28, 2017 at 11:03 - @Camille I don't have any special need for it, this is just the only example code I could find for my purpose :( im not familiar with javascript or jquery so I'm quite lost with this. I can fix it by storing data in javascript var but really should be done with a json file. – d.abyss Commented Nov 28, 2017 at 11:27
-
ok, remove function
processResults
to only havedataType
andurl
attributes inajax
– Camille Commented Nov 28, 2017 at 11:33 - @Camille I found the problem, the data in json file was not in correct format. My data now loads correctly. – d.abyss Commented Nov 28, 2017 at 12:08
1 Answer
Reset to default 5Found the solution with the help of @Camille.
Unnecessary reprocessing the json file (only necessary if your json file uses different identifiers than
"id"
and"text"
The json file was in an incorrect format, see https://select2/data-sources/formats
Code:
// Create countries dropdown
$('#allowedCountries').select2({
placeholder: 'Select allowed countries',
selectOnClose: false,
tags: false,
tokenSeparators: [',', ' '],
ajax: {
dataType : "json",
url : "includes/countries.json",
},
});
countries.json
{
"results": [
{
"id": 1,
"text": "Afghanistan"
},
{
"id": 2,
"text": "Aland Islands"
},
{
"id": 3,
"text": "Albania"
},
...
{
"id": 245,
"text": "Yemen"
},
{
"id": 246,
"text": "Zambia"
},
{
"id": 247,
"text": "Zimbabwe"
}
]
}
本文标签: javascriptSelect2 load data from local json fileStack Overflow
版权声明:本文标题:javascript - Select2 load data from local json file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745410611a2657450.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论