Skip to content

Commit e57e0b1

Browse files
committed
Added decorator to remove the placeholder object
This will remove the placeholder object from the results list.
1 parent 09a0bb8 commit e57e0b1

8 files changed

Lines changed: 256 additions & 7 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,49 @@ define('select2/dropdown/search',[
13951395
return Search;
13961396
});
13971397

1398+
define('select2/dropdown/hidePlaceholder',[
1399+
1400+
], function () {
1401+
function HidePlaceholder (decorated, $element, options, dataAdapter) {
1402+
this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
1403+
1404+
decorated.call(this, $element, options, dataAdapter);
1405+
}
1406+
1407+
HidePlaceholder.prototype.append = function (decorated, data) {
1408+
data = this.removePlaceholder(data);
1409+
1410+
decorated.call(this, data);
1411+
};
1412+
1413+
HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
1414+
if (typeof placeholder === 'string') {
1415+
placeholder = {
1416+
id: '',
1417+
text: placeholder
1418+
};
1419+
}
1420+
1421+
return placeholder;
1422+
};
1423+
1424+
HidePlaceholder.prototype.removePlaceholder = function (_, data) {
1425+
var modifiedData = data.slice(0);
1426+
1427+
for (var d = data.length - 1; d >= 0; d--) {
1428+
var item = data[d];
1429+
1430+
if (this.placeholder.id === item.id) {
1431+
modifiedData.splice(d, 1);
1432+
}
1433+
}
1434+
1435+
return modifiedData;
1436+
};
1437+
1438+
return HidePlaceholder;
1439+
});
1440+
13981441
define('select2/i18n/en',[],function () {
13991442
return {
14001443
noResults: function () {
@@ -1421,13 +1464,15 @@ define('select2/defaults',[
14211464

14221465
'./dropdown',
14231466
'./dropdown/search',
1467+
'./dropdown/hidePlaceholder',
14241468

14251469
'./i18n/en'
14261470
], function ($, ResultsList,
14271471
SingleSelection, MultipleSelection, Placeholder,
14281472
Utils, Translation,
14291473
SelectData, ArrayData, AjaxData, Tags,
1430-
Dropdown, Search, EnglishTranslation) {
1474+
Dropdown, Search, HidePlaceholder,
1475+
EnglishTranslation) {
14311476
function Defaults () {
14321477
this.reset();
14331478
}
@@ -1472,6 +1517,11 @@ define('select2/defaults',[
14721517
options.selectionAdapter,
14731518
Placeholder
14741519
);
1520+
1521+
options.resultsAdapter = Utils.Decorate(
1522+
options.resultsAdapter,
1523+
HidePlaceholder
1524+
);
14751525
}
14761526
}
14771527

dist/js/select2.amd.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,49 @@ define('select2/dropdown/search',[
13951395
return Search;
13961396
});
13971397

1398+
define('select2/dropdown/hidePlaceholder',[
1399+
1400+
], function () {
1401+
function HidePlaceholder (decorated, $element, options, dataAdapter) {
1402+
this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
1403+
1404+
decorated.call(this, $element, options, dataAdapter);
1405+
}
1406+
1407+
HidePlaceholder.prototype.append = function (decorated, data) {
1408+
data = this.removePlaceholder(data);
1409+
1410+
decorated.call(this, data);
1411+
};
1412+
1413+
HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
1414+
if (typeof placeholder === 'string') {
1415+
placeholder = {
1416+
id: '',
1417+
text: placeholder
1418+
};
1419+
}
1420+
1421+
return placeholder;
1422+
};
1423+
1424+
HidePlaceholder.prototype.removePlaceholder = function (_, data) {
1425+
var modifiedData = data.slice(0);
1426+
1427+
for (var d = data.length - 1; d >= 0; d--) {
1428+
var item = data[d];
1429+
1430+
if (this.placeholder.id === item.id) {
1431+
modifiedData.splice(d, 1);
1432+
}
1433+
}
1434+
1435+
return modifiedData;
1436+
};
1437+
1438+
return HidePlaceholder;
1439+
});
1440+
13981441
define('select2/i18n/en',[],function () {
13991442
return {
14001443
noResults: function () {
@@ -1421,13 +1464,15 @@ define('select2/defaults',[
14211464

14221465
'./dropdown',
14231466
'./dropdown/search',
1467+
'./dropdown/hidePlaceholder',
14241468

14251469
'./i18n/en'
14261470
], function ($, ResultsList,
14271471
SingleSelection, MultipleSelection, Placeholder,
14281472
Utils, Translation,
14291473
SelectData, ArrayData, AjaxData, Tags,
1430-
Dropdown, Search, EnglishTranslation) {
1474+
Dropdown, Search, HidePlaceholder,
1475+
EnglishTranslation) {
14311476
function Defaults () {
14321477
this.reset();
14331478
}
@@ -1472,6 +1517,11 @@ define('select2/defaults',[
14721517
options.selectionAdapter,
14731518
Placeholder
14741519
);
1520+
1521+
options.resultsAdapter = Utils.Decorate(
1522+
options.resultsAdapter,
1523+
HidePlaceholder
1524+
);
14751525
}
14761526
}
14771527

dist/js/select2.full.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10930,6 +10930,49 @@ define('select2/dropdown/search',[
1093010930
return Search;
1093110931
});
1093210932

10933+
define('select2/dropdown/hidePlaceholder',[
10934+
10935+
], function () {
10936+
function HidePlaceholder (decorated, $element, options, dataAdapter) {
10937+
this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
10938+
10939+
decorated.call(this, $element, options, dataAdapter);
10940+
}
10941+
10942+
HidePlaceholder.prototype.append = function (decorated, data) {
10943+
data = this.removePlaceholder(data);
10944+
10945+
decorated.call(this, data);
10946+
};
10947+
10948+
HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
10949+
if (typeof placeholder === 'string') {
10950+
placeholder = {
10951+
id: '',
10952+
text: placeholder
10953+
};
10954+
}
10955+
10956+
return placeholder;
10957+
};
10958+
10959+
HidePlaceholder.prototype.removePlaceholder = function (_, data) {
10960+
var modifiedData = data.slice(0);
10961+
10962+
for (var d = data.length - 1; d >= 0; d--) {
10963+
var item = data[d];
10964+
10965+
if (this.placeholder.id === item.id) {
10966+
modifiedData.splice(d, 1);
10967+
}
10968+
}
10969+
10970+
return modifiedData;
10971+
};
10972+
10973+
return HidePlaceholder;
10974+
});
10975+
1093310976
define('select2/i18n/en',[],function () {
1093410977
return {
1093510978
noResults: function () {
@@ -10956,13 +10999,15 @@ define('select2/defaults',[
1095610999

1095711000
'./dropdown',
1095811001
'./dropdown/search',
11002+
'./dropdown/hidePlaceholder',
1095911003

1096011004
'./i18n/en'
1096111005
], function ($, ResultsList,
1096211006
SingleSelection, MultipleSelection, Placeholder,
1096311007
Utils, Translation,
1096411008
SelectData, ArrayData, AjaxData, Tags,
10965-
Dropdown, Search, EnglishTranslation) {
11009+
Dropdown, Search, HidePlaceholder,
11010+
EnglishTranslation) {
1096611011
function Defaults () {
1096711012
this.reset();
1096811013
}
@@ -11007,6 +11052,11 @@ define('select2/defaults',[
1100711052
options.selectionAdapter,
1100811053
Placeholder
1100911054
);
11055+
11056+
options.resultsAdapter = Utils.Decorate(
11057+
options.resultsAdapter,
11058+
HidePlaceholder
11059+
);
1101011060
}
1101111061
}
1101211062

dist/js/select2.full.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,49 @@ define('select2/dropdown/search',[
18231823
return Search;
18241824
});
18251825

1826+
define('select2/dropdown/hidePlaceholder',[
1827+
1828+
], function () {
1829+
function HidePlaceholder (decorated, $element, options, dataAdapter) {
1830+
this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
1831+
1832+
decorated.call(this, $element, options, dataAdapter);
1833+
}
1834+
1835+
HidePlaceholder.prototype.append = function (decorated, data) {
1836+
data = this.removePlaceholder(data);
1837+
1838+
decorated.call(this, data);
1839+
};
1840+
1841+
HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
1842+
if (typeof placeholder === 'string') {
1843+
placeholder = {
1844+
id: '',
1845+
text: placeholder
1846+
};
1847+
}
1848+
1849+
return placeholder;
1850+
};
1851+
1852+
HidePlaceholder.prototype.removePlaceholder = function (_, data) {
1853+
var modifiedData = data.slice(0);
1854+
1855+
for (var d = data.length - 1; d >= 0; d--) {
1856+
var item = data[d];
1857+
1858+
if (this.placeholder.id === item.id) {
1859+
modifiedData.splice(d, 1);
1860+
}
1861+
}
1862+
1863+
return modifiedData;
1864+
};
1865+
1866+
return HidePlaceholder;
1867+
});
1868+
18261869
define('select2/i18n/en',[],function () {
18271870
return {
18281871
noResults: function () {
@@ -1849,13 +1892,15 @@ define('select2/defaults',[
18491892

18501893
'./dropdown',
18511894
'./dropdown/search',
1895+
'./dropdown/hidePlaceholder',
18521896

18531897
'./i18n/en'
18541898
], function ($, ResultsList,
18551899
SingleSelection, MultipleSelection, Placeholder,
18561900
Utils, Translation,
18571901
SelectData, ArrayData, AjaxData, Tags,
1858-
Dropdown, Search, EnglishTranslation) {
1902+
Dropdown, Search, HidePlaceholder,
1903+
EnglishTranslation) {
18591904
function Defaults () {
18601905
this.reset();
18611906
}
@@ -1900,6 +1945,11 @@ define('select2/defaults',[
19001945
options.selectionAdapter,
19011946
Placeholder
19021947
);
1948+
1949+
options.resultsAdapter = Utils.Decorate(
1950+
options.resultsAdapter,
1951+
HidePlaceholder
1952+
);
19031953
}
19041954
}
19051955

dist/js/select2.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/select2/defaults.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ define([
1616

1717
'./dropdown',
1818
'./dropdown/search',
19+
'./dropdown/hidePlaceholder',
1920

2021
'./i18n/en'
2122
], function ($, ResultsList,
2223
SingleSelection, MultipleSelection, Placeholder,
2324
Utils, Translation,
2425
SelectData, ArrayData, AjaxData, Tags,
25-
Dropdown, Search, EnglishTranslation) {
26+
Dropdown, Search, HidePlaceholder,
27+
EnglishTranslation) {
2628
function Defaults () {
2729
this.reset();
2830
}
@@ -67,6 +69,11 @@ define([
6769
options.selectionAdapter,
6870
Placeholder
6971
);
72+
73+
options.resultsAdapter = Utils.Decorate(
74+
options.resultsAdapter,
75+
HidePlaceholder
76+
);
7077
}
7178
}
7279

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
define([
2+
3+
], function () {
4+
function HidePlaceholder (decorated, $element, options, dataAdapter) {
5+
this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
6+
7+
decorated.call(this, $element, options, dataAdapter);
8+
}
9+
10+
HidePlaceholder.prototype.append = function (decorated, data) {
11+
data = this.removePlaceholder(data);
12+
13+
decorated.call(this, data);
14+
};
15+
16+
HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
17+
if (typeof placeholder === 'string') {
18+
placeholder = {
19+
id: '',
20+
text: placeholder
21+
};
22+
}
23+
24+
return placeholder;
25+
};
26+
27+
HidePlaceholder.prototype.removePlaceholder = function (_, data) {
28+
var modifiedData = data.slice(0);
29+
30+
for (var d = data.length - 1; d >= 0; d--) {
31+
var item = data[d];
32+
33+
if (this.placeholder.id === item.id) {
34+
modifiedData.splice(d, 1);
35+
}
36+
}
37+
38+
return modifiedData;
39+
};
40+
41+
return HidePlaceholder;
42+
});

0 commit comments

Comments
 (0)