2
votes

i have a problem using primefaces datatable with multiple selection ( checkbox ) with composite primary key below is my xhtml

 <p:dataTable id="cars" var="car" value="#{setujutolakanpencairan.listmktksppencairan0}"
                     rowKey="#{car.mktksppencairan0PK}"
                     selection="#{setujutolakanpencairan.mktksppencairan0s}"   >
            <p:ajax event="rowSelectCheckbox" process="@this"/>
            <p:column selectionMode="multiple" style="width:2%" />
            <f:facet name="header">
                Click "View" button after selecting multiple to see details
            </f:facet>
            <p:column headerText="Color">
                #{car.vcKeterangan}
            </p:column>

            <f:facet name="footer">
                <p:commandButton value="View" icon="ui-icon-search"
                                 update=":form:display" oncomplete="carDialog.show()" actionListener="#{setujutolakanpencairan.outprint()}"/>
            </f:facet>
        </p:dataTable>

Below is my back bean

private List<Mktksppencairan0> listmktksppencairan0;
private Mktksppencairan0[] mktksppencairan0s;

below is my getter setter

public Mktksppencairan0[] getMktksppencairan0s() {
    if (mktksppencairan0s != null) {

        for (Object asdf : mktksppencairan0s) {
            System.out.println("mktksppencairan0s mktksppencairan0s mktksppencairan0smktksppencairan0s =====" + asdf);
        }
    }
    return mktksppencairan0s;
}

public void setMktksppencairan0s(Mktksppencairan0[] mktksppencairan0s) {
    this.mktksppencairan0s = mktksppencairan0s;
}

below is list that display data

public List<Mktksppencairan0> getListmktksppencairan0() {
    TypedQuery<Mktksppencairan0> query = em.createNamedQuery("Mktksppencairan0.findBybulantahun", Mktksppencairan0.class);
    query.setParameter(
            "iTahun", mktkontrol.getTahun());
    query.setParameter(
            "iBulan", mktkontrol.getBulan());
    List<Mktksppencairan0> results = query.getResultList();
    return results;
}

public void setListmktksppencairan0(List<Mktksppencairan0> listmktksppencairan0) {
    this.listmktksppencairan0 = listmktksppencairan0;
}

here is my action that i used to debug the value

 public void outprint() {

    System.out.println("asdfasdf ===" + mktksppencairan0s.toString().length());
    System.out.println("asdfasdf ===" + mktksppencairan0s.toString());
    System.out.println("asdfasdf ===" + mktksppencairan0s);
    for (Mktksppencairan0 asdf : mktksppencairan0s) {
        System.out.println("mktksppencairan0s mktksppencairan0s mktksppencairan0smktksppencairan0s =====" + asdf.toString().length());
    }
}

when i debug using netbeans debugger it is result [] or 0 when i System.out it result

INFO: asdfasdf ===[Lcom.infion.web.biaya_marketing.entity.Mktksppencairan0;@a20a9b
INFO: asdfasdf ===64

INFO: asdfasdf ===[Lcom.infion.web.biaya_marketing.entity.Mktksppencairan0;@1f5db6
INFO: asdfasdf ===[Lcom.infion.web.biaya_marketing.entity.Mktksppencairan0;@1f5db6

FYI im using netbeans 7.3, glassfish 3.1.2 , primefaces 3.5 * the entity i used is generate by netbean so there is have two class of entity Mktksppencairan0PK and Mktksppencairan0 , where Mktksppencairan0 is have embbedded Mktksppencairan0PK thanks for helping me

NB : i have try with non composite primary key ( single primary key ) and its works, but when i try with another composite primary key it doesn't work

2

2 Answers

0
votes

You need to set selectionMode to "multiple" as @dmatos mentioned and rowkey has to return the unique identifier for each row and it need to be consistent. You can always implement selectabledatamodel in primefaces if it gets more complicated then accessing a unique field in rowKey.

0
votes

I had the same problem but I worked with List of entities as always.

private List<SipreTmpGuardia> beanList;
private List<SipreTmpGuardia> beanListSelected;

then I set in my datatable with selectionmode multiple:

    <!-- DATATABLE-->
                <p:dataTable id="dt" var="item" widgetVar="wdt"
                    rowIndexVar="rowIndex"
                    value="#{guardia.beanList}"
                    emptyMessage="#{constantesUtil.LIST_EMPTY}" paginator="true"
                    rows="20" paginatorPosition="top"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} "
                    selection="#{guardia.beanListSelected}" rowKey="#{item.codigoRowIndexTemp}"
                      >
                    <p:column headerText="N*" width="1%">
                        <h:outputText value="#{rowIndex+1}" />
                    </p:column>
                    <p:column selectionMode="multiple"  width="1%"/>
                    <p:column headerText="Indicador Mes o Reintegro" width="2%">
                        <h:outputText value="#{item.ctgIndSituacion==2?'Reintegro':'Proceso Mes'}" />
                    </p:column>

                    <f:facet name="footer">
                <p:commandButton ajax="false" action="#{guardia.saveListTmpGuardia()}" update=":frm:messages,:frm:dt,:frm:dtGM" icon="ui-icon-search" value="Guardar Seleccionados"  />
        </f:facet>
                </p:dataTable>

Edited: I understand that the RowKey in primefaces 5 doesnt support composite primarykey. What I did ,above, is set the rowKey="#{item.codigoRowIndexTemp}" and fill this in the function that fill my table .. value="#{guardia.beanList}" .

Dont need to set a composite primarykey just create a temporal property in your entity like "codigoRowIndexTemp" and fill it then.. When you select it ,it will filter your list by your codigoRowIndexTemp.

Try it!

BTW1: my composite primarykey or EmbeddedId is :

@EmbeddedId
protected SipreTmpGuardiaPK sipreTmpGuardiaPK;