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

标签 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

提前致谢

最佳答案

遵循的步骤:

  • 在记录中设置另一个属性 ORIGINAL_CHECK_VALUE,其值与在 ListGrid 中加载记录时属性 CHECK_VALUE 具有相同的值
  • 在复选框字段上添加更改的处理程序,并在复选框中值的每次更改时在列表中添加记录
  • 最后比较 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);
    }
}

国家/地区.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

关于gwt - 使用预加载的复选框捕获更改的 SmartGWT ListGrid 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22429294/

相关文章:

java - RPC 调用 - 静态方法不起作用

java - 如何使用 GWT 查询获取 iframe 的内容?

validation - GWT 错误 : No source code is available for type org. hibernate.validator.constraints.impl.SizeValidatorForString

gwt - SmartGWT 中同一 ListGridField 中的不同 ListGridFieldType?

java - 如何将 SmartGWT DataSource 与异构数据一起使用

jquery - GWT 适合我吗?

GWT 布局 : "take up the rest of the space."

java - Tomcat 6.0 中正常 webapp 形式的 SHA-256 消息摘要(与领域无关)

java - 如何在 Smart GWT 中创建 header

java - 如何处理 Smart-gwt SC.askforValue() 取消?