[英]How to set the last option from a drop-down list as selected using D3.js
I have a drop-down list with dates as options in it: 我有一个下拉列表,其中有日期作为选项:
<select id="dateSelector">
<option value="2016-01-01">Январь 2016</option>
<option value="2016-02-01">Февраль 2016</option>
<option value="2016-03-01">Март 2016</option>
</select>
I need to set the last date from the list (the most recent) as selected one. 我需要将列表中的最后一个日期(最新日期)设置为所选日期。 I've managed to do this by specifying the date manually. 我设法通过手动指定日期来做到这一点。 So how can I do it automatically in case if more dates will appear in the list as options in the future? 因此,如果将来有更多日期作为选项出现在列表中,我该如何自动执行呢?
d3.select("select#dateSelector")
.selectAll("option")
.data(uniqueDates)
.enter()
.append("option")
.text(function(d) {
return formatRU(new Date(d));
})
.attr("value", function(d) {
return d;
})
.property("selected", function(d) {
return d === "2016-03-01";
});
You could do it based on the index of the datum instead. 您可以改为基于基准的索引来进行。 Let N = uniqueDates.length
, then use: 令N = uniqueDates.length
,然后使用:
.property("selected", function(d, i) { return i == N-1; })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.