Created
March 22, 2017 16:05
-
-
Save roooodcastro/0f596666bb85222940519726dadda068 to your computer and use it in GitHub Desktop.
Selectize plugin - Select or preserve onBlur
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Plugin: "preserve_on_blur" (selectize.js) | |
* Copyright (c) 2016 Eric M. Klingensmith | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this | |
* file except in compliance with the License. You may obtain a copy of the License at: | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software distributed under | |
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | |
* ANY KIND, either express or implied. See the License for the specific language | |
* governing permissions and limitations under the License. | |
* | |
* @author Eric M. Klingensmith <[email protected]> | |
* | |
* Based on methods used in restore_on_backspace plugin by Brian Reavis & contributors. | |
*/ | |
Selectize.define('preserve_on_blur', function(options) { | |
var self = this; | |
options.text = options.text || function(option) { | |
return option[this.settings.labelField]; | |
}; | |
this.onBlur = (function(e) { | |
var original = self.onBlur; | |
return function(e) { | |
// Capture the current input value | |
var $input = this.$control_input; | |
var inputValue = $input.val(); | |
// Do the default actions | |
original.apply(this, e); | |
// Set the value back | |
var searchResults = this.currentResults.items; | |
// If there's only one search result, auto select it | |
if (searchResults.length == 1) { | |
if (!self.items[0] || self.items[0] != searchResults[0].id) { | |
this.addItem(searchResults[0].id, true); | |
} | |
} else { | |
// If there's zero or multiple results, just keep the typed text | |
this.setTextboxValue(inputValue); | |
} | |
}; | |
})(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for this plugin.Gists don't support pull requests. You might want to look at my fork. I added a trigger to the onChange() event so it behaves more like the regular select event.
Line 44 here:
https://gist.github.com/raumi75/06689de23d42d224651f7fcddf47eee2