-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect2.html
More file actions
139 lines (117 loc) · 3.74 KB
/
select2.html
File metadata and controls
139 lines (117 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Select2 Option Generator with Preview & Export</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<style>
body {
font-family: Arial, sans-serif;
padding: 2em;
max-width: 900px;
margin: auto;
background-color: #f9f9f9;
}
textarea, pre, button, select {
width: 100%;
margin-top: 1em;
font-size: 1em;
}
textarea {
height: 200px;
}
pre {
background-color: #fff;
padding: 1em;
border: 1px solid #ccc;
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
}
button {
padding: 0.75em;
margin-top: 0.5em;
margin-right: 0.5em;
background-color: #007bc7;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #005fa3;
}
label {
font-weight: bold;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 1em;
}
</style>
</head>
<body>
<h1>Select2 Option Generator</h1>
<label for="input">Enter items (one per line):<br>
<small>Format: <code>Label</code> or <code>Label|value</code></small>
</label>
<textarea id="input" placeholder="Alabama Chesterfield Borough Council London & South|london-south AL|AL"></textarea>
<div class="button-row">
<button onclick="generateOptions()">Generate Options</button>
<button onclick="copyToClipboard()">Copy to Clipboard</button>
<button onclick="downloadAsHTML()">Download as .html</button>
</div>
<label for="output">Generated HTML:</label>
<pre id="output"></pre>
<label for="preview">Live Preview:</label>
<select id="preview" class="js-select2" style="width: 100%;"></select>
<!-- JS Dependencies -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
function slugify(str) {
return str
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '') // remove special chars
.replace(/\s+/g, '-') // replace space with hyphen
.replace(/-+/g, '-'); // remove extra hyphens
}
function generateOptions() {
const input = document.getElementById('input').value;
const lines = input.split('\n').map(line => line.trim()).filter(Boolean);
const options = lines.map(line => {
let [label, value] = line.split('|').map(s => s.trim());
if (!value) value = slugify(label);
return `<option value="${value}">${label}</option>`;
});
const html = options.join('\n');
document.getElementById('output').textContent = html;
// Update live preview
const select = document.getElementById('preview');
select.innerHTML = options.join('');
$('.js-select2').select2();
}
function copyToClipboard() {
const text = document.getElementById('output').textContent;
navigator.clipboard.writeText(text).then(() => {
alert("Copied to clipboard!");
}).catch(err => {
alert("Copy failed: " + err);
});
}
function downloadAsHTML() {
const content = document.getElementById('output').textContent;
const blob = new Blob([content], { type: 'text/html' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'select2-options.html';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
</script>
</body>
</html>