Context menu for datatable inside datagrid not working

UI Components for JSF
Post Reply
clodijo
Posts: 1
Joined: 27 Nov 2014, 10:26

27 Nov 2014, 11:11

Hello, as explained in the subject I have a context menu associated to a datatable inside a datagrid.
When I right click on a row of the datatable, a setSelectedxxx method is called (datatable works in single selection mode) but the param is null and I can't have my context menu action listener works because it needs the selectedObject.

The Testcase

Code: Select all

<p:dataGrid id="dgGrid" var="obj" value="#{testCase.objList}" columns="#{testCase.objList.size()}" layout="grid" >                      
   <p:outputPanel>
      <p:contextMenu id="contexMenu" for="dtTable" >
         <p:menuitem id="miRemove" value="Remove sub object" icon="ui-icon-trash"
                               actionListener="#{testCase.remove()}" update="dtTable" />
      </p:contextMenu>
      <p:dataTable id="dtTable" var="subObj" value="#{testCase.map.get(obj.id)}" rowKey="#{subObj.id}"
                           selection="#{testCase.selectedSubObj}" selectionMode="single" >
         <f:facet name="header">
            <h:outputText value="#{obj.description}" />
         </f:facet>
         <p:column>
            <h:outputText value="#{subObj.description}" />
         </p:column>
      </p:dataTable>
   </p:outputPanel>
</p:dataGrid>

Code: Select all

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class TestCase {

    private final List<Obj> objList = new ArrayList<Obj>();
    private final Map<Integer, List<SubObj>> map = new HashMap<>();
    private SubObj selectedSubObj;

    public TestCase() {
        objList.add(new Obj(0, "obj_0"));
        objList.add(new Obj(1, "obj_1"));
        objList.add(new Obj(2, "obj_2"));
        objList.add(new Obj(3, "obj_3"));

        for (Obj o : objList) {
            List<SubObj> sList = new ArrayList<>();
            sList.add(new SubObj(0, ("sub_" + o.getId() + "_0"), o.getId()));
            sList.add(new SubObj(0, ("sub_" + o.getId() + "_1"), o.getId()));
            map.put(o.getId(), sList);
        }

    }
    
    public void remove() {
        if((getSelectedSubObj() != null) && (getSelectedSubObj().getParentId() != null)) {
            List<SubObj> sList = map.get(getSelectedSubObj().getParentId());
            if((sList != null) && !sList.isEmpty()) {
                sList.remove(getSelectedSubObj());
                map.put(getSelectedSubObj().getParentId(), sList);
            }
        }
    }

    /**
     * @return the objList
     */
    public List<Obj> getObjList() {
        return objList;
    }

    /**
     * @return the map
     */
    public Map<Integer, List<SubObj>> getMap() {
        return map;
    }

    /**
     * @return the selectedSubObj
     */
    public SubObj getSelectedSubObj() {
        return selectedSubObj;
    }

    /**
     * @param selectedSubObj the selectedSubObj to set
     */
    public void setSelectedSubObj(SubObj selectedSubObj) {
        this.selectedSubObj = selectedSubObj;
    }

    public class Obj {

        private Integer id;
        private String description;

        public Obj(Integer id, String description) {
            this.id = id;
            this.description = description;
        }

        /**
         * @return the id
         */
        public Integer getId() {
            return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(Integer id) {
            this.id = id;
        }

        /**
         * @return the description
         */
        public String getDescription() {
            return description;
        }

        /**
         * @param description the description to set
         */
        public void setDescription(String description) {
            this.description = description;
        }
    }
    
    public class SubObj {
        private Integer id;
        private String description;
        private Integer parentId;
        
        public SubObj(Integer id, String description, Integer parentId) {
            this.id = id;
            this.description = description;
            this.parentId = parentId;
        }

        /**
         * @return the id
         */
        public Integer getId() {
            return id;
        }

        /**
         * @param id the id to set
         */
        public void setId(Integer id) {
            this.id = id;
        }

        /**
         * @return the description
         */
        public String getDescription() {
            return description;
        }

        /**
         * @param description the description to set
         */
        public void setDescription(String description) {
            this.description = description;
        }

        /**
         * @return the parentId
         */
        public Integer getParentId() {
            return parentId;
        }

        /**
         * @param parentId the parentId to set
         */
        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }
    }
}
When I click the menuItem 'miRemove' it calls the method 'remove()' but 'getSelectedSubObj()' returns null.
Where have I gone wrong?
Thanks
Primefaces 5.1
JSF 2.2.7
Server Apache Tomcat 7.0.52

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 27 guests