【问题标题】:Empty list message on jquery-sortablejquery-sortable上的空列表消息
【发布时间】:2019-03-28 15:03:26
【问题描述】:

我正在使用jquery-sortable,当列表容器(ul) 被清空或加载为空时,我在修改它时遇到了一些困难。例如,如果您有两个容器:

  • 要从中拖动的集合列表始终包含一些项目。
  • 加载为空的目标列表(除非它正在被编辑并且它将包含一些列表项,但可以通过将它们拖出那里来清空)

空容器 (ul) 应在加载为空或在编辑时清空时显示一条消息(即此处没有内容)。

我尝试了几种方法都无济于事。

空容器的 HTML 示例

<ul id="mediaItemsListDest" class="playlist-items connectedSortable">
        <!-- force a message in html -->
    <p>Drag and drop an item from the list</p>
</ul>

不同的 JQUERY 方法

if( $("#mediaItemsListDest li").length >= 1 ) {
      //remove it when the ul contains an li
      $("#mediaItemsListDest p").css('display','none');
 }

if( $("#mediaItemsListDest li").length === 0 ) {
      //no li is found, display a message via jquery but don't add it as a <p> element in the html
      $(this).html("Sorry, this is empty");
 }

if( !$("#mediaItemsListDest").has("li").length ) {
       $(this).html("Sorry, this is empty");
}

他们都没有工作。我还能如何劫持这个空的或清空的列表?

这是一个测试小提琴:

DEMO

提前致谢。

【问题讨论】:

    标签: javascript jquery jquery-ui-sortable


    【解决方案1】:

    您需要处理每个列表更改错误消息状态,因此假设我们有以下 HTML - 您的演示中的示例:

    <ol id="mediaItemsListDest" class="simple_with_animation vertical">
      <p>Drag and drop an item from the list</p>
      <li>Item 1</li>
      <li>Item 2</li>
    </ol>
    

    此外,我还扩展了一个处理消息状态的函数,代码放在应用程序的初始化部分:

    function handleMessage() {
       let liObjects = $('#mediaItemsListDest li');
       let message = $('#mediaItemsListDest p');
    
       console.log('length', liObjects.length);
       console.log('message', message);
    
       if (liObjects.length !== 0) {
          message.css('display', 'none');
       } else {
          message.css('display', 'inline');
       }
    }
    
    handleMessage();
    

    这个函数需要在onDrop事件中调用:

    onDrop: function  ($item, container, _super) {
       // code part removed but you can find in your demo
    
       handleMessage();
    }
    

    我做了一个快速测试,它工作正常。我希望这个对您有所帮助,如果您需要更多信息,请告诉我。

    【讨论】:

    • 效果很好!有一个我无法摆脱或访问的幽灵占位符。开发控制台显示一个空的 UL,但是当容器被清空时那里有一些东西,在清空后会产生不必要的空间。但我猜这是脚本“挂钩”到某物的必要条件——一个目标,我会的。我希望我至少可以让它与 css 一起消失,这样它就不会改变我的列表。但这与这个问题无关。你的回答很好。谢谢
    • 通过修改你的 css() 解决了这个问题;用 addClass() 和 removeClass() 并在上面撒了一点 flexbox css...准备吃!
    猜你喜欢
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2012-03-28
    相关资源
    最近更新 更多