1

テーブルで選択した行を強調表示する方法を見つけようとしています。私のjspには、displaytagライブラリが作成している行のIDにアクセスできるjspスクリプトレットがあります。ユーザー ${currentNoteId} が選択した現在の行の ID と比較したいと思います。現在、行 ID = 849 (ハードコード) の場合、クラス「currentClass」がテーブルのその行だけに追加されます。{$currentNoteId} の 849 を変更する必要がありますが、その方法がわかりません。私はJava、Spring MVCを使用しています。jsp: ...

<%
request.setAttribute("dyndecorator", new org.displaytag.decorator.TableDecorator()  
{  
   public String addRowClass()  
   {  
      edu.ilstu.ais.advisorApps.business.Note row =               
      (edu.ilstu.ais.advisorApps.business.Note)getCurrentRowObject();      
            String rowId = row.getId();
            if ( rowId.equals("849") ) {
                return "currentClass";
            }
            return null;              
        }
    });
%> 

<c:set var="currentNoteId" value="${studentNotes.currentNote.id}"/>
...
<display:table id="noteTable" name="${ studentNotes.studentList }" pagesize="20"
  requestURI="notesView.form.html" decorator="dyndecorator">
    <display:column title="Select" class="yui-button-match" href="/notesView.form.html"
      paramId="note.id" paramProperty="id">
      <input type="button" class="yui-button-match2" name="select" value="Select"/>
    </display:column>
    <display:column property="userName" title="Created By" sortable="true"/>
    <display:column property="createDate" title="Created On" sortable="true" 
       format="{0,date,MM/dd/yy hh:mm:ss a}"/>
    <display:column property="detail" title="Detail" sortable="true"/>
</display:table>
...

これは JavaScript を使用して行うこともできますが、それが最善の方法かもしれませんが、ドキュメントではこれが推奨されていたので、試してみようと思いました。addRowClass() を使用した例は、行に既に存在するフィールド (ドキュメンテーションの例では金額が使用されています) との比較であるか、「849」ID のようにハードコーディングされていない限り、どこにも見つかりません。

ご協力いただきありがとうございます。

4

1 に答える 1

1

私は先に進み、代わりにjavascriptでそれを行いました。次のようにスクリプトレットで currentNoteId を使用した場合:

String rowId = row.getId();
String noteId = (String) pageContext.getAttribute("currentNoteId");
if ( rowId.equals( noteId ) ) {
    return "currentClass";
}
return null;

エラーを受け取りました: エラーが発生しました。別のメソッドで定義された内部クラス内の非最終変数 pageContext を参照できません。

だから代わりに私は書いた:

function highlightCurrentTableRow(tableId, currentRowId ) {
    var table = document.getElementById(tableId);
    var rows = table.getElementsByTagName("tr");
    console.log( "rowId", "'" + currentRowId + "'" );
    for (i = 1; i < rows.length; i++) {
        rowId = rows[i].getElementsByTagName("td")[0].innerHTML;
        console.log( "  rowId", "'" + rowId + "'" );
        if ( rowId == currentRowId ) {
            console.log( "got here" );
            var rowClass = rows[i].getAttribute("class");
            rows[i].setAttribute("class", rowClass + " currentClass"   );  
        };
    }
}

実際には、「クラス」がキーワードであるため、これは IE では機能しない可能性があるため、代わりに Yahoo YUI addClass(element, class) を使用したので、置き換えました

var rowClass = rows[i].getAttribute("class");
rows[i].setAttribute("class", rowClass + " currentClass"   ); 

YAHOO.util.Dom.addClass(rows[i],'currentClass');
于 2010-04-07T19:19:30.327 に答える