简体   繁体   English

如何通过鼠标拖动在铁选择器中选择多个元素

[英]How to select multiple elements in iron-selector by mouse drag

How can I select multiple items inside an iron-selector by clicking, holding and dragging the mouse, or even by drawing a rectangle with the mouse, and selecting all the items under it? 如何通过单击,按住和拖动鼠标,甚至用鼠标绘制一个矩形,然后选择其下的所有项目,在iron-selector选择多个项目?

I use Polymer 1.11.3 and iron-selector 2.1.0, and reading the documentation provided no obvious solution. 我使用聚合物1.11.3和铁选择器2.1.0,并且阅读文档没有提供明显的解决方案。

This is my actual element in which I want to enable the drag-selection: 这是我要启用拖动选择的实际元素:

在此处输入图片说明

My goal is to be able to click eg on Sunday 7, drag the mouse to 15, release the click, and have 7-15 selected. 我的目标是能够单击,例如在星期日7,将鼠标拖动到15,释放单击,并选择7-15。

You can select multiple items in iron-selector with multi attribute: 您可以在具有multi属性的iron-selector选择多个项目:

 <iron-selector attr-for-selected="name" selected-items = "{{selected}}" multi>
    <div name="foo">Foo</div>
    <div name="bar">Bar</div>
    <div name="zot">Zot</div>
  </iron-selector>

Selected items will be an array at selected property. 选定项将是selected属性处的数组。

DEMO 演示

To be able to select my mouse click and drag, do the following: 为了能够选择鼠标单击并拖动,请执行以下操作:

  1. Set the css property user-select: none; 设置css属性user-select: none; to the element which holds your selectable items. 到包含您的可选项目的元素。

  2. Add on-track="handleTrack" to the element which holds your selectable items. on-track="handleTrack"到保存可选项目的元素。

  3. Put this div somewhere in your element: <div id="selectionBox" style="position:absolute; top:0; left:0; height:0; width:0; border:2px solid #000; background-color:rgba(128, 128, 128, 0.3); z-index:999;"></div> 将此div放在元素中的某个位置: <div id="selectionBox" style="position:absolute; top:0; left:0; height:0; width:0; border:2px solid #000; background-color:rgba(128, 128, 128, 0.3); z-index:999;"></div>

Then, add these functions to your element: 然后,将以下功能添加到您的元素中:

handleTrack: function(e) {
    switch(e.detail.state) {
        case "start":
            this.x1 = e.detail.x;
            this.y1 = e.detail.y;
            this.drawRectangle(this.x1, this.y1, this.x2, this.y2);
            break;
        case "track":
            this.x2 = e.detail.x;
            this.y2 = e.detail.y;
            this.drawRectangle(this.x1, this.y1, this.x2, this.y2);
            break;
        case "end":
            this.x2 = e.detail.x;
            this.y2 = e.detail.y;
            this.drawRectangle(0, 0, 0, 0);
            this.selectRectangle(e);
        break;
    }
},
drawRectangle: function(x1, y1, x2, y2) {
    this.$.selectionBox.style.left = x1 + 'px';
    this.$.selectionBox.style.top = y1 + 'px';
    this.$.selectionBox.style.width = (x2 - x1) + 'px';
    this.$.selectionBox.style.height = (y2 - y1) + 'px';
},
selectRectangle: function(e) {
    var tol = 20;
    var ironSelectors = Polymer.dom(e.currentTarget).querySelectorAll("iron-selector");
    ironSelectors.forEach(function(selector) {
        selector.items.forEach(function(i) {
            var el = i.getBoundingClientRect();
            if ((el.left+tol >= this.x1) && (el.top+tol >= this.y1) && (el.right-tol <= this.x2) && (el.bottom-tol <= this.y2)) {
                selector.select(i.value);
            }
        }.bind(this));
    }.bind(this));
}

This code also works if you have multiple iron-selector . 如果您有多个iron-selector则此代码也适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 聚合物:铁选择剂问题 - Polymer: iron-selector issue 单击铁选择器中的元素时,防止铁选择被触发 - Prevent iron-select to be fired when click on element in iron-selector dom 更改元素顺序后,聚合物 2 铁选择器所选元素不会更新 - Polymer 2 iron-selector selected element doesn't update after the dom changes order of elements 如何将鼠标拖过多个输入或带有 contentEditable 的元素到 select 里面的文本? - How can I drag the mouse across multiple inputs or elements with contentEditable to select the text inside? 聚合物铁选择器使dom.repeat中的this.push()混乱 - polymer iron-selector messing up this.push() in dom-repeat 如何使用鼠标拖动选项在jQuery数据表中选择多行 - How to select the multiple rows in jQuery datatable using the mouse drag option 如何为HTML元素创建鼠标拖动滑块? - How to create a mouse drag slider for HTML elements? 使用查询选择器Alll选择多个元素-JavaScript - Select Multiple Elements with Query Selector Alll - JavaScript Select canvas 网格中的多个单元格使用鼠标拖动 - Select multiple cells in grid of canvas using a mouse drag 如何为触摸和鼠标(拖动)事件制作可拖动元素 - How to make a draggable elements for touch and mouse(drag) events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM