gxt3 TreeGrid过滤器

时间:2015-11-24 14:02:11

标签: java filter treegrid

我在TreeGrid上添加过滤器时遇到了麻烦 我需要你的帮助。 谢谢!

Treegrid喜欢这张图片:(在此图片中,我点击节点1,节点2显示,点击节点2节点4显示,1,2,3,4是ID从数据库中获取)

enter image description here

如果我输入值为1或2或3进入
StoreFilterField treeGrid可以显示正常 喜欢这张图片:

enter image description here

如果我输入值为4进入
StoreFilterField 它是这样的: 它正在等待......

enter image description here

谁可以帮助我? 为什么这样?

返回json数据如下:

{"records":[{"id":2,"name":"two","desc":"two","parent":true,"permission":false,"state":1}]}

为什么过滤id为4,id为1可以显示,id为2可以从数据库中搜索并返回为jsondata,但id为4(谁输入到 StoreFilterField不能作为jsondata返回。

我的代码:

package com.sa.permissions.client.role;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.UriUtils;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.Style;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.core.client.dom.ScrollSupport;
import com.sencha.gxt.core.client.util.IconHelper;
import com.sencha.gxt.data.client.loader.HttpProxy;
import com.sencha.gxt.data.client.writer.UrlEncodingWriter;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.data.shared.Store;
import com.sencha.gxt.data.shared.TreeStore;
import com.sencha.gxt.data.shared.loader.ChildTreeStoreBinding;
import com.sencha.gxt.data.shared.loader.JsonReader;
import com.sencha.gxt.data.shared.loader.TreeLoader;
import com.sencha.gxt.desktop.client.extend.*;
import com.sencha.gxt.theme.base.client.info.InfoDefaultAppearance;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.DialogHideEvent;
import com.sencha.gxt.widget.core.client.event.RowDoubleClickEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.StoreFilterField;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.info.Info;
import com.sencha.gxt.widget.core.client.toolbar.SeparatorToolItem;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
import com.sencha.gxt.widget.core.client.treegrid.TreeGrid;

import java.util.*;

/*import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.Exportable;*/

public class RoleListWindow extends MyWindow{
    private final RoleListWindow myself = this;
    private TreeLoader<JsonData> loader;
    private TreeStore<JsonData> store;
    private JsonDataProperties prop;
    private ColumnModel<JsonData> columnModel;
    private TreeGrid<JsonData> grid;
    private ToolBar toolBar;
    private AddRoleWindow addRoleWindow;
    private final CheckBoxSelectionModel<JsonData> sm;
    private SelectedHandler selectedHandler;

    private boolean multipleSelect;

    private boolean selected;


    private StoreFilterField<JsonData> filterField;
    private TextButton submit;

    @Override
    protected void onLoad() {
        super.onLoad();
    }

    @Override
    public void onShow() {
        getTreeLoader().load();
        sm.deselectAll();
        super.onShow();
    }


    @Override
    public String getAppName() {
        return "role list";
    }

    private final String URL_OF_ROLE_LIST = Const.SERVER .concat( "role!showList.do");
    private final String URL_OF_ROLE_DEL = Const.SERVER .concat("role!showdel.do");

    public RoleListWindow() {
        sm = new CheckBoxSelectionModel<JsonData>(new IdentityValueProvider<JsonData>());
        init();
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        if (selected && selected != isSelected()) {
            this.addButton(getSubmit());
            this.setButtonAlign(BoxLayoutContainer.BoxLayoutPack.CENTER);

        }
        this.selected = selected;
    }

    public JsonDataProperties getProp() {
        if (this.prop == null)
            this.prop = GWT.create(JsonDataProperties.class);
        return this.prop;
    }

    public void setProp(JsonDataProperties prop) {
        this.prop = prop;
    }

    public TreeStore<JsonData> getStore() {
        if (this.store == null) {
            this.store = new TreeStore<JsonData>(new ModelKeyProvider<JsonData>() {
                @Override
                public String getKey(JsonData item) {
                    return item.getId().toString();
                }
            });
        }
        return this.store;
    }

    public TreeLoader<JsonData> getLoader() {
        return loader;
    }

    public void setLoader(TreeLoader<JsonData> loader) {
        this.loader = loader;
    }

    public SelectedHandler getSelectedHandler() {
        return selectedHandler;
    }

    public void setSelectedHandler(SelectedHandler selectedHandler) {
        this.selectedHandler = selectedHandler;
    }

    public boolean isMultipleSelect() {
        return multipleSelect;
    }

    public void setMultipleSelect(boolean multipleSelect) {
        if (multipleSelect)
            sm.setSelectionMode(Style.SelectionMode.MULTI);
        else
            sm.setSelectionMode(Style.SelectionMode.SINGLE);
        getGrid().setSelectionModel(sm);
        this.multipleSelect = multipleSelect;
    }


    public StoreFilterField<JsonData> getFilterField() {
        if (null == this.filterField) {
            filterField = new StoreFilterField<JsonData>() {
                @Override
                protected boolean doSelect(Store<JsonData> jsonUserDataStore, JsonData parent, JsonData item, String filter) {
                  /*  new AlertMessageBox("","item="+item.getName()+" parent="+(null!=parent?parent.getName():"null")).show();*/
                    return (null!=item&&item.getId()!=null&&item.getId().equals(JsonUtil.parseLong(filter.trim(),0L)) );
                }
            };
            filterField.setPixelSize(fieldwidth, fieldheight);
            filterField.bind(getStore());
            filterField.setEmptyText("input filter");
        }
        return filterField;
    }

    public void setFilterField(StoreFilterField<JsonData> filterField) {
        this.filterField = filterField;
    }

    public TextButton getSubmit() {
        if (this.submit == null) {
            submit = new TextButton("submit");
            submit.addSelectHandler(new SelectEvent.SelectHandler() {
                @Override
                public void onSelect(SelectEvent event) {
                    if (null != getSelectedHandler()) {
                        List<JsonData> list = getGrid().getSelectionModel().getSelection();
                        if (null != list && !list.isEmpty()) {
                            Iterator<JsonData> it = list.iterator();

                            Map<Long, String> map = new HashMap<Long, String>();
                            while (it.hasNext()) {
                                JsonData jsonData = it.next();
                                map.put(jsonData.getId(), jsonData.getName());
                                selectedHandler.setReturnValue(map);
                            }
                        }
                    }
                    myself.hide();
                }
            });
        }
        return submit;
    }
    private ColumnModel<JsonData> getColumnModel() {
        if (this.columnModel == null) {
            ColumnConfig<JsonData, Long> id = new ColumnConfig<JsonData, Long>(getProp().id(), 20, "ID");
            final ColumnConfig<JsonData, String> appName = new ColumnConfig<JsonData, String>(getProp().name(), 100, "name");
            appName.setCell(new AbstractCell<String>() {
                @Override
                public void render(Context context, String value, SafeHtmlBuilder sb) {
                    if (JsonUtil.isNotEmpty(value)) {
                        String key = "<span ".concat("  title='").concat(value).concat("'").concat(">").concat(JsonUtil.cutStr(value,50)).concat("</span>");
                        sb.appendHtmlConstant(key);
                    }
                }
            });
            final ColumnConfig<JsonData, String> desc = new ColumnConfig<JsonData, String>(getProp().desc(), 50, "desc");
            desc.setCell(new AbstractCell<String>() {
                @Override
                public void render(Context context, String value, SafeHtmlBuilder sb) {
                    if (JsonUtil.isNotEmpty(value)) {
                        String key = "<span ".concat("  title='").concat(value).concat("'").concat(">").concat(JsonUtil.cutStr(value,10)).concat("</span>");
                        sb.appendHtmlConstant(key);
                    }
                }
            });

            ColumnConfig columnConfig=sm.getColumn();
            columnConfig.setCell(new AbstractCell() {
                                     @Override
                                     public void render(Context context, Object value, SafeHtmlBuilder sb) {

                                         if((((JsonData) value).getParent()||((JsonData) value).getPermission()))
                                             sm.getAppearance().renderCellCheckBox(context, value, sb);
                                     }
                                 }
            );


            List<ColumnConfig<JsonData, ?>> l = new ArrayList<ColumnConfig<JsonData, ?>>();
            l.add(columnConfig);

            l.add(id);
            l.add(appName);
            l.add(desc);
            this.columnModel = new ColumnModel<JsonData>(l);
        }
        return this.columnModel;

    }

    private TreeLoader<JsonData> getTreeLoader() {
        if (this.loader == null) {
            JsonGridAutoBeanFactory factory = GWT.create(JsonGridAutoBeanFactory.class);
            DataRecordJsonReader reader = new DataRecordJsonReader(factory, JSonDataRecordResult.class);
            RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, URL_OF_ROLE_LIST);

            HttpProxy<JsonData> jsonProxy = new HttpProxy<JsonData>(requestBuilder) { };
            jsonProxy.setWriter(new UrlEncodingWriter<JsonData>(factory, JsonData.class));
            this.loader = new TreeLoader<JsonData>(jsonProxy, reader) {
                @Override
                public boolean hasChildren(JsonData parent) {
                    return parent.getParent();
                }
            };
            this.loader.addLoadHandler(new ChildTreeStoreBinding<JsonData>(getStore()));
        }

        return this.loader;

    }


    private TreeGrid<JsonData> getGrid() {
        if (this.grid == null) {
            ColumnModel<JsonData> columnModel = getColumnModel();
            this.grid = new TreeGrid<JsonData>(getStore(), columnModel, columnModel.getColumns().get(1));
            this.grid.addRowDoubleClickHandler(new RowDoubleClickEvent.RowDoubleClickHandler() {
                @Override
                public void onRowDoubleClick(RowDoubleClickEvent event) {
                    JsonData data = (JsonData) event.getSource().getSelectionModel().getSelectedItem();
                    AddRoleWindow editRoleWindow = getAddRoleWindow();
                    editRoleWindow.setAddorEdit(false);/*
                    editRoleWindow.setRoleIdValue(data.getId());*/
                    editRoleWindow.setLoader(getTreeLoader());
                    editRoleWindow.setStateValue(data.getState());
                    editRoleWindow.getRoleName().setValue(data.getName());
                    editRoleWindow.getRoleDesc().setValue(data.getDesc());
                    editRoleWindow.show();
                }
            });
            this.grid.setBorders(true);
            this.grid.setTreeLoader(getTreeLoader());
            this.grid.getView().setStripeRows(true);
            this.grid.getView().setColumnLines(true);
            this.grid.getView().setTrackMouseOver(false);
            this.grid.getView().setForceFit(true);
            this.grid.getView().setAutoFill(true);
            this.grid.getView().setAutoExpandColumn(columnModel.getColumns().get(1));
            sm.setSelectionMode(Style.SelectionMode.MULTI);
            /* sm.setSelectionMode(isMultipleSelect()?Style.SelectionMode.MULTI:Style.SelectionMode.SINGLE);*/
            this.grid.setSelectionModel(sm);
        }
        return this.grid;
    }

    private ToolBar getToolBar() {
        if (null == this.toolBar) {
            this.toolBar = new ToolBar();
            Image add = getImage("app_add","add",getImgpix(),getImgpix());

            this.toolBar.add(add);

           toolBar.add(getFilterField());
        }
        return this.toolBar;
    }

    private AddRoleWindow getAddRoleWindow() {
        if (this.addRoleWindow == null) {
            this.addRoleWindow = (AddRoleWindow) MoudleFactory.moudleMap.get(AddRoleWindow.class.getName());
            this.addRoleWindow = null == this.addRoleWindow ? new AddRoleWindow() : this.addRoleWindow;
        }
        return this.addRoleWindow;
    }

    private void init() {
        VerticalLayoutContainer  con = new VerticalLayoutContainer ();
        con.add(getToolBar(), new VerticalLayoutContainer.VerticalLayoutData(1, -1));
        con.add(getGrid(), new VerticalLayoutContainer.VerticalLayoutData(1, 1));

        this.setPixelSize(defaultWidth, defaultHeight);
        con.setScrollMode(ScrollSupport.ScrollMode.AUTO);

        this.add(con);
        if (isSelected()) {
            this.addButton(getSubmit());
            this.setButtonAlign(BoxLayoutContainer.BoxLayoutPack.CENTER);
            this.setHeadingText("role select");
        } else
            this.setHeadingText("role");
        this.setBodyBorder(true);
        this.setMinimizable(true);
        this.setMaximizable(true);


    }


    class DataRecordJsonReader extends JsonReader<List<JsonData>, JSonDataRecordResult> {
        public DataRecordJsonReader(AutoBeanFactory factory, Class<JSonDataRecordResult> rootBeanType) {
            super(factory, rootBeanType);
        }

        @Override
        protected List<JsonData> createReturnData(Object loadConfig, JSonDataRecordResult records) {
            List<JsonData> r = records.getRecords();
            return r;
        }

    }

    interface JsonData {
        public Long getId();
        public String getName();
        public String getDesc();
        public boolean getParent();
        public boolean getPermission();
        public Integer getState();


    }

    class KeyProvider implements ModelKeyProvider<JsonData> {

        @Override
        public String getKey(JsonData item) {
            return item.getId().toString();
        }
    }

    interface JSonDataRecordResult {
        public List<JsonData> getRecords();

    }

    interface JsonGridAutoBeanFactory extends AutoBeanFactory {
        AutoBean<JSonDataRecordResult> items();
    }

    interface JsonDataProperties extends PropertyAccess<JsonData> {
        @Editor.Path("id")
        ModelKeyProvider<JsonData> key();
        ValueProvider<JsonData, Long> id();
        ValueProvider<JsonData, String> name();
        ValueProvider<JsonData, String> desc();
        ValueProvider<JsonData, Boolean> parent();
        ValueProvider<JsonData, Boolean> permission();
        ValueProvider<JsonData, Integer> state();
    }



}

0 个答案:

没有答案