ui-select icon indicating copy to clipboard operation
ui-select copied to clipboard

uiSelect should expose OPEN and other events

Open ttonyh opened this issue 12 years ago • 24 comments

uiSelect should expose open and other events that select2 has: opening, change, close, highlight, selecting, removing, removed, loaded, focus, and blur. I understand several are already available but it would be nice to have them all accessible through a single format, e.g. broadcast/on or via attr/observe.

ttonyh avatar Nov 24 '14 07:11 ttonyh

+1, btw ng-blur works

syzer avatar Dec 03 '14 08:12 syzer

+1. I have a form with a bunch of these and I don't want the data to be fetched for them until the person clicks on one. the opening event is the only way I can see to do this unless somebody knows of a different way.

jasedwards avatar Dec 03 '14 17:12 jasedwards

I've found a pretty complex workaround using decorators:

Set up a decorator that extends uiSelect directive with a 'compile' function, passing uiSelect's existing 'link' function to the 'post' link. Then a watcher, to watch some internal properties such as $select.open & $select.focus.

    module.config( function( $provide ) {
        $provide.decorator( "uiSelectDirective", function( $delegate ) {
            var directive = $delegate[ 0 ];

            directive.compile = function compile( tElement, tAttrs ) {
                return {
                    pre: function preLink( scope, iElement, iAttrs, controller ) {
                        scope.$watchGroup( [ "$select.open", "$select.focus" ], function( val ) {
                            scope.$parent.$broadcast( "uiSelect:events", val );
                        });
                    },
                    post: directive.link
                }
            };

            return $delegate;
        });
    });

Finally, add a listener and pick out the event type sent:

    scope.$on( "uiSelect:events", function( e, events ) {
        var open = events[ 0 ],
            focus = events[ 1 ];

        if ( open ) {
            // Do something...
        }
    });

Hope this helps until the events are added into uiSelect.

( Tested on Angular v1.3.1 & uiSelect v0.8.3 )

ttonyh avatar Dec 03 '14 21:12 ttonyh

always forget about delegates. thanks for this.

jasedwards avatar Dec 03 '14 21:12 jasedwards

:+1: thanks!

syzer avatar Dec 04 '14 08:12 syzer

@syzer are you sure that ng-blur works ? I did not succeed to use it, see http://jsfiddle.net/opeaucelle/ca8n37g7/

oslo10e avatar Dec 05 '14 10:12 oslo10e

$select.search is what i was looking for

syzer avatar Dec 05 '14 12:12 syzer

@oslo10e if u read ttonyh answer again u will get it.

syzer avatar Dec 05 '14 12:12 syzer

Just correct me if I am wrong, there's no out-of-the-box ng-blur support and the best approach is @ttonyh elegant snippet, isn't it?

jossemarGT avatar Dec 16 '14 00:12 jossemarGT

@jossemarGT true

syzer avatar Dec 16 '14 11:12 syzer

@jossemarGT I have implemented a good work around with using jQuery events see http://jsfiddle.net/opeaucelle/o4njwsgk/

oslo10e avatar Dec 16 '14 13:12 oslo10e

@oslo10e thanks I'll try it, at this moment I've tried with @ttonyh 's approach watching $select.focus and the second value that it provides. But it's getting my nerve :angry:

jossemarGT avatar Dec 16 '14 13:12 jossemarGT

I think the workaround with decorators is too much for the simplicity of reacting to these specific events. Instead, why not add an on-close callback attribute. I've modified my select.js to accept this.

In the ui-select link function, parse the attribute:

$select.onCloseCallback = $parse(attrs.onClose);

then inside the controller's close method call

ctrl.onCloseCallback($scope, {
    $model: ctrl.parserResult.modelMapper($scope, {})
});

Works as expected and is the right fix for the app my team is working on.

cvharris avatar Feb 17 '15 15:02 cvharris

Workaround https://github.com/angular-ui/ui-select/issues/432#issuecomment-65490071 doesn't work

captainkovalsky avatar Mar 23 '15 16:03 captainkovalsky

Anyone have any luck getting the blur event (and all the others) to work on the active text field? I'm thinking of using jQuery to bind it after it's live, and take control from there.

The snippet from Tony H worked for me, except that the events are called at points during selection that would not normally be interpreted as a .blur or .focus on a normal select.

What I'm after is using the text the user has entered as a newly created value which gets selected and blurs if the user clicks away from the active ui-select element. The select/dropdown currently remains in an active state until a selection is made.

Also, I want that when the tab key gets pressed during selection, it tabs to the next element. It currently selects the first item in the list, and leaves that item focused. (Edit: never mind, that part seems to be working fine.)

Are any of these things possible out-of-the-box?

ghost avatar Apr 08 '15 17:04 ghost

So, I bound .blur to the ".ui-select-search" input box, and selected the first item in the list of options each time that this blurred. The only problem came when I arrowed down to make a selection, then pressed the tab key, it still selected the first option. Any ideas?

ghost avatar Apr 13 '15 15:04 ghost

This is still an issue. One solution it's to use the decorators or add some additional attribute to the directive and then do some work in the link function, but really best would be to have these events in the open API layer.

ghost avatar Apr 28 '15 15:04 ghost

What I've elected to do is handle validation of the ui-select on form submit, which is less than ideal. Leveraging the jQuery Select2 plugin, and moving away from Angular in this specific case seems like a viable option as well.

ghost avatar Apr 28 '15 17:04 ghost

@ttonyh 's workaround wasn't working for me on angular 1.3.15 and ui-select 0.12.0, so I had to tweak it a little bit:

  .config(function ($provide) {
    $provide.decorator('uiSelectDirective', function ($delegate) {

      var directive = $delegate[0];
      var directiveCompile = directive.compile;

      directive.compile = function (tElement, tAttrs) {
        var link = directiveCompile.apply(this, arguments);
        return function(scope, elem, attrs) {
          link.apply(this, arguments);
          scope.$watch('$select.open', function(val) {
            scope.$parent.$broadcast('uiSelect:open', val);
          });
        };
      };

      return $delegate;

    });
  });

If you want to know when blur occurs on your controller: (note this event will also fire if user selected something)

    $scope.$on('uiSelect:open', function(evt, opened) {
      if(!opened) {
        console.log('user selected something or blur ocurred');
      }
    });

yvesmh avatar Jun 06 '15 00:06 yvesmh

Since my last update, there's been some changes to the way ui-select sets up the directive. They now use the compile function, which is why my above code won't work. Here's an update that I just tried, and it seems to work, if you guys want to give it a shot and let me know your results:

module.config( function( $provide ) {
    $provide.decorator( "uiSelectDirective", function( $delegate ) {
        var directive = $delegate[ 0 ],
            compile = directive.compile;

        directive.compile = function compile( tElement, tAttrs ) {
            var link = compile.apply( this, arguments );

            return function( scope, element, attrs, controller ) {
                link.apply( this, arguments );

                var $select = controller[ 0 ];

                scope.$watchGroup( [ "$select.open", "$select.focus" ], function( val ) {
                    scope.$parent.$broadcast( "uiSelect:events", val );
                });
            };
        };

        return $delegate;
    });
});

This basically extends their compile function instead of the code I posted above, which overrides it.

EDIT: ( Tested on Angular v1.3.17 & uiSelect v0.12.1 )

ttonyh avatar Aug 14 '15 02:08 ttonyh