Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    List Grid select all check box fields

    Hi all,

    I have a list grid which has a number of rows containing checkboxes.

    What I'm trying to achieve is when a check box is ticked, all the check boxes in the row is ticked. So I almost have is working...

    Code:
        private void setupRoleList() {
            listGrid.setWidth100();
            listGrid.setHeight100();
            listGrid.setDataSource(CaseTypeRoleDS.getInstance(caseType.getCode()));
            listGrid.setSortField(CaseTypeRoleDS.NAME);
            listGrid.setShowHeaderContextMenu(false);
            listGrid.setCanDragResize(false);
            listGrid.setCanSort(false);
            listGrid.setAutoSaveEdits(false);
            listGrid.setSelectionType(SelectionStyle.SIMPLE);
            listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
    
            layout.addMember(listGrid);
    
            // Set the action checkboxes to 55 width
            listGrid.addDrawHandler(new DrawHandler() {
                @Override
                public void onDraw(DrawEvent drawEvent) {
                    listGrid.setAutoFitData(Autofit.HORIZONTAL);
                    for (ListGridField field : listGrid.getFields()) {
                        if (!field.getName().equals(CaseTypeRoleDS.NAME)) {
                            field.setWidth(55);
                            field.setCanEdit(true);
                        }
                    }
                }
            });
    
            listGrid.addDataArrivedHandler(new DataArrivedHandler() {
                @Override
                public void onDataArrived(DataArrivedEvent dataArrivedEvent) {
                    for (ListGridRecord record : listGrid.getRecords()) {
                        if (record.getAttributeAsBoolean(CaseTypeRoleDS.ALL_ALLOWED)) {
                            listGrid.selectRecord(record);
                        }
                    }
                }
            });
    
            listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
                @Override
                public void onSelectionChanged(SelectionEvent selectionEvent) {
                    if (selectionEvent.getSelection() != null) {
                        ListGridRecord record = selectionEvent.getRecord();
                        boolean selected = listGrid.isSelected(record);
                        for (String attributeName : record.getAttributes()) {
                            if (!attributeName.equals(CaseTypeRoleDS.NAME) && !attributeName.equals(CaseTypeRoleDS.ID)) {
                                record.setAttribute(attributeName, selected);
                            }
                        }
                        //RecordList recordList = new RecordList(new Record[] { record });
                        //listGrid.applyRowData(recordList);
    
                        //int rowIndex = listGrid.getRecordList().indexOf(record);
                        //listGrid.refreshRow(rowIndex);
    
                        listGrid.redraw();
                    }
                }
            });
        }
    Data Source
    Code:
        @Override
        protected void executeUpdate(final String requestId, DSRequest request, final DSResponse response) {
            final ListGridRecord record = new ListGridRecord(request.getData());
    
            Integer roleId = null;
            Map<String, Boolean> updates = new HashMap<String, Boolean>();
            for (String attributeName : record.getAttributes()) {
                if (attributeName.equals(ID)) {
                    roleId = record.getAttributeAsInt(attributeName);
                } else if (!attributeName.startsWith("_check")) {
                    updates.put(attributeName, record.getAttributeAsBoolean(attributeName));
                }
            }
            RpcServiceLocator.getRoleService().updateCaseTypePermissions(roleId, caseTypeCode, updates, new ErrorMessageCallback<RoleDto>("Failed to update role") {
                @Override
                public void onSuccess(RoleDto roleDto) {
                    response.setData(new Record[]{mapper.map(roleDto)});
                    processResponse(requestId, response);
                }
            });
        }
    The issue I have is when the selection check box is used, and I select all the checkbox fields in the selection change hander, when I call save, the changed data is not passed.

    So I assume the problem is in my selection changed handler, updating the record I'm guessing is not marking the cells as updated.

    How can I achieve this? I've tried refreshing the row, and calling applyRowData(), they didn't seem to work either.

    Thanks,
    Dale
    Last edited by ellisd5; 20 May 2014, 01:53.

    #2
    Sorry to bump this,

    Been trying to resolve this, still can't figure this out, anyone have any idea have to inform the data source of the change

    Comment


      #3
      Hi ellisd5,

      I'm not sure I completely understand you case. The checkbox you are taking about in "when a check box is ticked, all the check boxes in the row is ticked" is repeated per row, correct?

      If so, I'd think that you are using the wrong handler(?).ListGrid.addSelectionChangedHandler is about changed selected row in the ListGrid.
      I'd think you'll need a ListGridField.addChangedHandler(...) for your "click me to select all"-box and update the values for the other ListGridFields there.

      Best regards,
      Blama

      Comment


        #4
        Hi blama,

        thanks for replying.

        In simpler terms, what I'm trying to do is, when a row is selected, then all the checkbox fields are selected, when the row is unselected, all the checkbox fields are unselected.

        From the user interface everything seems fine, it works how it should, when selected, all the checkboxes do get checked, and visa versa.

        To give an example of my problem this is what happens...

        1. A row is selected
        2. Event fired and attributes "ATT1" and "ATT2" are set to true.
        3. redraw() is called and the grid displays the row with check boxes for "ATT1" and "ATT2" checked.
        4. The listGrids saveAllEdits() method is called.
        5. In the datasources executeUpdate() method, the request object does not contain any changes for the record to "ATT1" and "ATT2", this is the problem.

        So the issue seems to be that in manually setting the attribute values, when calling saveAllEdits(), it is not recognizing that there has been a change. As mentioned I have tried various things but I just cant figure out how to make the changes get passed to the data source.

        Thanks,
        Dale

        Comment


          #5
          Hi Dale,

          I don't see a write operation somewhere like the saveAllEdits() you wrote about.
          What will definitely work (although I don't know if it is the best way to do it) is to do a Datasource.updateData(Record yourRecord) in the SelectionChangedHandler.
          This should also take care of the update of the display.
          If you change many (more than one) records, you might want to queue the updates in order to have only one HTTP hit.

          Best regards,
          Blama

          Comment


            #6
            Thanks Blama,

            I haven't included the saveAllEdits() code, but it's called on a button click.

            I did try updateData() but the problem with that is that it fires the update to the datasource straight away listGrid.setAutoSaveEdits(false); is ignored when invoking programmatically.

            I think I'm going to have to do what you suggested in the queing up the records and then calling updateData(), this was always my backup option, not as clean as id like but anything that works will do.

            Thanks

            Comment


              #7
              You probably want to use ListGrid.setEditValue/setEditValues() rather than record.setAttribute
              Last edited by Isomorphic; 21 May 2014, 02:33.

              Comment


                #8
                Thanks Isomorphic,

                That was what I was after..

                Code:
                        listGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
                            @Override
                            public void onSelectionChanged(SelectionEvent selectionEvent) {
                                if (selectionEvent.getSelection() != null) {
                                    ListGridRecord record = selectionEvent.getRecord();
                                    boolean selected = listGrid.isSelected(record);
                                    int rowIndex = listGrid.getRecordList().indexOf(record);
                
                                    for (String attributeName : record.getAttributes()) {
                                        if (attributeName.startsWith(CaseTypeRoleDS.PERMISSION_PREFIX)) {
                                            int colNum = listGrid.getFieldNum(attributeName);
                                            listGrid.setEditValue(rowIndex, colNum, selected);
                                        }
                                    }
                                }
                            }
                        });
                Cheers,
                Dale

                Comment

                Working...
                X