使用预加载的复选框捕获已更改的SmartGWT ListGrid记录

时间:2014-03-15 20:23:49

标签: gwt smartgwt

我有一个ListGrid,状态从数据库加载,部分或全部复选框将在加载时检查。我用过:

 newListGrid.setSelectionType(SelectionStyle.SIMPLE);
 newListGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);

用户选择或取消选中一个或多个复选框并保存其更改。我只想收集已更改的记录。

我尝试过SelectionUpdatedHandler,但是我没有看到访问已更改记录的方法 - 只选择了记录。

我尝试了一个SelectionChangedHandler,它允许我只收集更改的记录,但每次点击都会触发两次(因此尝试设置属性发生两次,重置它):

 class FilterSelectionChangedHandler implements SelectionChangedHandler {
      onSelectionChanged(SelectionChangedEvent event) { 
           record = event.getRecord();
           record.setAttribute(CHECK_VALUE, event.getState()); // set this field to whatever user did
           editedRecords.add(record); // editedRecords is a set
    }
}

有没有其他方法可以获得复选框状态?现在我正在使用ListGrid.getSelected,然后删除所有选定的记录,没有选择剩下的任何记录,但必须有更好的方法。

我正在使用SmartGWT 3.1和GWT 2.3

提前致谢

1 个答案:

答案 0 :(得分:2)

要遵循的步骤:

  • ORIGINAL_CHECK_VALUE
  • 中加载记录时,与属性CHECK_VALUE具有相同值的记录中设置另一个属性ListGrid
  • 在复选框字段中添加已更改的处理程序,并在复选框中每次更改值时在List
  • 中添加记录
  • 最后比较ORIGINAL_CHECK_VALUECHECK_VALUE的值,找出已修改的记录。

以下是经过一些修改后直接形成Smart GWT Showcase的示例代码。

<强> EntryPoint.java:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.ExportImageFormat;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.IntegerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.IntegerRangeValidator;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.ChangedEvent;
import com.smartgwt.client.widgets.grid.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class SmartGWTProject implements EntryPoint {

    public void onModuleLoad() {

        final ListGrid countryGrid = new ListGrid();
        countryGrid.setAlwaysShowEditors(true);
        countryGrid.setWidth(550);
        countryGrid.setHeight(224);
        countryGrid.setShowAllRecords(true);
        countryGrid.setCellHeight(22);
        countryGrid.setDataSource(CountryXmlDS.getInstance());

        ListGridField nameField = new ListGridField("countryName", "Country");
        ListGridField memberG8Field = new ListGridField("member_g8", "Member G8");
        countryGrid.setFields(nameField, memberG8Field);

        countryGrid.fetchData(null, new DSCallback() {

            @Override
            public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
                Record[] records = dsResponse.getData();
                for (Record record : records) {
                    record.setAttribute("original_member_g8",
                            record.getAttributeAsBoolean("member_g8"));
                }
                countryGrid.setData(records);
            }
        });
        countryGrid.setCanEdit(true);
        countryGrid.setEditEvent(ListGridEditEvent.CLICK);

        final List<ListGridRecord> editedRecords = new ArrayList<ListGridRecord>();
        memberG8Field.addChangedHandler(new ChangedHandler() {

            @Override
            public void onChanged(ChangedEvent event) {
                ListGridRecord record = countryGrid.getRecord(event.getRowNum());
                record.setAttribute("member_g8", (Boolean) event.getValue());
                editedRecords.add(record); // editedRecords is a set
            }
        });

        IButton btn = new IButton("Find edited records");
        btn.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                Set<String> uniqueCountryIds = new HashSet<String>();
                for (ListGridRecord record : editedRecords) {
                    if (uniqueCountryIds.add(record.getAttribute("countryCode"))) {
                        // check for unique records only
                        if (!record.getAttributeAsBoolean("original_member_g8").equals(
                                record.getAttributeAsBoolean("member_g8"))) {
                            System.out.println(record.getAttribute("countryName"));
                        }
                    }
                }
            }
        });

        btn.setTop(400);
        btn.setWidth(100);

        Canvas canvas = new Canvas();

        canvas.addChild(countryGrid);
        canvas.addChild(btn);

        canvas.draw();
    }
}

<强> CountryXmlDS.java:

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.*;

public class CountryXmlDS extends DataSource {

    private static CountryXmlDS instance = null;

    public static CountryXmlDS getInstance() {
        if (instance == null) {
            instance = new CountryXmlDS("countryDS");
        }
        return instance;
    }

    public CountryXmlDS(String id) {

        setID(id);
        setRecordXPath("/List/country");
        DataSourceIntegerField pkField = new DataSourceIntegerField("pk");
        pkField.setHidden(true);
        pkField.setPrimaryKey(true);

        DataSourceTextField countryCodeField = new DataSourceTextField("countryCode", "Code");
        countryCodeField.setRequired(true);

        DataSourceTextField countryNameField = new DataSourceTextField("countryName", "Country");
   countryNameField.setRequired(true);

        DataSourceBooleanField memberG8Field = new DataSourceBooleanField("member_g8", "G8");

        setFields(pkField, countryCodeField, countryNameField, memberG8Field);

        setDataURL("ds/test_data/country.data.xml");
        setClientOnly(true);
    }
}

<强> country.data.xml:

<List>

    <country>
        <countryName>United States</countryName>
        <countryCode>US</countryCode>
        <member_g8>true</member_g8>
    </country>
    <country>
        <countryName>China</countryName>
        <countryCode>CH</countryCode>
        <member_g8>false</member_g8>
    </country>
    <country>
        <countryName>Japan</countryName>
        <countryCode>JA</countryCode>
        <member_g8>true</member_g8>
    </country>
    <country>
        <countryName>India</countryName>
        <countryCode>IN</countryCode>
        <member_g8>false</member_g8>
    </country>
    <country>
        <countryName>Germany</countryName>
        <countryCode>GM</countryCode>
        <member_g8>true</member_g8>
    </country>
    <country>
        <countryName>United Kingdom</countryName>
        <countryCode>UK</countryCode>
        <member_g8>true</member_g8>
    </country>
    <country>
        <countryName>France</countryName>
        <countryCode>FR</countryCode>
        <member_g8>true</member_g8>
    </country>

</List>

项目结构的屏幕截图:

enter image description here