【问题标题】:jQuery UI Selectable Without Selection BoxjQuery UI 可选择不带选择框
【发布时间】:2013-04-10 15:49:09
【问题描述】:

在这里给出这个例子:http://jqueryui.com/selectable/#display-grid 我想在没有单击“n 拖动”时出现的“选择框”的情况下进行选择。即,当我点击数字 5 并拖动到数字 6 然后到 10 我得到这个:

实际上我想要的是从 5 拖到 6 到 10,并且只选择它们而不选择 9。

我搜索了文档并找不到该选项,而且我的谷歌技能并没有给我带来任何运气,我认为这一定已经完成了,只是碰巧我自己无法掌握它或找到一个现有的解决方案,所以这里的任何帮助都值得赞赏(不是说你应该为我做研究,我只是希望有人之前处理过这个问题,这样他就可以给我指出正确的方向)。

这也可能是我错过了尝试使用 jquery UI 完成此任务的重点,但这是我发现的第一个适合(某种)我想要完成的示例。

【问题讨论】:

    标签: jquery-ui jquery-ui-selectable


    【解决方案1】:

    首先,您可能想要隐藏 .ui-selectable-helper 或更改 CSS:

    .ui-selectable-helper{display:none;}
    

    接下来,做这样的事情:

    $(function() {
        var _mousedown = false;
        $('#selectable').selectable({
            start: function(event,ui){
                _mousedown=true;
            },
            stop: function(event,ui){
                _mousedown=false;
                $('.ui-selected').removeClass('ui-selected');
                $('.selecting').addClass('ui-selected');
            },
            selecting: function(event,ui){
                if($('.ui-selecting').length == 1)
                    $(ui.selecting).addClass('selecting');
    
                $('.ui-selecting').removeClass('ui-selecting');
                $('.selecting').addClass('ui-selecting');
            },
            unselecting: function(event,ui){
                if($(ui.unselecting).hasClass('selecting'))
                    $(ui.unselecting).removeClass('selecting');
            }
        });
    
        $('#selectable').on('mouseenter', '.ui-selectee', function(){
            if(_mousedown)
                $(this).addClass('selecting');
        });
    });
    

    演示: http://jsfiddle.net/dirtyd77/7UVNS/5/(助手隐藏)

    http://jsfiddle.net/dirtyd77/7UVNS/6/(助手可见)

    如果您有任何问题,请告诉我!


    ***更新:***

    .selectable() 无法满足您的需求。但是,这是我创建的东西。希望对您有所帮助!

    JAVASCRIPT:

    $(function() {
        var _mousedown = false,
            _last=null;    
        $('#selectable li').mousedown(function(){
            _mousedown = true;
            $('.ui-selected').removeClass('ui-selected');
            $('#selectable li').attr('unselectable', 'on').css('user-select', 'none');
            $(this).addClass('ui-selecting');
        }).mouseup(function(){
            _mousedown=false;  
            $('.ui-selecting').addClass('ui-selected').removeClass('ui-selecting');
            $('#selectable li').removeAttr('unselectable style');
        }).mouseenter(function(){
            if(_mousedown){
                if($(this).hasClass('ui-selecting'))
                    $(_last).removeClass('ui-selecting');
    
                $(this).addClass('ui-selecting')
            }
        }).mouseleave(function(){
            if(_mousedown){
                _last =  $(this)[0];
                $(this).addClass('ui-selecting');
            }
        });
    }); 
    

    演示: http://jsfiddle.net/dirtyd77/7UVNS/9/


    ***更新#2:***

    如果您想在移动设备上使用它,我建议您完全更改格式以避免任何可能的陷阱。以下是我的做法:

    JAVASCRIPT:

    $(function() {
        var _clicked = false;
        $('#btn_select').click(function(){
            _clicked = false;
            $(this).hide();
            $('#selectable li').removeAttr('unselectable style');
            $('.ui-selecting').addClass('ui-selected').removeClass('ui-selecting');
        });
        $('#selectable li').click(function(){
            if(!_clicked){
                _clicked = true;
                $('.ui-selected').removeClass('ui-selected');
                $('#selectable li').attr('unselectable', 'on').css('user-select', 'none');
                $('#btn_select').show();
            }
            if($(this).hasClass('ui-selecting')){
                $(this).removeClass('ui-selecting');
            }else{
                $(this).addClass('ui-selecting');
            }
        });
    });  
    

    *注意:您可能希望 $('body').click() 充当 end_selection 按钮。

    演示: http://jsfiddle.net/dirtyd77/7UVNS/13/

    【讨论】:

    • 嗨,Dom,感谢您的帮助。不过有一件事 - 在您的示例中,如果我尝试转到 1、2、6、10、9,我最终只选择了 1 和 9,实际上我希望它们都被选中。
    • 不幸的是,.selectable() 将始终使用帮助程序。这意味着您不能使用.selectable() 来获得所需的效果。但是,我写了一些应该可以帮助你的东西。检查答案。
    • 哇,谢谢 Dom - 这正是我想要完成的事情!
    • @Dom:现在我一直在尝试让这个示例在移动设备(即 iOS 和 Android)上运行,我遇到了 mouseenter 的问题,我一直在寻找,看起来没有直接的解决方案(如插件或某事)所以我问你是否可能遇到过这个例子以及你是如何解决的?我的想法是绑定“touchmove”事件,然后检查位置并检查我是否已经将某些元素转移到另一个元素中 - 只是希望有更少的代码量解决方案所以希望你也许对此没有什么想法。谢谢!
    • @Nikola 很抱歉,我对 jQuery Mobile 没有太多经验。我刚刚在我的手机上测试了这个,看看你的意思。为避免沮丧,您可能希望以不同的方式解决此问题。我已经更新了我的答案。
    【解决方案2】:

    这并没有回答最初的问题,但它确实展示了如何访问可选小部件的帮助器 div。

    var helper = $( "#selectable" ).selectable('widget').data().uiSelectable.helper;
    helper.css( { border:'none' } );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多