2016-11-10 101 views
1

选择我有它的日期作为选项的下拉列表,从下拉列表中的最后一个选项设置:如何为使用D3.js

<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> 

我需要设置从最后日期列表(最新)作为选定的一个。我设法通过手动指定日期来做到这一点。那么,如果更多的日期将作为未来的选项出现在列表中,我该如何自动执行?

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

回答

1

你可以根据基准指数,而不是去做。让N = uniqueDates.length,然后用:

.property("selected", function(d, i) { return i == N-1; })