动态地将id分配给div

时间:2017-02-03 11:47:55

标签: java html jsp

我有这个班级

public class Cluster {
    private int clusterId = -1;

    private DataPoint centroid;
    private ArrayList<DataPoint> points;

    private boolean selected = false;


    private float clusterPercentage = -1;
    private int clusterValue = -1;

    public Cluster(int id, DataPoint centroid) {
        ...
    }
}

在我的聚类算法完成后,它会创建几个聚类,这些聚类存储在ArrayList中,如下所示:

ArrayList<Cluster> my_clusters;
my_clusters.add(new Cluster(...));

所有群集的clusterPercentageclusterValue

都有一些值

我正在进行互动。因此,我设置了Tomcat Server 8并在.jsp

中显示此模型

我要做的是能够打印出每个群集已计算的数据,并允许用户更改每个群集的selected变量。

为此,我在Cluster ArrayList上进行迭代并打印出来。

for (int i = 0; i < km.k; ++i) {
        %>
                <div class="col-md-4">
                    <div class="thumbnail">
                        <div class="caption">
                            <p><strong>Cluster</strong> <%=cluster_id %>: <%=cluster_size%> points. <br>
                            Percentage: <%=df4.format(cluster_percentage)%> % <br>
                            Expressed value: <%=cluster_value %> <br>
                            Filter test: <%
                            if (cluster_filter) {
                                %><strong>PASS</strong><%
                            }
                            else {
                                %>FAIL<%
                            }
                            %><br><hr>
                            Sample: <%=sample_id%><br>
                            Value: <%=sample_highest_value%><br>
                            Expr. percentage: 
                            <%
                            if (sample_highscore >= threshold) {
                                %><strong><%=df4.format(sample_highscore)%> %</strong><%
                            }
                            else {
                                %><%=df4.format(sample_highscore)%> %<%
                            }
                            %>
                            <div class="checkbox">
                              <label><input type="checkbox" value="">Option 1</label>
                            </div>
                        </div>
                    </div>
                </div>
                <%
        } %>

所以我的问题是:

有没有办法动态地为我的checkbox divs分配ID,以便我能够知道选择了哪个复选框?当然,div's必须与clusterId匹配。

2 个答案:

答案 0 :(得分:2)

您可以使用相同的 cluster_id 为div创建动态ID。

<div class="checkbox" id="<%=cluster_id %>_div">

答案 1 :(得分:0)

你可以输入迭代器号来计算复选框的数量并用id附加它(如果是id)。

public class MyCheckBoxCell extends TreeCell<String> {

    private final CheckBox checkBox = new CheckBox();

    private BooleanProperty currentSelectedBinding ;

    // only need this if you are using the indeterminateProperty() of your
    // CheckBoxTreeItems
    private BooleanProperty currentIndeterminateBinding ;

    public MyCheckBoxCell() {

        // add extra event handling to the check box here...

    }

    @Override
    protected void updateItem(String item, boolean empty) {

        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            setText(item);
            setGraphic(checkBox);
            if (currentSelectedBinding != null) {
                checkBox.selectedProperty().unbindBidirectional(currentSelectedBinding);
            }
            if (currentIndeterminateBinding != null) {
                checkBox.indeterminateProperty().unbindBidirectional(currentIndeterminateBinding);
            }
            if (getTreeItem() instanceof CheckBoxTreeItem) {
                CheckBoxTreeItem cbti = (CheckBoxTreeItem<?>) getTreeItem();
                currentSelectedBinding = cbti.selectedProperty();
                checkBox.selectedProperty().bindBidirectional(currentSelectedBinding);
                currentIndeterminateBinding = cbti.indeterminateProperty();
                checkBox.indeterminateProperty().bindBidirectional(currentIndeterminateBinding);
            }
        }
    }
}

我希望它可以帮助你