最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Execute row click using listener on primefaces datatable - Stack Overflow

programmeradmin4浏览0评论

Problem is short. I have created a p:datatable but within the p:column i actually have a div element. Unfortunately the datatable should have selectable rows and the div just won't cooperate ;). So the solution is to call it manually, on the div element there is onclick listener, but how should I call the rowSelection of the datatable? Is there some list of the functions of Primefaces' elements?

the code:

<p:dataTable var="user" value="#{rec.friends}" rowKey="#{user.id}" widgetVar="friendscrollist"
        rendered="#{not empty rec.friends}" scrollable="true" rowIndexVar="findex"
        scrollHeight="500" scrollWidth="220" selectionMode="single" selection="#{rec.chosenFriend}" styleClass="friendscroll">
                <p:column width="198" id="friend#{findex}">
    <div class="friendlist" onclick="friendscrollist.clickRow(#{findex})" />
                </p:column>
                <p:ajax update=":leftform" event="rowSelect" />
                <p:ajax update=":leftform" event="rowUnselect" />
</p:dataTable>

Of course it is a simplified version, only things u need. So the question is what to call in the div onclick?

Problem is short. I have created a p:datatable but within the p:column i actually have a div element. Unfortunately the datatable should have selectable rows and the div just won't cooperate ;). So the solution is to call it manually, on the div element there is onclick listener, but how should I call the rowSelection of the datatable? Is there some list of the functions of Primefaces' elements?

the code:

<p:dataTable var="user" value="#{rec.friends}" rowKey="#{user.id}" widgetVar="friendscrollist"
        rendered="#{not empty rec.friends}" scrollable="true" rowIndexVar="findex"
        scrollHeight="500" scrollWidth="220" selectionMode="single" selection="#{rec.chosenFriend}" styleClass="friendscroll">
                <p:column width="198" id="friend#{findex}">
    <div class="friendlist" onclick="friendscrollist.clickRow(#{findex})" />
                </p:column>
                <p:ajax update=":leftform" event="rowSelect" />
                <p:ajax update=":leftform" event="rowUnselect" />
</p:dataTable>

Of course it is a simplified version, only things u need. So the question is what to call in the div onclick?

Share Improve this question asked Jan 12, 2013 at 15:49 AtaisAtais 11.3k7 gold badges74 silver badges115 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 13

the <p:dataTable widgetVar> has unselectAllRows() and selectRow(index) functions which are exactly what you need.

<div onclick="friendscrollist.unselectAllRows(); friendscrollist.selectRow(#{findex})">

There's unfortunately no documentation available for those functions, but in Chrome/Firebug you can see a list of all available functions in autocomplete list when you enter friendscrollist. in JS console. Below is a screen from Chrome:

Looking in the JS source code or common sense based on the function name should tell/hint you what those functions do.


Update: I stand corrected, a few of those functions are actually documented in PrimeFaces 3.4 User's Guide. They're in the "Client Side API" section of chapter 3.26 "DataTable", on page 146.

The accepted solution will work for a single selection table, but won't work for multiple selection. Another way to solve this problem that should work for both is to use the following instead of <div>

<h:panelGroup layout="block">

That will render a <div> inside your column, and will not interfere with the onclick event processing.

For pagination, you can control rows per page issue by this way:

friendscrollist.selectRow(#{rowIndex} - friendscrollist.paginator.getCurrentPage() * friendscrollist.paginator.cfg.rows);

I want to add another information for everybody who faces a similar issue with right click or context menu. You cannot capture right click event if you are using <p:graphicimage>. Somehow, oncontextmenu event of <img> tag is removed in primefaces.

If you want to select the row by right clicking an image content inside a row, you should use tag and oncontextmenu event.

<img src="..." oncontextmenu="dataTable.unselectAllRows();dataTable.selectRow(#{rowIndex});"/> 

If pagination is enabled you can do something like this:

onmouseup="friendscrollist.unselectAllRows();friendscrollist.selectRow(#{rowIndex} - friendscrollist.paginator.getCurrentPage() * 15);"

When 15 is the number of rows in your Table.

I didn't found a way to get the number of rows programatically...

发布评论

评论列表(0)

  1. 暂无评论