简体   繁体   English

如何动态填写下拉菜单?

[英]How can I fill in a drop-down-menu dynamically?

I got some code (HTML and JavaScript) which creates a table with three columns and dynamic rows. 我得到了一些代码(HTML和JavaScript),它创建了一个包含三列和动态行的表。 I would like to have a drop-down-menu based on the entries of my first column. 我想根据我的第一栏的条目提供一个下拉菜单。 So the drop-down-menu should be filled after my table is completed. 因此,在我的表格完成后,应填写下拉菜单。 The first column can have multiple identical entries. 第一列可以有多个相同的条目。 So it is necessary to only show them once in my drop-down-menu. 所以有必要只在我的下拉菜单中显示一次。 At the moment I only have a static drop-down-menu. 目前我只有一个静态下拉菜单。 See code below. 见下面的代码。 The Program is working for static drop-down-menu very well. 该程序非常适用于静态下拉菜单。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <table id="myTable">
        <colgroup>
            <col width="150" style="background-color:red"></col>
            <col width="165"></col>
        </colgroup>
        <tr  style ="background-color:grey">
            <th>plane
                <select id="modelRangeDropdown" onchange="filterReports()">
                    <option selected="selected">All</option>
                    <option>number1</option>
                    <option>number2</option>                        
                </select>                   
            </th>   
            <th>Datum</th>
            <th>Secret</th>
        </tr>
        <xsl:for-each select="logstore/plane/trigger">
            <tr>
                <td><xsl:value-of select="../Name"/></td>
                <td><xsl:value-of select="date"/></td>
                <td><xsl:value-of select="secret"/></td>
            </tr>
        </xsl:for-each>
    </table>
    <script type="text/javascript" src="/../../../filterReports.js"></script>           
</body>
</html>
</xsl:template>
</xsl:stylesheet>

If you could use XSLT 2.0, you could use distinct-values and write the xsl:for-each like so 如果你可以使用XSLT 2.0,你可以使用distinct-values并像这样编写xsl:for-each

<select id="modelRangeDropdown" onchange="filterReports()">
    <option selected="selected">All</option>
    <xsl:for-each select="distinct-values(logstore/plane/Name)">
        <option value="{.}">
            <xsl:value-of select="." />
        </option>
    </xsl:for-each>
</select>  

On the other hand, if you are restricted to XSLT 1.0, you would need to make use of a technique called Muenchian Grouping . 另一方面,如果您仅限于XSLT 1.0,则需要使用一种名为Muenchian Grouping的技术。 You would define a key like so: 您可以像这样定义一个键:

<xsl:key name="planes" match="plane/Name" use="." />

Then, to get the distinct values, you would do this..... 然后,为了获得不同的值,你会这样做.....

<select id="modelRangeDropdown" onchange="filterReports()">
    <option selected="selected">All</option>
    <xsl:for-each select="logstore/plane/Name[generate-id() = generate-id(key('planes', .)[1])]">
        <option value="{.}">
            <xsl:value-of select="." />
        </option>
    </xsl:for-each>                    
</select>  

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

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