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

问题描述 投票:0回答:2

如何通过单击,按住并拖动鼠标,甚至用鼠标绘制矩形,并选择其下的所有项目,在iron-selector中选择多个项目?

我使用Polymer 1.11.3和iron-selector 2.1.0,并且阅读文档没有提供明显的解决方案。

这是我想要启用拖动选择的实际元素:

enter image description here

我的目标是能够点击例如在星期日7,将鼠标拖动到15,释放单击,并选择7-15。

javascript polymer iron-elements
2个回答
0
投票

您可以使用iron-selector属性在multi中选择多个项目:

 <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财产的数组。

DEMO


0
投票

为了能够选择我的鼠标单击并拖动,请执行以下操作:

  1. 将css属性user-select: none;设置为包含可选项的元素。
  2. on-track="handleTrack"添加到包含可选项的元素中。
  3. 将这个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>

然后,将这些函数添加到您的元素:

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));
}

如果您有多个iron-selector,此代码也适用。

© www.soinside.com 2019 - 2024. All rights reserved.