簡體   English   中英

從JQuery中的PrimeFaces數據表中查找特定單元格

[英]Find a particular cell from a PrimeFaces datatable in JQuery

我是Primeface和JQuery的新手。 我有一個數據表,我想在jquery中訪問該數據表的特定單元格。 以下是我想做的-

我的數據表代碼:

<p:dataTable id="employeeDataTable" var="employee" value="#{userPreferenceBean.employeeDataModel}"
            paginator="true" rows="10" selection="#{userPreferenceBean.selectedEmployeeList}" rowIndexVar="rowIndex">

            <f:facet name="header">
                List of Employees
            </f:facet>

            <p:column selectionMode="multiple" style="width:2%" />

            <p:column headerText="Primary Employee" style="width:2%">
                <p:commandButton value="Me" update=":#{p:component('primaryEmployeeDetails')}" id="ajax"
                    actionListener="#{userPreferenceBean.saveEmployee}" styleClass="ui-priority-primary">
                    <f:param name="employeeName" value="#{employee.name}" />
                </p:commandButton>
            </p:column>

            <p:column headerText="Name" style="width:48%">
                    #{employee.name}
            </p:column>

            <p:column headerText="Department" style="width:48%">
                    #{employee.department}
            </p:column>

            <f:facet name="footer">
                <p:commandButton id="submit" value="Save Preferences" icon="ui-icon-disk"
                    update=":#{p:component('selectedEmployeeDetails')}" />
            </f:facet>
        </p:dataTable>

現在,我想從JQuery的特定單元格(例如,第n行和第2列)訪問命令按鈕。 這就是我現在正在做的-

$(document).ready(function() {
        $(".ui-priority-primary").click(function() {
            alert('test')
            var row = 'employeeDataTable:' + rowIndex + ':ajax';
            $(row).each(function() {
                alert('test again')
            });
        });
});

現在,帶有“測試”的警報正在工作,但是帶有“再次測試”的警報不起作用。 這意味着我能夠從命令按鈕獲取click事件。 但是在我看來,我無法從數據表中獲取特定的單元格。 您能幫我了解我在這里犯什么錯誤嗎? 謝謝。

問候,Sudipta Deb

生成的HTML

假設您要使用rowIndex在行中獲取單元格,並使用columnIndex在列中獲取單元格。 因此,要通過jQuery獲取該單元格,您需要執行以下操作:

$(document.getElementById("myForm:employeeDataTable"))
    .find("tbody:first").children("tr:nth-of-type(" + rowIndex + ") td:nth-of-type(" + columnIndex + ")")

當您需要絕對ID時,最好給父母一個適當的名字,或者您可以使用document.getElementById("#{p:component('employeeDataTable')}")代替,因此primefaces會為您進行ID查找。
我使用document.getElementById代替了jQuery ID-selector # ,因為那樣的話,您必須對每個:進行轉義。


因此,對於給定的函數,請使用以下命令:

 var row = $(document.getElementById("myForm:employeeDataTable")).find("tbody:first").children("tr:nth-of-type(" + rowIndex + ")"); row.each(function() { alert('test again') }); 

test again提醒test again dataTable每一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM