java 如何在 <p:dataTable primefaces 中获取选定的单元格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15254098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:00:21  来源:igfitidea点击:

how to get the selected cell in <p:dataTable primefaces?

javaajaxjsfprimefaces

提问by Karim Oukara

I want to get, if possible, the selected cell or colomne (id) of primefaces <p:dataTable

如果可能,我想获取素面的选定单元格或列(id) <p:dataTable

my table is like :

我的桌子是这样的:

<p:dataTable id="table" var="list" value="#{bean.list}" rowKey="#{list}" selectionMode="single" >
    <p:ajax event="rowSelect" listener="#{bean.onRowSelect}" />
    <p:column headerText="Date" >
        <h:outputText  value="#{list.SDate}" />
    </p:column>
    <p:column headerText="Name" >
        <h:outputText value="#{list.IName}" />
    </p:column>
</p:dataTable>

with this method I can get the row selected (line) using <p:ajax event="rowSelect" listener="#{bean.onRowSelect}" />but I can't get the selected colomn "Date" or "Name"

使用这种方法,我可以使用选定的行(行),<p:ajax event="rowSelect" listener="#{bean.onRowSelect}" />但无法获得选定的列“ Date”或“ Name

onrowSelecte method is like :

onrowSelecte 方法是这样的:

 public void onRowSelect(SelectEvent event)
        myObject obj = (myObject)event.getObject();
        //.......
 }

采纳答案by SRy

You can do something like this to get the value of the specific column

你可以做这样的事情来获取特定列的值

<p:dataTable id="firsttable" var="list" value="#{bean.list}" rowKey="#{list}" selectionMode="single" >
     <p:column headerText="Date" >
        <h:outputText  value="#{list.SDate}" />
    </p:column>
    <p:column headerText="Name" >
        <h:outputText value="#{list.IName}" />
    </p:column>
</p:dataTable>

// This is capture the value of selected column
<h:inputText id="selectedId" value="#{bean.selectedColumn}" style="display:none">
       <f:ajax listener="#{bean.onRowSelect}"></f:ajax>
</h:inputText>

This script captures the values of the selected row and sets the inputHidden

此脚本捕获所选行的值并设置 inputHidden

 jQuery.noConflict();
    $(window).load(function () {
         $(document).delegate("#firsttable td", "click", function (event) {
             var columnNumber = jQuery(this).index();//get index of clicked row
         var colval=jQuery(this).find('div span').text()); // get the column value
         $("#selectedId").val(colval); //set value in the inputtext
         $("#selectedId").change(); //this will trigger the ajax listener
       });
    });

And In the bean define property to get the input text value

并在 bean 中定义属性以获取输入文本值

 String selectedColumn;

 public void onRowSelect(AjaxBehaviorEvent event) {
         String value=getSelectedColumn();
         System.out.println(value);
}

回答by ?ɑ??????

Karim,

卡里姆,

I think you are forgetting add updateon your ajax event:

我认为您忘记在 ajax 事件中添加更新

<p:ajax event="rowSelect" listener="#{bean.onRowSelect}" update="table" />

On updateyou need put your table id or element that encapsulates your table.

update你需要把它封装了你的表的表ID或元素。

If the table is inside outputPanel, you can update it id instead the table.

如果表在 outputPanel 内,您可以更新它 id 而不是表。

The updateattribute it is necessary for send the screen information to your backBean.

update屏幕信息发送到您的 backBean 所必需的属性。

I hope it heps...

我希望它heps...

Good Luck!

祝你好运!

回答by Avinash Singh

I don't think primefaces has anything to select a column , you may need to add something like

我不认为 primefaces 有任何选择列,您可能需要添加类似

<h:outputText  value="#{list.SDate}" >
<f:ajax event="select" listener="#{bean.setSelectedColumn}"/>
</h:outputText>

Use event.getComponent() to further determine which column is selected

使用 event.getComponent() 进一步确定选择了哪一列

回答by J Slick

Regarding PrimeFaces DataTable, widgetVar, Javascript, jQuery:

关于 PrimeFaces DataTable、widgetVar、Javascript、jQuery:

I had trouble using Primefaces widgetVar to get the selected row of a PrimeFaces 3.5 DataTable. I looked through the PF source here...

我无法使用 Primefaces widgetVar 来获取 PrimeFaces 3.5 DataTable 的选定行。我在这里查看了 PF 源...

https://code.google.com/p/primefaces/source/browse/primefaces/trunk/src/main/resources/META-INF/resources/primefaces/datatable/datatable.js?r=10301

https://code.google.com/p/primefaces/source/browse/primefaces/trunk/src/main/resources/META-INF/resources/primefaces/datatable/datatable.js?r=10301

...and also used Chrome debugger to examine the DataTable, but did not find a getSelectedRow method. I probably missed something useful, but here is my hack, which works.

...并且还使用 Chrome 调试器来检查 DataTable,但没有找到 getSelectedRow 方法。我可能错过了一些有用的东西,但这是我的 hack,它有效。

var selectedRow = Array(); $('#idForm1\\:idDT tr.ui-state-highlight').each(function(i) { $(this).children('td').each(function(ii) { selectedRow.push($(this).text()); }); });

var selectedRow = Array(); $('#idForm1\\:idDT tr.ui-state-highlight').each(function(i) { $(this).children('td').each(function(ii) { selectedRow.push($(this).text()); }); });

Array selectedRow is populated with the <td>text values of the selected DataTable row.

数组 selectedRow 填充有<td>所选 DataTable 行的文本值。