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>
javascript arrays json jquery-select2
add a comment |
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>
javascript arrays json jquery-select2
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
add a comment |
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>
javascript arrays json jquery-select2
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
javascript arrays json jquery-select2
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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