Enable Shift-Multiselect in jQuery UI Selectable

前端 未结 3 1762
小鲜肉
小鲜肉 2020-12-14 08:41

I want to enable multiselect capabilities in a jQuery UI Selectable table by holding shift.

I probably should do something like this if shift i

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 08:46

    After looking around I was unable to find a solution to this problem while still using jQuery UI's Selectable function, so I wrote one up. Essentially it taps into the selected / unselected callbacks of Selectable to manage DOM state while still honoring the callbacks as per the standard Selectable API. It supports the following use case:

    • Click one of the elements (shift+click, cntl+click, or click+drag also) anywhere in the list
    • Shift+click another element in the list
    • All elements between plus the two end points become selected

    Usage for a table:

    $('table').shiftSelectable({filter: 'tr'});
    

    A few notes. (1) It currently only supports sibling elements. (2) It will pass through configuration options as you will see in the table example as well as the Selectable methods. (3) I heart underscore.js so it is used even though for this it is not essential. Feel free to swap out its simple checks and extends if you don't want to use this awesome library. And no, I have no affiliation with underscore.js. :)

    table fiddle example

    list fiddle example

    Hope this helps someone else! Cheers.

提交回复
热议问题