intérieur avec SelectParser.prototype.add_option
j'ai obtenu mon attribut personnalisé à l'aide jquery et je l'ai poussé comme une option.
Le SelectParser.prototype.add_option
ressemble:
SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
if (option.nodeName.toUpperCase() === "OPTION") {
if (option.text !== "") {
if (group_position != null) {
this.parsed[group_position].children += 1;
}
this.parsed.push({
user_id: $(option).attr('data-user-id'),
array_index: this.parsed.length,
options_index: this.options_index,
value: option.value,
text: option.text,
html: option.innerHTML,
title: option.title ? option.title : void 0,
selected: option.selected,
disabled: group_disabled === true ? group_disabled : option.disabled,
group_array_index: group_position,
group_label: group_position != null ? this.parsed[group_position].label : null,
classes: option.className,
style: option.style.cssText
});
} else {
this.parsed.push({
array_index: this.parsed.length,
options_index: this.options_index,
empty: true
});
}
return this.options_index += 1;
}
};
Comme vous remarqué que je ne mets que cette ligne user_id: $(option).attr('data-user-id'),
de code.
ensuite pour pousser les données utilisateur-id à la liste <li>
ajouter cette ligne 'data-user-id': item.user_id
-Chosen.prototype.choice_build
donc à la fin il ressemble:
Chosen.prototype.choice_build = function(item) {
var choice, close_link,
_this = this;
choice = $('<li />', {
"class": "search-choice"
}).html("<span>" + (this.choice_label(item)) + "</span>");
if (item.disabled) {
choice.addClass('search-choice-disabled');
} else {
close_link = $('<a />', {
"class": 'search-choice-close',
'data-option-array-index': item.array_index,
'data-user-id': item.user_id
});
close_link.bind('click.chosen', function(evt) {
return _this.choice_destroy_link_click(evt);
});
choice.append(close_link);
}
return this.search_container.before(choice);
};
Ca y est! Vous pouvez pousser autant d'attributs personnalisés que vous le souhaitez de cette façon.
Je ne trouve aucune propriété/méthode par défaut dans 'chosen' pour effectuer ce travail. Vous avez été contraint de le faire en utilisant le code personnalisé jquery. – Mohammad
@Mohammad Merci. –