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

使用预加载的复选框捕获已更改的SmartGWT ListGrid记录,gwt,smartgwt,Gwt,Smartgwt,我有一个从数据库加载状态的ListGrid,加载时会选中部分或所有复选框。我用过: newListGrid.setSelectionType(SelectionStyle.SIMPLE); newListGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX); 用户选择或取消选择一个或多个复选框并保存其更改。我只想收集已更改的记录 我尝试了SelectionUpdatedHandler,但我看不到访问已更改记录的方法-仅访问已选择

我有一个从数据库加载状态的ListGrid,加载时会选中部分或所有复选框。我用过:

 newListGrid.setSelectionType(SelectionStyle.SIMPLE);
 newListGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
用户选择或取消选择一个或多个复选框并保存其更改。我只想收集已更改的记录

我尝试了SelectionUpdatedHandler,但我看不到访问已更改记录的方法-仅访问已选择的记录

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

是否有其他方法获取复选框状态?现在我正在使用ListGrid.getSelected,然后删除所有选中的记录,剩下的记录都不会被选中,但一定有更好的方法

我正在使用SmartGWT 3.1和GWT 2.3

提前感谢

以下步骤:

  • 当在
    列表网格中加载记录时,在与属性
    检查值
    具有相同值的记录中设置另一个属性
    原始检查值
  • 在复选框字段中添加更改的处理程序,并在复选框中的值每次更改时在
    列表中添加记录
  • 最后比较
    原始检查值
    检查值
    的值,找出编辑过的记录
下面是一些修改后直接形成的示例代码

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();
    }
}
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);
    }
}
<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>
country.data.xml:

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();
    }
}
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);
    }
}
<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>

美国
美国
真的
中国
中国
假的
日本
青年成就组织
真的
印度
在里面
假的
德国
转基因的
真的
大不列颠联合王国
英国
真的
法国
FR
真的
项目结构截图:

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();
    }
}
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);
    }
}
<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>

我不知道您是如何实现您的
列表网格的。我仍然尝试实现一个示例代码。请看一看,如果不符合您的设计,请告诉我。我很高兴改变它。谢谢你,布拉吉,我感谢你的努力。它没有提供解决方案(我正在使用SelectionAppearance.CHECKBOX-因此我没有属性,changedEvent不允许我访问复选框值,但也许我可以合并其中的一些值。我奖励你尝试。有时会发生这种情况,因为我们不知道你的应用程序设计,但如果你使用了任何东西,我仍然很高兴。我一直都精通我说一个聪明的人只要一点提示就足以解决任何问题祝你好运。。