PrimeFaces DataTable CellEdit get entity/object

前端 未结 3 2131
栀梦
栀梦 2021-02-14 04:40

I have a datatable which displays various entities based on a List<>. When I select a cell for editing I want to be able to also get the entity somehow in order to update it.

3条回答
  •  不知归路
    2021-02-14 04:50

    I liked @user1928596's answer so I extended it a bit to get the exact datapoint represented by the cell and update only that. This does couple the datatable column heading text to the backend code but I don't know of a better way to do this.

    What really surprised me about the result is that when I edit the data in the datatable, the data in the backing bean is changed as well. I don't need cellEditEvent.getNewValue() because the data in the view is somehow bound to the data in the backing bean. I thought it was display-only. The log statement at the end of the onCellEdit() method was intended to show the old and new values for the Event object but it only shows the new value.

    Here's the cell-editable datatable:

    Here's the display code:

        
              
            
            
                
            
            
                
            
            
                
                        
                            
                        
                
            
            
                
            
        
    

    And the onCellEdit method (which I named editEvent()):

    public void editEvent(CellEditEvent cellEditEvent) {
        Object newValue = cellEditEvent.getNewValue();
        String columnHeader = cellEditEvent.getColumn().getHeaderText();
        Event editedEvent = (Event) ((DataTable) cellEditEvent.getComponent()).getRowData();
        Event eventBeforeEdit = null;
        for (Event thisEvent : events) { // Find this event in the list of cached events.
            if (editedEvent.getId() == thisEvent.getId()) {
                eventBeforeEdit = thisEvent;
            }
        }
        log.info("Updating event " + eventBeforeEdit + " to " + editedEvent);
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
        String update = null;
        if ("Event Name".equals(columnHeader)) {
            update = "update events set name = '" + newValue + "' where id = " + editedEvent.getId();
        } else if ("Start Date".equals(columnHeader)) {
            update = "update events set startdate = '" + dateFormatter.format(newValue) + "' where id = " + editedEvent.getId();
        } else if ("End Date".equals(columnHeader)) {
            update = "update events set enddate = '" + dateFormatter.format(newValue) + "' where id = " + editedEvent.getId();
        } else if ("Status".equals(columnHeader)) {
            update = "update events set status = '" + newValue + "' where id = " + editedEvent.getId();
        } else {
            log.error("Unrecognized value " + newValue + " encountered during event edit.");
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Event Save Failure:", "Apologies but we were unable to parse your entry " + newValue);
            FacesContext.getCurrentInstance().addMessage(null, message);
            return;
        }
        try {
            mysqlNamedParameterJdbcTemplate.update(update, new HashMap());
        } catch (DuplicateKeyException e) { // There may be an event with the same name.
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Event Save Failure:",
                    "That name has already been used for an archived or logically deleted event. " + "Please use a different name for the new event to avoid confusion.");
            FacesContext.getCurrentInstance().addMessage(null, message);
            return;
        }
        log.info("Event " + eventBeforeEdit + " updated to " + editedEvent);
        loadEvents();
    }
    

提交回复
热议问题