Select2: Why code demo copied don't work?











up vote
0
down vote

favorite












I have 10.000 row data to load in select.
improve performance I thought to load them with the "query" constructor.
I wanted to start from the example "e5" (Loading Data) on the documentation page.
I have also added demos of all "e10" - "Loading Array Data"



Except the first element "e10" all the others do not work. Who can help me ?
Where am I wrong?






$(document).ready(function() {
$("#e5").select2({
minimumInputLength: 1,
query: function(query) {
var data = { results: },
i, j, s;

for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) { s = s + query.term; }
data.results.push({ id: query.term + i, text: s });
}

query.callback(data);
}
});

$("#e10").select2({
theme: "classic",
data:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}],
});

var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];

function format(item) { return item.tag; }

$("#e10_2").select2({
data: { results: data, text: 'tag' },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_3").select2({
data: { results: data, text: function(item) { return item.tag; } },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_4").select2({
data: function() { return { text: 'tag', results: data }; },
formatSelection: format,
formatResult: format,
theme: "classic"
});
});

<link href="http://www.digitalenetwork.it/select2test/css/select2.css" rel="stylesheet" />
<script src="http://www.digitalenetwork.it/select2test/js/jquery-3.3.1.min.js"></script>
<script src="http://www.digitalenetwork.it/select2test/js/select2.full.min.js"></script>

<div>
<div>
<div>
<label for="e5">e5</label>
<div><input id="e5" name="e5" type="hidden" style="width:100%"></div>
</div>
<div>
<label for="e10">e10</label>
<div><input type="hidden" id="e10" style="width:100%" /></div>
</div>
<div>
<label for="e10_2">e10_2</label>
<div><input type="hidden" id="e10_2" style="width:100%" /></div>
</div>
<div>
<label for="e10_3">e10_3</label>
<div><input type="hidden" id="e10_3" style="width:100%" /></div>
</div>
<div>
<label for="e10_4">e10_4</label>
<div><input type="hidden" id="e10_4" style="width:100%" /></div>
</div>
</div>
</div>












share|improve this question
























  • I've removed some unnecessary/unrelated libraries and styling to reduce the example to the bare minimum that still shows the problem.
    – Andreas
    Nov 21 at 18:47















up vote
0
down vote

favorite












I have 10.000 row data to load in select.
improve performance I thought to load them with the "query" constructor.
I wanted to start from the example "e5" (Loading Data) on the documentation page.
I have also added demos of all "e10" - "Loading Array Data"



Except the first element "e10" all the others do not work. Who can help me ?
Where am I wrong?






$(document).ready(function() {
$("#e5").select2({
minimumInputLength: 1,
query: function(query) {
var data = { results: },
i, j, s;

for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) { s = s + query.term; }
data.results.push({ id: query.term + i, text: s });
}

query.callback(data);
}
});

$("#e10").select2({
theme: "classic",
data:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}],
});

var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];

function format(item) { return item.tag; }

$("#e10_2").select2({
data: { results: data, text: 'tag' },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_3").select2({
data: { results: data, text: function(item) { return item.tag; } },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_4").select2({
data: function() { return { text: 'tag', results: data }; },
formatSelection: format,
formatResult: format,
theme: "classic"
});
});

<link href="http://www.digitalenetwork.it/select2test/css/select2.css" rel="stylesheet" />
<script src="http://www.digitalenetwork.it/select2test/js/jquery-3.3.1.min.js"></script>
<script src="http://www.digitalenetwork.it/select2test/js/select2.full.min.js"></script>

<div>
<div>
<div>
<label for="e5">e5</label>
<div><input id="e5" name="e5" type="hidden" style="width:100%"></div>
</div>
<div>
<label for="e10">e10</label>
<div><input type="hidden" id="e10" style="width:100%" /></div>
</div>
<div>
<label for="e10_2">e10_2</label>
<div><input type="hidden" id="e10_2" style="width:100%" /></div>
</div>
<div>
<label for="e10_3">e10_3</label>
<div><input type="hidden" id="e10_3" style="width:100%" /></div>
</div>
<div>
<label for="e10_4">e10_4</label>
<div><input type="hidden" id="e10_4" style="width:100%" /></div>
</div>
</div>
</div>












share|improve this question
























  • I've removed some unnecessary/unrelated libraries and styling to reduce the example to the bare minimum that still shows the problem.
    – Andreas
    Nov 21 at 18:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 10.000 row data to load in select.
improve performance I thought to load them with the "query" constructor.
I wanted to start from the example "e5" (Loading Data) on the documentation page.
I have also added demos of all "e10" - "Loading Array Data"



Except the first element "e10" all the others do not work. Who can help me ?
Where am I wrong?






$(document).ready(function() {
$("#e5").select2({
minimumInputLength: 1,
query: function(query) {
var data = { results: },
i, j, s;

for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) { s = s + query.term; }
data.results.push({ id: query.term + i, text: s });
}

query.callback(data);
}
});

$("#e10").select2({
theme: "classic",
data:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}],
});

var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];

function format(item) { return item.tag; }

$("#e10_2").select2({
data: { results: data, text: 'tag' },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_3").select2({
data: { results: data, text: function(item) { return item.tag; } },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_4").select2({
data: function() { return { text: 'tag', results: data }; },
formatSelection: format,
formatResult: format,
theme: "classic"
});
});

<link href="http://www.digitalenetwork.it/select2test/css/select2.css" rel="stylesheet" />
<script src="http://www.digitalenetwork.it/select2test/js/jquery-3.3.1.min.js"></script>
<script src="http://www.digitalenetwork.it/select2test/js/select2.full.min.js"></script>

<div>
<div>
<div>
<label for="e5">e5</label>
<div><input id="e5" name="e5" type="hidden" style="width:100%"></div>
</div>
<div>
<label for="e10">e10</label>
<div><input type="hidden" id="e10" style="width:100%" /></div>
</div>
<div>
<label for="e10_2">e10_2</label>
<div><input type="hidden" id="e10_2" style="width:100%" /></div>
</div>
<div>
<label for="e10_3">e10_3</label>
<div><input type="hidden" id="e10_3" style="width:100%" /></div>
</div>
<div>
<label for="e10_4">e10_4</label>
<div><input type="hidden" id="e10_4" style="width:100%" /></div>
</div>
</div>
</div>












share|improve this question















I have 10.000 row data to load in select.
improve performance I thought to load them with the "query" constructor.
I wanted to start from the example "e5" (Loading Data) on the documentation page.
I have also added demos of all "e10" - "Loading Array Data"



Except the first element "e10" all the others do not work. Who can help me ?
Where am I wrong?






$(document).ready(function() {
$("#e5").select2({
minimumInputLength: 1,
query: function(query) {
var data = { results: },
i, j, s;

for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) { s = s + query.term; }
data.results.push({ id: query.term + i, text: s });
}

query.callback(data);
}
});

$("#e10").select2({
theme: "classic",
data:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}],
});

var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];

function format(item) { return item.tag; }

$("#e10_2").select2({
data: { results: data, text: 'tag' },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_3").select2({
data: { results: data, text: function(item) { return item.tag; } },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_4").select2({
data: function() { return { text: 'tag', results: data }; },
formatSelection: format,
formatResult: format,
theme: "classic"
});
});

<link href="http://www.digitalenetwork.it/select2test/css/select2.css" rel="stylesheet" />
<script src="http://www.digitalenetwork.it/select2test/js/jquery-3.3.1.min.js"></script>
<script src="http://www.digitalenetwork.it/select2test/js/select2.full.min.js"></script>

<div>
<div>
<div>
<label for="e5">e5</label>
<div><input id="e5" name="e5" type="hidden" style="width:100%"></div>
</div>
<div>
<label for="e10">e10</label>
<div><input type="hidden" id="e10" style="width:100%" /></div>
</div>
<div>
<label for="e10_2">e10_2</label>
<div><input type="hidden" id="e10_2" style="width:100%" /></div>
</div>
<div>
<label for="e10_3">e10_3</label>
<div><input type="hidden" id="e10_3" style="width:100%" /></div>
</div>
<div>
<label for="e10_4">e10_4</label>
<div><input type="hidden" id="e10_4" style="width:100%" /></div>
</div>
</div>
</div>








$(document).ready(function() {
$("#e5").select2({
minimumInputLength: 1,
query: function(query) {
var data = { results: },
i, j, s;

for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) { s = s + query.term; }
data.results.push({ id: query.term + i, text: s });
}

query.callback(data);
}
});

$("#e10").select2({
theme: "classic",
data:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}],
});

var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];

function format(item) { return item.tag; }

$("#e10_2").select2({
data: { results: data, text: 'tag' },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_3").select2({
data: { results: data, text: function(item) { return item.tag; } },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_4").select2({
data: function() { return { text: 'tag', results: data }; },
formatSelection: format,
formatResult: format,
theme: "classic"
});
});

<link href="http://www.digitalenetwork.it/select2test/css/select2.css" rel="stylesheet" />
<script src="http://www.digitalenetwork.it/select2test/js/jquery-3.3.1.min.js"></script>
<script src="http://www.digitalenetwork.it/select2test/js/select2.full.min.js"></script>

<div>
<div>
<div>
<label for="e5">e5</label>
<div><input id="e5" name="e5" type="hidden" style="width:100%"></div>
</div>
<div>
<label for="e10">e10</label>
<div><input type="hidden" id="e10" style="width:100%" /></div>
</div>
<div>
<label for="e10_2">e10_2</label>
<div><input type="hidden" id="e10_2" style="width:100%" /></div>
</div>
<div>
<label for="e10_3">e10_3</label>
<div><input type="hidden" id="e10_3" style="width:100%" /></div>
</div>
<div>
<label for="e10_4">e10_4</label>
<div><input type="hidden" id="e10_4" style="width:100%" /></div>
</div>
</div>
</div>





$(document).ready(function() {
$("#e5").select2({
minimumInputLength: 1,
query: function(query) {
var data = { results: },
i, j, s;

for (i = 1; i < 5; i++) {
s = "";
for (j = 0; j < i; j++) { s = s + query.term; }
data.results.push({ id: query.term + i, text: s });
}

query.callback(data);
}
});

$("#e10").select2({
theme: "classic",
data:[{id:0,text:'enhancement'},{id:1,text:'bug'},{id:2,text:'duplicate'},{id:3,text:'invalid'},{id:4,text:'wontfix'}],
});

var data=[{id:0,tag:'enhancement'},{id:1,tag:'bug'},{id:2,tag:'duplicate'},{id:3,tag:'invalid'},{id:4,tag:'wontfix'}];

function format(item) { return item.tag; }

$("#e10_2").select2({
data: { results: data, text: 'tag' },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_3").select2({
data: { results: data, text: function(item) { return item.tag; } },
formatSelection: format,
formatResult: format,
theme: "classic"
});

$("#e10_4").select2({
data: function() { return { text: 'tag', results: data }; },
formatSelection: format,
formatResult: format,
theme: "classic"
});
});

<link href="http://www.digitalenetwork.it/select2test/css/select2.css" rel="stylesheet" />
<script src="http://www.digitalenetwork.it/select2test/js/jquery-3.3.1.min.js"></script>
<script src="http://www.digitalenetwork.it/select2test/js/select2.full.min.js"></script>

<div>
<div>
<div>
<label for="e5">e5</label>
<div><input id="e5" name="e5" type="hidden" style="width:100%"></div>
</div>
<div>
<label for="e10">e10</label>
<div><input type="hidden" id="e10" style="width:100%" /></div>
</div>
<div>
<label for="e10_2">e10_2</label>
<div><input type="hidden" id="e10_2" style="width:100%" /></div>
</div>
<div>
<label for="e10_3">e10_3</label>
<div><input type="hidden" id="e10_3" style="width:100%" /></div>
</div>
<div>
<label for="e10_4">e10_4</label>
<div><input type="hidden" id="e10_4" style="width:100%" /></div>
</div>
</div>
</div>






javascript arrays json jquery-select2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 18:43









Andreas

16.1k32640




16.1k32640










asked Nov 21 at 18:19









Gianfranco Facchini

6




6












  • I've removed some unnecessary/unrelated libraries and styling to reduce the example to the bare minimum that still shows the problem.
    – Andreas
    Nov 21 at 18:47


















  • I've removed some unnecessary/unrelated libraries and styling to reduce the example to the bare minimum that still shows the problem.
    – Andreas
    Nov 21 at 18:47
















I've removed some unnecessary/unrelated libraries and styling to reduce the example to the bare minimum that still shows the problem.
– Andreas
Nov 21 at 18:47




I've removed some unnecessary/unrelated libraries and styling to reduce the example to the bare minimum that still shows the problem.
– Andreas
Nov 21 at 18:47

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418308%2fselect2-why-code-demo-copied-dont-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418308%2fselect2-why-code-demo-copied-dont-work%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)