Add the highlight method to the allowed methods

137 views
Skip to first unread message

soycab...@gmail.com

unread,
Apr 14, 2014, 9:37:04 AM4/14/14
to sel...@googlegroups.com
I've read about ways of showing new elements not included in the current list, specifically the createSearchChoice and the createSearchChoicePosition properties.

The second property can be 'bottom', 'top' or a function returning a specific position, so some people is using it in order to avoid the user pressing "Enter" and creating by accident a new element.

But for me, this is not the desirable approach to deal with the problem. If there are a lot of terms that resolves to the search value, the 'bottom' can be far away, and the user must scroll or write more to narrow the list.

You can see my desirable approach at this fiddle: http://jsfiddle.net/V7SLe/

The js is the original select2.js with a modification at line 3260, where I added the "highlight" method to the list of allowed methods.
From line 3397 is the specific code of the example.

If there is a element with the property "isNew" set to true, the highlight is made at the second element (.on('select2-loaded', function(e) { ...). If not, the first element is the selected by default.


Why the highlight method is not allowed by default?


PD: I think it could be possible to return a diferent set of elements depending on the search term and include the "new" element (with a property marking it as "new") in the list of results to show. Anyway, the same applies respect to the use of the highlight method.

soycab...@gmail.com

unread,
May 9, 2014, 5:56:29 AM5/9/14
to sel...@googlegroups.com, soycab...@gmail.com
Is there any reason to hide the "highlight" method?
Perhaps the events "select2-loaded" or "select2-open" could be expanded to provide a method to highlight a specific element, something like e.highlight(number).

My solution until now is (isNew is a custom attribute returned from the query):

.on('select2-loaded', function(e) {
var data = e.items.results;
for (var i=0; i<data.length; i++)
{
if (data[i].isNew)
{
$('.select2field').select2("highlight", i);
break;
}
}
})

but I need to modify the select2 component and I would prefer this functionality to be included on select2.

Any help?

Reply all
Reply to author
Forward
0 new messages