简体   繁体   English

如何获取所选单元格

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

I want to get, if possible, the selected cell or colomne (id) of primefaces <p:dataTable 如果可能的话,我想获得主要面的选定单元格或colomne(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}" />来选择行(行),但我无法获得所选的colomn“ Date ”或“ Name

onrowSelecte method is like : onrowSelecte方法如下:

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

Karim, 卡里姆

I think you are forgetting add update on your ajax event: 我想你忘了在你的ajax事件上添加更新

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

On update you 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中,则可以更新它而不是表。

The update attribute it is necessary for send the screen information to your backBean. update属性是将屏幕信息发送到backBean所必需的。

I hope it heps... 我希望它能够......

Good Luck! 祝好运!

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()进一步确定选择了哪个列

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的define属性中获取输入文本值

 String selectedColumn;

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

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. 我无法使用Primefaces widgetVar来获取PrimeFaces 3.5 DataTable的选定行。 I looked through the PF source here... 我在这里浏览了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. ...还使用Chrome调试器来检查DataTable,但没有找到getSelectedRow方法。 I probably missed something useful, but here is my hack, which works. 我可能错过了一些有用的东西,但这是我的黑客,它有效。

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. Array selectedRow使用所选DataTable行的<td>文本值进行填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM