选中复选框时如何突出显示整行

时间:2012-04-13 20:35:31

标签: java javascript jquery html jsf-2

我有一个JSF数据表。我想在选中相应的复选框时突出显示该行。我如何编辑JavaScript代码来实现这种效果?

<?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" src="resources/js/jquery-1.7.2.min.js"></script>
    </h:head>
    <h:body>

        <h1><img src="resources/css/images/icon.png" alt="NVIDIA.com" /> 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="greenBand" class="ui-state-default ui-corner-allh" style="position:relative; top:35px; left:0px;"> 
            <h:graphicImage alt="Dashboard"  style="position:relative; top:-20px; left:9px;"  value="resources/images/logo_sessions.png" />
        </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">

                        <!-- The sortable data table -->
                        <h:dataTable id="dataTable" value="#{SessionsController.dataList}" var="item">

                            <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}" />
                        <h:commandButton value="prev" action="#{SessionsController.pagePrevious}"
                                         disabled="#{SessionsController.firstRow == 0}" />
                        <h:commandButton value="next" action="#{SessionsController.pageNext}"
                                         disabled="#{SessionsController.firstRow + SessionsController.rowsPerPage >= SessionsController.totalRows}" />
                        <h:commandButton value="last" action="#{SessionsController.pageLast}"
                                         disabled="#{SessionsController.firstRow + SessionsController.rowsPerPage >= SessionsController.totalRows}" />
                        <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}" />
                            <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}" />
                        <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","#787878");
              }, 
              function () {
                $(this).css("background","");
              }
            );
        </script>


    </h:body>
</html>

3 个答案:

答案 0 :(得分:5)

我无法确定你的复选框在哪里或者他们的名字是什么,但我确实注意到你在脚本中使用了jQuery。我创建了一个快速的小 jsFiddle demo 来向您展示如何使用jQuery。在我的示例中,我创建了一个.highlight类,我将background-color设置为我想要的颜色。在jQuery中,我将所有复选框分组并附加一个 .change() 处理程序,我会在每次点击时打开/关闭高亮类。

<强> Check out the jsFiddle demo

HTML:

<div id="row-1"><input type="checkbox" id="chk-1" />Row 1</div>
<div id="row-2"><input type="checkbox" id="chk-2" />Row 2</div>
<div id="row-3"><input type="checkbox" id="chk-3" />Row 3</div>
<div id="row-4"><input type="checkbox" id="chk-4" />Row 4</div>

CSS:

div
{
    display: block;
    height: 20px;
    padding: 20px;
    border-bottom: dashed 1px #000;
}

input
{
    margin-right: 10px;
}

.highlight
{
    background-color: yellow;
}

jQuery的:

$("input[type=checkbox]").on("change", function() {

    var $chk = $(this),
        num = $chk.attr("id").substring(4),
        $row = $("#row-" + num);

    $row.toggleClass("highlight");

});

输出:

enter image description here

答案 1 :(得分:4)

我的jQuery方法是这样的:

            <p:column >
                <h:selectBooleanCheckbox onclick="highlight(this)" />
            </p:column>

在CSS文件中创建.highlighted类:

.highlighted {
    background-color: red;
}

最后是实际功能:

          function highlight(param) {  
            var row = jQuery(param).parent().parent(); //children() available as well
                row.toggleClass('highlighted');
          } 

您只需获得所点击row的{​​{1}}并处理checkbox的分配。直接和简单。

编辑:当然CSS class的数量取决于您的html元素组成。编辑了适合你的情况的功能,我尝试了不同的元素组成。

答案 2 :(得分:3)

以下是我在选中复选框时让Javascript更改背景颜色的示例:

<html>
    <body>
        <table>
            <tr id="changeme1">
                <td><input type="checkbox" onclick="highlight('changeme1');" /></td>
                <td>Test Box</td>
            </tr>
            <tr id="changeme2">
                <td><input type="checkbox" onclick="highlight('changeme2');" /></td>
                <td>Test2</td>
            </tr>
        </table>
        <script>
        function highlight(id)
        {
            object = document.getElementById(id).style.backgroundColor;
            if(object == "yellow")
            {
                document.getElementById(id).style.backgroundColor = "white";
            }else{
                document.getElementById(id).style.backgroundColor = "yellow";
            }
        }
        </script>
    </body>
</html>