数据表中没有选定的行显示在带有primefaces的datalist中

时间:2011-12-01 17:05:47

标签: select datatable primefaces row

如果我在datatable的复选框中选择一些行并按“查看”按钮。它将在数据主义者中展示。但是在数据主义者中没有任何东西。它表明,“selectedServers”没有存储数据表中的选定行。

我需要帮助!

list.xhtml

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.prime.com.tr/ui"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">

    <h:head>
        <link rel="stylesheet" media="screen" href="style/style.css" />
    </h:head>
    <h:body>
    <div id="container">
        <div id="header">
            <p:panel id="headerpanel">
            <f:facet name="header">
                    <h:outputText value="cloudMon"/>
                </f:facet>
            </p:panel>
        </div>

        <div id="mainnav">
            <h:form>
                <p:dataTable
                    var="gerate"
                    value="#{tableBean.server}"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                    paginator="true"
                    rows="10"
                    rowsPerPageTemplate="5,10,15" 
                    emptyMessage="es gibt keine anpassende Elemente" 
                    selection="#{tableBean.selectedServers}">

                    <f:facet name="header">
                        <p:outputPanel>
                            <h:outputText value="suchen:" />
                            <p:inputText id="globalFilter" onkeyup="carsTable.filter()" style="width: 100px" />
                        </p:outputPanel>
                    </f:facet>

                    <p:column selectionMode="multiple" />

                    <p:column sortBy="#{gerate.owner}"
                        headerText="gerate_owner" 
                        filterBy="#{gerate.owner}"
                        filterMatchMode="contains" >

                        <h:outputText value="#{gerate.owner}"/>
                    </p:column>

                    <p:column sortBy="#{gerate.year}" 
                        headerText="gerate.year" 
                        filterBy="#{gerate.year}"
                        filterMatchMode="startsWith" >

                        <h:outputText value="#{gerate.year}"/>
                    </p:column>


                    <f:facet name="footer">
                        <p:commandButton value="view" 
                            update="displayMulti"
                            oncomplete="multiGeraeteDialog.show()"/>

                        insgesamt #{fn:length(tableBean.server)} geraete
                    </f:facet>
                </p:dataTable>

                <p:dialog header="geraete anzeigen" 
                    widgetVar="multiGeraeteDialog"
                    width="300" height="300" 
                    showEffect="explode" hideEffect="explode">
                            ajlkdjflasjl
                    <p:dataList id="displayMulti"                                   
                        value="#{tableBean.selectedServers}"
                        var="select">
                               aa: #{select.owner}
                    </p:dataList>

                </p:dialog>
            </h:form>
        </div> 

        <div id="menu">
            <p:panel>
                <h:outputText value="Menu" />
        </p:panel>
        </div>

        <div id="contents">
            <p:panel header="Hello World">
                <h:outputText value="Hello!" />
        </p:panel>
        </div>

        <div id="footer">
            <p:themeSwitcher initialText="Change Skin" buttonPreText="Skin: " />
        </div>
    </div>
    </h:body>
</html>

TableBean.java

package tud.cloudmon;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "tableBean")
@RequestScoped
public class TableBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2160613756770646562L;

    private final static String[] colors;

    private final static String[] manufacturers;
    private final static String[] owner;

    static {
        colors = new String[10];
        colors[0] = "Black";
        colors[1] = "White";
        colors[2] = "Green";
        colors[3] = "Red";
        colors[4] = "Blue";
        colors[5] = "Orange";
        colors[6] = "Silver";
        colors[7] = "Yellow";
        colors[8] = "Brown";
        colors[9] = "Maroon";

        manufacturers = new String[10];
        manufacturers[0] = "IBM";
        manufacturers[1] = "Fujitsu";
        manufacturers[2] = "BenQ";
        manufacturers[3] = "Samsung";
        manufacturers[4] = "Oracle";
        manufacturers[5] = "HP";
        manufacturers[6] = "Dell";
        manufacturers[7] = "Packard Bell";
        manufacturers[8] = "USRobotics";
        manufacturers[9] = "Intel";

        owner = new String[10];
        owner[1]="Google";
        owner[2]="Amazon";
        owner[3]="Private";
        owner[4]="Private";
        owner[5]="SETI";
        owner[6]="Google";
        owner[7]="Google";
        owner[8]="Google";
        owner[9]="Amazon";
        owner[0]="Amazon";
    }

    private List<Server> server;

    private List<Server> servers;

    private Server selectedServer;

    private Server[] selectedServers;

    public TableBean() {
        server = new ArrayList<Server>();

        populateRandomServer(server, 50);
    }

    public Server[] getSelectedServers(){
        return selectedServers;
    }

    public void setSelectedServers(Server[] selectedServers){
        this.selectedServers = selectedServers;
    }

    public Server getSelectedServer() {
        return selectedServer;
    }

    public void setSelectedServer(Server selectedServer) {
        this.selectedServer = selectedServer;
    }

    private void populateRandomServer(List<Server> list, int size) {
        for(int i = 0 ; i < size ; i++)
        list.add(new Server(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomOwner()));
    }

    public List<Server> getServer() {
        return server;
    }

    public List<Server> getServers(){
        return servers;
    }

    private int getRandomYear() {
        return (int) (Math.random() * 50 + 1960);
    }

    private String getRandomOwner() {
        return owner[(int) (Math.random() * 10)];
    }

    private String getRandomManufacturer() {
        return manufacturers[(int) (Math.random() * 10)];
    }

    private String getRandomModel() {
        return UUID.randomUUID().toString().substring(0, 8);
    }
}

1 个答案:

答案 0 :(得分:0)

**I had the same problem. Nightmare. Thought primefaces was easy to use.
Try this:
Use 2 forms with the attribute <h:form prependId="false">
That worked for me.
Good luck**

<?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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.prime.com.tr/ui"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">

    <h:head>
        <link rel="stylesheet" media="screen" href="style/style.css" />
    </h:head>
    <h:body>
    <div id="container">
        <div id="header">
            <p:panel id="headerpanel">
            <f:facet name="header">
                    <h:outputText value="cloudMon"/>
                </f:facet>
            </p:panel>
        </div>

        <div id="mainnav">
            <h:form prependId="false">
                <p:dataTable
                    var="gerate"
                    value="#{tableBean.server}"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
                    paginator="true"
                    rows="10"
                    rowsPerPageTemplate="5,10,15" 
                    emptyMessage="es gibt keine anpassende Elemente" 
                    selection="#{tableBean.selectedServers}">

                    <f:facet name="header">
                        <p:outputPanel>
                            <h:outputText value="suchen:" />
                            <p:inputText id="globalFilter" onkeyup="carsTable.filter()" style="width: 100px" />
                        </p:outputPanel>
                    </f:facet>

                    <p:column selectionMode="multiple" />

                    <p:column sortBy="#{gerate.owner}"
                        headerText="gerate_owner" 
                        filterBy="#{gerate.owner}"
                        filterMatchMode="contains" >

                        <h:outputText value="#{gerate.owner}"/>
                    </p:column>

                    <p:column sortBy="#{gerate.year}" 
                        headerText="gerate.year" 
                        filterBy="#{gerate.year}"
                        filterMatchMode="startsWith" >

                        <h:outputText value="#{gerate.year}"/>
                    </p:column>


                    <f:facet name="footer">
                        <p:commandButton value="view" 
                            update="displayMulti"
                            oncomplete="multiGeraeteDialog.show()"/>

                        insgesamt #{fn:length(tableBean.server)} geraete
                    </f:facet>
                </p:dataTable>
</h:form>
<h:form prependId="false">
                <p:dialog header="geraete anzeigen" 
                    widgetVar="multiGeraeteDialog"
                    width="300" height="300" 
                    showEffect="explode" hideEffect="explode">
                            ajlkdjflasjl
                    <p:dataList id="displayMulti"                                   
                        value="#{tableBean.selectedServers}"
                        var="select">
                               aa: #{select.owner}
                    </p:dataList>

                </p:dialog>
            </h:form>
        </div> 

        <div id="menu">
            <p:panel>
                <h:outputText value="Menu" />
        </p:panel>
        </div>

        <div id="contents">
            <p:panel header="Hello World">
                <h:outputText value="Hello!" />
        </p:panel>
        </div>

        <div id="footer">
            <p:themeSwitcher initialText="Change Skin" buttonPreText="Skin: " />
        </div>
    </div>
    </h:body>
</html>