angularjs – 我如何才能使它只有一个空的状态或一个放置目标占位符的ui-sortable显示?

前端之家收集整理的这篇文章主要介绍了angularjs – 我如何才能使它只有一个空的状态或一个放置目标占位符的ui-sortable显示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个连接的ui可排序的列表。当其中一个列表为空时,我需要显示一条消息;当拖动时清空空列表时,我需要显示一个样式的放置目标并隐藏空列表消息。我能够编写绝大多数这个代码here is a simplifed Codepen of it working.

错误是当您从填充列表中拖动空列表然后再次出现时,空列表将显示空列表占位符和样式放样目标。这是一个屏幕截图:

问题的根源似乎在于我计算sortableList指令的列表是否为空:

  1. scope.isEmpty = function() {
  2. if (!scope.attachments) {
  3. return true;
  4. } else if (scope.dragDirection === 'drag-out' && !scope.hovered) {
  5. return scope.attachments.length <= 1;
  6. } else if (scope.hovered) {
  7. return false;
  8. } else {
  9. return scope.attachments.length === 0;
  10. }
  11. };

请注意,我正在跟踪范围的状态,并使用$ apply确保DOM更新如下所示:

  1. function onDragStart() {
  2. scope.$apply(function() {
  3. scope.dragDirection = 'drag-out';
  4. });
  5. }
  6.  
  7. function onDragStop() {
  8. scope.$apply(function() {
  9. scope.dragDirection = '';
  10. });
  11. }
  12.  
  13. function onDragOver() {
  14. scope.$apply(function() {
  15. scope.hovered = true;
  16. });
  17. }
  18.  
  19. function onDragOut() {
  20. scope.$apply(function() {
  21. scope.hovered = false;
  22. });
  23. }

这是指令模板的html:

  1. <div class="drop-target" ui-sortable="sortOptions" ng-model="attachments">
  2. <div ng-repeat="attachment in attachments" class="attachment-Box">
  3. <span class="fa fa-bars pull-left drag-handle"></span>
  4. <div class="link-attachment">
  5. <a href ng-href="{{ attachment.fileUrl }}" target="_blank" class="attachment-name">{{ attachment.name }}</a>
  6. <div class="extra-info link-info">{{ attachment.fileType }}</div>
  7. </div>
  8. </div>
  9. <attachment-empty-state ng-show="isEmpty()"></attachment-empty-state>
  10. </div>

依赖列表对于代码工作来说是相当长的,我简化了实际生产代码中的代码,并消除了依赖关系将使定制代码相当可观。如果你想尝试让它自己运行,这里是一个依赖关系的列表:jquery,jquery-ui,angular,bootstrap,lodash,并且可以从angular-ui进行排序。在那里也有一些字体。

我想我解决了这个问题。这是一个 codepen with the solution

基本上,问题是当您的游标将项目从可排序列表中拖出时,dragout事件被正确地触发,但占位符将保留在可排序列表中,直到将其拖动到另一个可排序列表中。所以在两个时间之间,attach-empty-state元素和占位符都将显示在sortable-list中。

以下是我在代码中编辑的行:

文件

  1. attachment-empty-state {
  2. ...
  3. // hide empty state when the placeholder is in this list
  4. .placeholderShown & {
  5. display:none;
  6. }
  7. }

JS:

  1. //Inside sortable-list
  2. // Helper function
  3. function setPlaceholderShownClass(element) {
  4. $(".drop-target").removeClass("placeholderShown");
  5. $(element).addClass("placeholderShown");
  6. }
  7.  
  8. ...
  9.  
  10. function onPlaceholderUpdate(container,placeholder) {
  11. setPlaceholderShownClass(container.element.context);
  12. ...
  13. }

如果你不喜欢使用jQuery在全局添加删除类,你可以使用$ rootScope。$ broadcast(“placeholderShown”)和$ rootScope。$ on(“placeholderShown”,function(){// scope logic}想到一点点jQuery不太复杂,即使它不是纯粹的角度。

猜你在找的Angularjs相关文章