简体   繁体   English

将所选元素移动到最后

[英]Move selected element to the end

We have HTML code 我们有HTML代码

<select id="mySelect">
 <optgroup label="First group" id="firstGrp">
   <option value="One">One</option>
   <option value="Two">Two</option>
</optgroup>
 <optgroup label="Second group" id="secondGrp">
   <option value="Three">Three</option>
   <option value="Six">Six</option>
   <option value="Four">Four</option>
   <option value="Five">Five</option>
 </optgroup>
</select>

I want to move <option value="Six">Six</option> to the end of <optgroup label="Second group" id="secondGrp"> . 我想将<option value="Six">Six</option><optgroup label="Second group" id="secondGrp">

So, I select necessary element and remove it. 所以,我选择必要的元素并将其删除。

var secondGrp = d3.selectAll("#secondGrp option");
var six = secondGrp.filter(function(d, i){ return d === "Six")})
six.remove();

Unfortunately, I have no idea how to add it at the end of the <option value="Six">Six</option> . 不幸的是,我不知道如何在<option value="Six">Six</option>的末尾添加它。

I'm late to this party, but I'd like to add to this conversation the new lower() and raise() functions, introduced in D3 4.0. 我迟到了这个派对,但是我想在这个对话中添加D3 4.0中引入的新的lower()raise()函数。 Its very simple: 它非常简单:

selection.raise() Re-inserts each selected element, in order, as the last child of its parent. selection.raise()按顺序重新插入每个选定元素作为其父元素的最后一个子元素。

and: 和:

selection.lower() Re-inserts each selected element, in order, as the first child of its parent. selection.lower()按顺序重新插入每个选定元素作为其父元素的第一个子元素。

So, all you need to do is this: 所以,你需要做的就是:

d3.select("[value='Six']").raise();

Check this fiddle, you can click the buttons and see it going up and down: https://jsfiddle.net/gerardofurtado/omk8r7dh/ 检查这个小提琴,你可以点击按钮,看看它上下: https//jsfiddle.net/gerardofurtado/omk8r7dh/

The names here are a bit confusing, because "raise" will make "six" go down in the list, and "lower" will make it go up. 这里的名字有点令人困惑,因为“加注”会使“六”在列表中下降,“下限”会使它上升。

As an alternative to the answer by echonax you could just re-append the removed DOM element without the need to create and append a new one. 作为echonax 答案的替代方案,您只需重新附加已删除的DOM元素,而无需创建和追加新元素。 This comes in handy if there are many attributes on this element. 如果此元素有许多属性,这会派上用场。

d3.select("#secondGrp").append(function() {
  return d3.select("option[value=Six]").remove().node();
});

 d3.select("#secondGrp").append(function() { return d3.select("option[value=Six]").remove().node(); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <select id="mySelect"> <optgroup label="First group" id="firstGrp"> <option value="One">One</option> <option value="Two">Two</option> </optgroup> <optgroup label="Second group" id="secondGrp"> <option value="Three">Three</option> <option value="Six">Six</option> <option value="Four">Four</option> <option value="Five">Five</option> </optgroup> </select> 

This make use of the fact, that selection.remove() will return the selection of elements which got removed. 这利用了一个事实,即selection.remove()将返回被删除的元素的选择。 This is the use case it was designed for: 这是它的用例:

however, you can pass a function to selection.append or selection.insert to re-add elements. 但是,您可以将函数传递给selection.append或selection.insert以重新添加元素。

Here's the result: https://jsfiddle.net/bL6aavxr/1/ 结果如下: https//jsfiddle.net/bL6aavxr/1/

You can use an attribute selector to remove the value like this, no need for filter: 您可以使用属性选择器删除这样的值,不需要过滤器:

d3.select("[value='Six']").remove();

then you can "append" your desired option with whatever attributes and text you like: 那么你可以用你喜欢的任何属性和文字“追加”你想要的选项:

d3.select("#secondGrp").append('option').attr("value", "Six").html("Six");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM