java - 打开新页面时排除第一列

标签 java javascript jsf jsf-2

我有一个带有行单击的 JSF 表。当我单击选择框时,将打开新页面。如何修改列 Select 以从选择中排除。

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"    
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <ui:insert name="header">           
            <ui:include src="header.xhtml"/>         
        </ui:insert>
        <script type="text/javascript">
            function addOnclickToDatatableRows() {
                //gets all the generated rows in the html table
                var trs = document.getElementById('form:dataTable').getElementsByTagName('tbody')[0]
                .getElementsByTagName('tr');
                //on every row, add onclick function (this is what you're looking for)
                for (var i = 0; trs.length > i; i++) {
                    trs[i].onclick = new Function("rowOnclick(this)");
                }
            }

            function rowOnclick(tr) {   
                var elements = tr.cells[0].childNodes;
                for(var i = 0; elements.length > i; i++) {
                    if ((typeof elements[i].id !== "undefined") &amp;&amp;
                    (elements[i].id.indexOf("lnkHidden") > -1)) {
                        //opne in a new window//  window.open(elements[i].href);
                        location.href=elements[i].href
                        break;
                    }
                }
                return false;
            }
        </script>
    </h:head>
    <h:body  onload="addOnclickToDatatableRows();">
        <!-- Demo click on row -->
        <h1><img src="resources/css/images/icon.png" alt="DX-57" /> History Center</h1>
        <!-- layer for black background of the buttons -->
        <div id="toolbar" style="margin: 0 auto; width:1180px; height:30px; position:relative;  background-color:black">
            <!-- Include page Navigation -->
            <ui:insert name="Navigation">           
                <ui:include src="Navigation.xhtml"/>         
            </ui:insert>

        </div>  

        <div id="logodiv" style="position:relative; top:35px; left:0px;"> 
            <h:graphicImage alt="Demo Click on row"  style="position:relative; top:-20px; left:9px;"  value="" />
        </div>
        <div id="main" style="margin: 0 auto; width:1190px; height:700px; position:absolute;  background-color:transparent; top:105px">

            <div id="mainpage" style="margin: 0 auto; width:1190px; height:500px; position:absolute;  background-color:transparent; top:80px">

                <div id="settingsHashMap" style="width:750px; height:400px; position:absolute;  background-color:r; top:20px; left:1px">

                    <h:form id="form">

                        <ui:debug />

                        <!-- The sortable data table -->
                        <h:dataTable id="dataTable" value="#{SessionsController.dataList}" binding="#{table}" var="item">
                            <!-- Check box -->
                            <h:column>
                                <f:facet name="header">
                                    <h:outputText value="Select" />
                                </f:facet>
                                <h:selectBooleanCheckbox onclick="highlight(this)" value="#{SessionsController.selectedIds[dataItem.id]}" />
                                <!-- Click on table code -->
                                <h:outputLink id="lnkHidden" value="AnotherPage.xhtml"
                                              style="display:none">
                                    <f:param name="id" value="#{item.aSessionID}" />
                                </h:outputLink>
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:commandLink value="№" actionListener="#{SessionsController.sort}">
                                        <f:attribute name="№" value="№" />
                                    </h:commandLink>
                                </f:facet>
                                <h:outputText value="#{table.rowIndex + SessionsController.firstRow + 1}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:commandLink value="Account Session ID" actionListener="#{SessionsController.sort}">
                                        <f:attribute name="sortField" value="Account Session ID" />
                                    </h:commandLink>
                                </f:facet>
                                <h:outputText value="#{item.aSessionID}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:commandLink value="User ID" actionListener="#{SessionsController.sort}">
                                        <f:attribute name="sortField" value="User ID" />
                                    </h:commandLink>
                                </f:facet>
                                <h:outputText value="#{item.userID}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:commandLink value="Activity Start Time" actionListener="#{SessionsController.sort}">
                                        <f:attribute name="sortField" value="Activity Start Time" />
                                    </h:commandLink>
                                </f:facet>
                                <h:outputText value="#{item.activityStart}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:commandLink value="Activity End Time" actionListener="#{SessionsController.sort}">
                                        <f:attribute name="sortField" value="Activity End Time" />
                                    </h:commandLink>
                                </f:facet>
                                <h:outputText value="#{item.activityEnd}" />
                            </h:column>
                            <h:column>
                                <f:facet name="header">
                                    <h:commandLink value="Activity" actionListener="#{SessionsController.sort}">
                                        <f:attribute name="sortField" value="Activity" />
                                    </h:commandLink>
                                </f:facet>
                                <h:outputText value="#{item.activity}" />
                            </h:column>
                        </h:dataTable>

                        <!-- The paging buttons -->
                        <h:commandButton value="first" action="#{SessionsController.pageFirst}"
                                         disabled="#{SessionsController.firstRow == 0}" >
                            <f:ajax render="@form" execute="@form"></f:ajax>
                        </h:commandButton>


                        <h:commandButton value="prev" action="#{SessionsController.pagePrevious}"
                                         disabled="#{SessionsController.firstRow == 0}" >
                            <f:ajax render="@form" execute="@form"></f:ajax>
                        </h:commandButton>

                        <h:commandButton value="next" action="#{SessionsController.pageNext}"
                                         disabled="#{SessionsController.firstRow + SessionsController.rowsPerPage >= SessionsController.totalRows}" >
                            <f:ajax render="@form" execute="@form"></f:ajax>
                        </h:commandButton>    

                        <h:commandButton value="last" action="#{SessionsController.pageLast}"
                                         disabled="#{SessionsController.firstRow + SessionsController.rowsPerPage >= SessionsController.totalRows}" >
                            <f:ajax render="@form" execute="@form"></f:ajax>
                        </h:commandButton>

                        <h:outputText value="Page #{SessionsController.currentPage} / #{SessionsController.totalPages}" />
                        <br />

                        <!-- The paging links -->
                        <ui:repeat value="#{SessionsController.pages}" var="page">
                            <h:commandLink value="#{page}" actionListener="#{SessionsController.page}"
                                           rendered="#{page != SessionsController.currentPage}" >
                                <f:ajax render="@form" execute="@form"></f:ajax>   
                            </h:commandLink>
                            <h:outputText value="#{page}" escape="false"
                                          rendered="#{page == SessionsController.currentPage}" />
                        </ui:repeat>
                        <br />

                        <!-- Set rows per page -->
                        <h:outputLabel for="rowsPerPage" value="Rows per page" />
                        <h:inputText id="rowsPerPage" value="#{SessionsController.rowsPerPage}" size="3" maxlength="3" />
                        <h:commandButton value="Set" action="#{SessionsController.pageFirst}" >
                            <f:ajax render="@form" execute="@form"></f:ajax>
                        </h:commandButton>
                        <h:message for="rowsPerPage" errorStyle="color: red;" />

                    </h:form>                  

                </div>   

                <div id="settingsdivb" style="width:350px; height:400px; position:absolute;  background-color:transparent; top:20px; left:800px">

                </div>   
            </div>  
        </div>

        <script type="text/javascript">
            $("tr").not(':first').hover(
            function () {
                $(this).css("background","#707070");
            }, 
            function () {
                $(this).css("background","");
            }
        );              

            function highlight(param) {  
                var row = jQuery(param).parent().parent().children();
                row.toggleClass('highlited');
            }        
        </script>


    </h:body>
</html>

最佳答案

只是一个愚蠢的答案,希望我得到你的要求。

你为什么不改变

for (var i = 0; trs.length > i; i++) {
    trs[i].onclick = new Function("rowOnclick(this)");
}

for (var i = 1; trs.length > i; i++) {
        trs[i].onclick = new Function("rowOnclick(this)");
    }

从 1 开始循环应从第二行分配 onClick 函数,避免将其绑定(bind)到第一行。

编辑:

我已经使用提供的 HTML 测试了该脚本,它似乎可以工作。 单击该复选框不会启动重定向,而单击其他列则会启动重定向。

在您的页面上进行测试并告诉我!

<script type="text/javascript">
        <!-- http://stackoverflow.com/questions/10566980/modify-javascript-to-exclude-first-column-from-opening-new-page -->

        function addOnclickToDatatableRows() {
            //gets all the generated rows in the html table
            var trs = document.getElementById('form:dataTable').getElementsByTagName('tbody')[0]
            .getElementsByTagName('tr');
            //on every row, add onclick function (this is what you're looking for)
            for (var i = 0; trs.length > i; i++) {
                var cells = trs[i].cells;                    
                for(var j=1; j<cells.length; j++){
                    cells[j].onclick = new Function("rowOnclick(this.parentElement)");
                }                    
            }
        }

        function rowOnclick(tr) {
                var elements = tr.cells[0].childNodes;
                for(var i = 0; elements.length > i; i++) {                           
                    if ((typeof elements[i].id != "undefined") && (elements[i].id.indexOf("lnkHidden") > -1)) {
                        //opne in a new window//  window.open(elements[i].href);
                        location.href=elements[i].href
                        break;
                    }
                }                
            return false;
        }
    </script>

关于java - 打开新页面时排除第一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565183/

相关文章:

java - 从 Object 转换为 boolean 有效的 Java 语言吗?

javascript - 在运行时向控制台方法添加动态值,并完整保留原始调用位置和行号

jsf - 通过 p :fileUpload mode=advanced 上传文件时传递另一个输入组件的值

JSF 2 : Accessing managed bean's instance in the phase listener object?

Java 字符串条件中断不起作用

java - 无法配置 spring boot 安全 - 总是 403

javascript - 循环遍历 Javascript 数组

html - JavaServer Faces 2.2 和 HTML5 支持,为什么 XHTML 仍在使用

Java 奇偶数递归求和

javascript - 点击注册 1d 战舰游戏。使用数组记录以前的用户输入,然后与当前输入进行交叉检查