0

I've used the Sortable component from UIKit in my website. I've made it work and now I will be using the event to execute something when ever I move an icon. The event is called change.uk.sortable.

Using jQuery you can call it by:

$(document).ready(function () {
    $("#sample").on("change.uk.sortable", function () {


    });
});

How can I do something like this in AngularJS?

2
  • May be without using jQuery: document.getElementById('sample').addEventListener('change.uk.sortable', function() {...}) Commented Aug 25, 2017 at 5:44
  • @IvanMinakov: I want to use AngularJS. Commented Aug 25, 2017 at 7:46

1 Answer 1

1
+100

You can either use a directive (which is more the AngularJS way), or since you have to include jQuery for uiKit, you could just use jQuery, as in your question.

Either way you'll need to wrap in a $scope.apply() function if you want to update scope properties.

Directive version:

var app = angular.module('App', []);

app.controller('AppCtrl', function($scope) {    
  $scope.movedItemDirectiveVersion = "No item has been moved yet...";    
});

app.directive("mySortable", function() {
  return {
    restrict: 'A',
    link: function(scope, elem, attrs) {
      elem.on('change.uk.sortable.mySortable', function(event, sortableObj, element, action) {
        scope.$apply(function() {
          scope.movedItemDirectiveVersion = "You moved " + element[0].innerText;
        });
      });
      // clean up when scope is destroyed
      scope.$on("$destroy",
        function() {
          elem.off("change.uk.sortable.mySortable");
        }
      );
    }
  }
});
<ul class="uk-sortable uk-grid uk-grid-small uk-grid-width-1-5" data-uk-sortable="" my-sortable>
  <li class="uk-grid-margin" style="">
    <div class="uk-panel uk-panel-box">Item 1</div>
  </li>
  <li class="uk-grid-margin" style="">
    <div class="uk-panel uk-panel-box">Item 2</div>
  </li>
</ul>

Non-directive version:

var app = angular.module('App', []);

app.controller('AppCtrl', function($scope) {
  $scope.movedItem = "No item has been moved yet...";

  $('#withoutDirective').on("change.uk.sortable", function(event, sortableObj, element, action) {
    $scope.$apply(function() {
      $scope.movedItem = "You moved " + element[0].innerText;
    });
  });
});
<ul id="withoutDirective" class="uk-sortable uk-grid uk-grid-small uk-grid-width-1-5" data-uk-sortable="">
  <li class="uk-grid-margin" style="">
    <div class="uk-panel uk-panel-box">Item 1</div>
  </li>
  <li class="uk-grid-margin" style="">
    <div class="uk-panel uk-panel-box">Item 2</div>
  </li>
</ul>

Demo for both versions: http://jsfiddle.net/s9fr43of/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, this is already OK. But can I just request for one code please. I also want to know where i drop the icon so that I can update the database the new arrangement. Thank you in advance.
Have a look at the saveOrdering function in codepen.io/malles/pen/pvLOyb for the basic idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.