Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

jsf - Why the primfaces datatable cant access the variable in a list<Object> that contains a list<Object>

My error is that I can't access the variables from my class part in the columns of a datatable

public class Blueprint  implements Serializable {
    private String name;
    private List<Part> parts;
    
    public Blueprint(String name, List<Part> parts) {
        this.name = name;
        this.parts = parts;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Part> getParts() {
        return parts;
    }

    public void setParts(List<Part> parts) {
        this.parts = parts;
    }

public class Part implements Serializable{

    public Part(String name, Integer amount) {
        this.name = name;
        this.amount = amount;
    }
    public String name;
    public Integer amount;

    public String getName() {
        System.out.println(name);
        return name;
    }

    public void setName(String _Name) {
        this.name = _Name;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmoung(Integer _Amount) {
        this.amount = _Amount;
    }


<p:panelGrid columns="1">

                        <p:dataTable id ="BlueprintList" value="#{Blueprints.blueprints}" var="blueprint" rowKey="#{blueprint.name}"  selectionMode="single" selection="#{Blueprints.selectedPart}">
                            <p:column style="width:16px">
                                <p:rowToggler />
                            </p:column>
                            <p:column headerText="Name">
                                <p:outputLabel value="#{blueprint.name}"/>
                            </p:column>

                            <p:rowExpansion>
                                <p:panelGrid columns="1"  >
                                    <p:dataTable value="#{blueprint.parts}" var="a">
                                        <p:column headerText="Name>
                                                <p:outputLabel value="#{a}" />
                                            </p:column>
                                            <p:column headerText="Anzahl">
                                                <p:outputLabel value="#{a}"/>
                                            </p:column>
                                   
                                    </p:dataTable>
                                </p:panelGrid>
                            </p:rowExpansion>
                            
                        </p:dataTable>

The problem is in when I want to access the name and amount over the var a it doesn't show up and if I write it manually, it doesn't work.

I hope somebody can help me. Thanks.

question from:https://stackoverflow.com/questions/65649580/why-the-primfaces-datatable-cant-access-the-variable-in-a-listobject-that-cont

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For the outer dataTable, the selection="..." clause needs to reference a blueprint because the value="..." clause is a list of blueprints. To get the selected part, you'll need to include the selection clauses on the inner datatable as well.

Where you use the variable {a}, you should just be able to include the field name as in #{a.name} and #{a.amount}. You didn't indicate which version of Primefaces or what development tool your using, but in my version of NetBeans v12.2, Netbeans won't do the field name lookup automatically in this case. I just had to type it in manually, and ignore the warning triangle.

I had issues getting the selectedPart to work, maybe because there was no command button to submit the form on my page. I resolved it with a p:ajax tag.

Here's my version. I tested it with Primefaces 8, running on Wildfly 21 and using Java 11.

<h:form>

    <p:dataTable id ="BlueprintList" value="#{rowExpansionController.blueprints}" 
                 var="blueprint" 
                 rowKey="#{blueprint.name}"  
                 selectionMode="single" selection="#{rowExpansionController.selectedBlueprint}"
                 rowExpandMode="single">
        <p:ajax event="rowSelect" listener="#{rowExpansionController.rowSelectBlueprint}" />
        <p:column style="width:16px">
            <p:rowToggler  />
        </p:column>
        <p:column headerText="Name">
            #{blueprint.name}
        </p:column>

        <p:rowExpansion>
            <p:dataTable value="#{blueprint.parts}" var="part" 
                         rowKey="#{part.name}" selectionMode="single" 
                         selection="#{rowExpansionController.selectedPart}">
                <p:ajax event="rowSelect" listener="#{rowExpansionController.rowSelectPart}" />
                <p:column headerText="Name">
                    #{part.name}
                </p:column>
                <p:column headerText="Amount">
                    #{part.amount}
                </p:column>

            </p:dataTable>
        </p:rowExpansion>

    </p:dataTable>

</h:form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...