动态创建的 RowSelection p:datatable return 总是第 0 行
RowSelection in dynamically created p:datatable return 0th row always
我正在处理动态table的任务,从任何查询中,我都完成了所有任务不显示并返回该行中的值。
托管 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>
我对设置行键的理解很混乱,也无法在 table 中设置 Sr. 固定列,代码总是 returns 固定列中的 0。请提出一些解决方案。
虽然我仍然无法通过第 selection 行完成此操作,但我通过将 Sr No 作为按钮并通过按钮调用第 select 行来解决了这个问题,我有上面上传了完整的代码。
<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=tableData;
如果有人能建议正确的方法来完成所有操作,我会很高兴 selection 而不是命令按钮。
我正在处理动态table的任务,从任何查询中,我都完成了所有任务不显示并返回该行中的值。
托管 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>
我对设置行键的理解很混乱,也无法在 table 中设置 Sr. 固定列,代码总是 returns 固定列中的 0。请提出一些解决方案。
虽然我仍然无法通过第 selection 行完成此操作,但我通过将 Sr No 作为按钮并通过按钮调用第 select 行来解决了这个问题,我有上面上传了完整的代码。
<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=tableData;
如果有人能建议正确的方法来完成所有操作,我会很高兴 selection 而不是命令按钮。