Sha256: a311ed6879e8e171bf174721383d9765d63b0e5e2d17adf80b7583ba3c0e4655
Contents?: true
Size: 964 Bytes
Versions: 44
Compression:
Stored size: 964 Bytes
Contents
export default class AutoSelectOptionsFromUrl { constructor(options = {}) { this.$source = options.source; this.$select = options.select; this.sourceToParams = options.sourceToParams; this.run(); } run() { this.$source.on("change", this._onSourceChange.bind(this)); this._onSourceChange(); } _onSourceChange() { const select = this.$select; const params = this.sourceToParams(this.$source); const url = this.$source.data("url"); $.getJSON(url, params, function (data) { select.find("option:not([value=''])").remove(); const selectedValue = select.data("selected"); data.forEach((option) => { let optionElement = $(`<option value="${option.id}">${option.body}</option>`).appendTo(select); if (option.id === selectedValue) { optionElement.attr("selected", true); } }); if (selectedValue) { select.val(selectedValue); } }); } }
Version data entries
44 entries across 44 versions & 1 rubygems