我正在通过任何查询来创建动态表的任务,到目前为止,我已经完成了所有任务,但停留在选择行的位置,当选择任何行时,将显示第0行no及其值返回行。
托管bean是
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.PrimeFaces;
@ManagedBean(name="liveRangeService", eager = true)
@ApplicationScoped
public class LiveRangeService implements Serializable {
private List< Map<String, String> > tableData;
private Map<String, String> selectedData;
private List< Map<String, String> > filteredData;
private int Sr;
private List<String> tableHeaderNames;
private String tableColWidths;
private List< Map<String, String> > selectedRow;
private String RowSelected;
public String getRowSelected() {
return RowSelected;
}
public void setRowSelected(String RowSelected) {
this.RowSelected = RowSelected;
}
public Map<String, String> getSelectedData() {
return selectedData;
}
public void setSelectedData(Map<String, String> selectedData) {
this.selectedData = selectedData;
}
public List<Map<String, String>> getTableData() {
return tableData;
}
public List<String> getTableHeaderNames() {
return tableHeaderNames;
}
public LiveRangeService() {
}
public void LiveRangeServicesss(String Qry) {
System.out.println("Qry: " + Qry);
tableData = new ArrayList< Map<String, String> >();
tableHeaderNames = new ArrayList<String>();
tableHeaderNames.add("ID");
tableHeaderNames.add("Title");
tableHeaderNames.add("Opn_Amt");
tableHeaderNames.add("Smr_Amt");
{
Map<String, String> playlist = new HashMap<String, String>();
playlist.put("ID", "1010001");
playlist.put("Title", "Share Capital - Mr. 1");
playlist.put("Opn_Amt", "0");
playlist.put("Smr_Amt", "0");
tableData.add(playlist);
}
{
Map<String, String> playlist = new HashMap<String, String>();
playlist.put("ID", "1010002");
playlist.put("Title", "Share Capital - Mr. 2");
playlist.put("Opn_Amt", "0");
playlist.put("Smr_Amt", "0");
tableData.add(playlist);
}
{
Map<String, String> playlist = new HashMap<String, String>();
playlist.put("ID", "1010003");
playlist.put("Title", "Share Capital - Mrs. 3");
playlist.put("Opn_Amt", "0");
playlist.put("Smr_Amt", "0");
tableData.add(playlist);
}
PrimeFaces.current().ajax().update("form:dlgTBL");
PrimeFaces.current().executeScript("PF('dlg').show();");
}
public List<Map<String, String>> getSelectedRow() {
try {System.out.println("Selected Row! " + selectedRow.size());} catch (Exception e) {}
return selectedRow;
}
public void setSelectedRow(List<Map<String, String>> selectedRow) {
// System.out.println( "selected size: " + selectedRow.size() );
this.selectedRow = selectedRow;
}
public String getTableColWidths() {
return tableColWidths;
}
public void setTableColWidths(String tableColWidths) {
this.tableColWidths = tableColWidths;
}
public List<Map<String, String>> getFilteredData() {
return filteredData;
}
public void setFilteredData(List<Map<String, String>> filteredData) {
this.filteredData = filteredData;
}
public void onRowSelect() {
System.out.println("Select Table Values: " + this.Sr + ", " + this.RowSelected);
}
public int getSr() {
return Sr;
}
public void setSr(int Sr) {
this.Sr = Sr;
}
}
并且html部分是
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form id="form">
<p:messages>
<p:autoUpdate />
</p:messages>
<p:commandButton value="Update Grid" action="#{liveRangeService.LiveRangeServicesss('Select ID, Title, Opn_Amt, Smr_Amt From Coa32 Order By Title')}" id="cmdUpdate" />
<p:dialog id="dlgTBL" modal="true" showEffect="bounce" widgetVar="dlg" resizable="false">
<p:dataTable var="result" id="tbl" widgetVar="dtlTBL"
value="#{liveRangeService.tableData}"
paginator="false"
scrollable="true" rowIndexVar="rowIndex" scrollHeight="500"
scrollRows="50" liveScroll="true"
filterDelay="1100"
selectionMode="single" selection="#{liveRangeService.rowSelected}"
rowKey="#{liveRangeService.sr}"
>
<p:ajax event="rowSelect" listener="#{liveRangeService.onRowSelect}" />
<f:facet name="header">
<p:outputPanel layout="inline" styleClass="tabSpacer">
<h:outputText value="Global Filter:" />
<p:inputText id="globalFilter" onkeyup="PF('dtlTBL').filter()" style="width:150px;margin-left:10px;"/>
</p:outputPanel>
</f:facet>
<p:column width="10">
<f:facet name="header">
<h:outputText value="Sr." />
</f:facet>
<h:outputText value="#{rowIndex+1}" />
</p:column>
<p:columns value="#{liveRangeService.tableHeaderNames}"
var="mycolHeader"
width="#{colIndex==0?'10%':colIndex==1?'70%':colIndex==2?'10%':colIndex==3?'10%':'0'}"
columnIndexVar="colIndex"
sortBy="#{result[mycolHeader]}"
filterBy="#{result[mycolHeader]}"
filterMatchMode="contains"
>
<f:facet name="header">
<h:outputText value="#{mycolHeader}" />
</f:facet>
<h:outputText value="#{result[mycolHeader]}" />
<br />
</p:columns>
</p:dataTable>
</p:dialog>
</h:form>
</h:body>
</html>
我对设置行键的理解很困惑,也无法在表中设置Sr.固定列,因此代码始终在固定列中返回0。请提出一些解决方案。
尽管我仍然无法通过行选择来做到这一点,但是我通过将Sr No设置为按钮并通过该按钮调用行选择来解决了问题,我已经在上面上传了完整的代码。
<p:column width="50">
<f:facet name="header">
<h:outputText value="Sr." />
</f:facet>
<p:commandButton style="width: 49px" action="#{liveRangeService.onRowSelect(rowindex)}"/>
</p:column>
此外,由于过滤器和排序影响序列号,因此给出错误的行号,我在填充tableData之后设置了filtereddata;
filteredData=tableData;
[如果有人可以建议通过行选择而不是命令按钮进行所有选择,则我会很乐意。