Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selectize doesn't preserve title attribute #1200 #2118

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@
<td valign="top"><code>string</code></td>
<td valign="top"><code>'disabled'</code></td>
</tr>
<tr>
<td valign="top"><code>titleField</code></td>
<td valign="top">The name of the property which provides the title attribute.</td>
<td valign="top"><code>string</code></td>
<td valign="top"><code>'title'</code></td>
</tr>
<tr>
<td valign="top"><code>sortField</code></td>
<td valign="top">
Expand Down
1 change: 1 addition & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Selectize.defaults = {
valueField: 'value',
labelField: 'text',
disabledField: 'disabled',
titleField: 'title',
optgroupLabelField: 'label',
optgroupValueField: 'value',
lockOptgroupOrder: false,
Expand Down
2 changes: 2 additions & 0 deletions src/selectize.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ $.fn.selectize = function (settings_user) {
var field_label = settings.labelField;
var field_value = settings.valueField;
var field_disabled = settings.disabledField;
var field_title = settings.titleField;
var field_optgroup = settings.optgroupField;
var field_optgroup_label = settings.optgroupLabelField;
var field_optgroup_value = settings.optgroupValueField;
Expand Down Expand Up @@ -97,6 +98,7 @@ $.fn.selectize = function (settings_user) {
option[field_label] = option[field_label] || $option.text();
option[field_value] = option[field_value] || value;
option[field_disabled] = option[field_disabled] || $option.prop('disabled');
option[field_title] = option[field_title] || $option.prop('title');
option[field_optgroup] = option[field_optgroup] || group;
option.styles = $option.attr('style') || '';
option.classes = $option.attr('class') || '';
Expand Down
5 changes: 5 additions & 0 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,11 @@ $.extend(Selectize.prototype, {
if (templateName === 'option' || templateName === 'item') {
html.attr('data-value', value || '');
}
if (templateName === 'option' || templateName === 'item') {
if (data[self.settings.titleField]) {
html.attr('title', data[self.settings.titleField]);
}
}

// update cache
if (cache) {
Expand Down
25 changes: 21 additions & 4 deletions test/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
disabledField: 'dis'
});
assert.deepEqual(test.selectize.options, {
'a': { text: 'Item A', value: 'a', grp: ['Group 1', 'Group 2'], $order: 1, dis: false, styles: '', classes: '' },
'b': { text: 'Item B', value: 'b', grp: ['Group 1', 'Group 2'], $order: 2, dis: false, styles: '', classes: '' }
'a': { text: 'Item A', value: 'a', grp: ['Group 1', 'Group 2'], $order: 1, dis: false, title: '', styles: '', classes: '' },
'b': { text: 'Item B', value: 'b', grp: ['Group 1', 'Group 2'], $order: 2, dis: false, title: '', styles: '', classes: '' }
});
assert.deepEqual(test.selectize.optgroups, {
'Group 1': { label: 'Group 1', val: 'Group 1', $order: 3, dis: false },
Expand All @@ -113,14 +113,31 @@
disabledField: 'dis'
});
assert.deepEqual(test.selectize.options, {
'a': { text: 'Item A', value: 'a', grp: ['Group 1', 'Group 2'], $order: 1, dis: true, styles: '', classes: '' },
'b': { text: 'Item B', value: 'b', grp: ['Group 1', 'Group 2'], $order: 2, dis: false, styles: '', classes: '' }
'a': { text: 'Item A', value: 'a', grp: ['Group 1', 'Group 2'], $order: 1, dis: true, title: '', styles: '', classes: '' },
'b': { text: 'Item B', value: 'b', grp: ['Group 1', 'Group 2'], $order: 2, dis: false, title: '', styles: '', classes: '' }
});
assert.deepEqual(test.selectize.optgroups, {
'Group 1': { label: 'Group 1', val: 'Group 1', $order: 3, dis: false },
'Group 2': { label: 'Group 2', val: 'Group 2', $order: 4, dis: true }
}, '2');
});
it('should allow respect title attribute of option', function () {
var test = setup_test(['<select>',
'<optgroup label="Group 1">',
'<option value="a" title="Title A">Item A</option>',
'<option value="b">Item B</option>',
'</optgroup>',
'</select>'].join(''), {
optgroupValueField: 'val',
optgroupField: 'grp',
disabledField: 'dis',
titleField: 'til'
});
assert.deepEqual(test.selectize.options, {
'a': { text: 'Item A', value: 'a', grp: 'Group 1', $order: 1, dis: false, til: 'Title A', styles: '', classes: '' },
'b': { text: 'Item B', value: 'b', grp: 'Group 1', $order: 2, dis: false, til: '', styles: '', classes: '' },
});
});
it('should add options in text form (no html entities)', function () {
var test = setup_test('<select><option selected value="a">&lt;hi&gt;</option></select>', {});
expect(test.selectize.options['a'].text).to.be.equal('<hi>');
Expand Down