Как обращаться с StoreEvent в редактируемой сетке ??GXT - PullRequest
0 голосов
/ 01 марта 2011

Я создал редактируемую сетку, в которой только 3 столбца из 5 доступны для редактирования.Мне нужно получить число, введенное в эти редактируемые ячейки и добавить их для отображения в других ячейках той же сетки.Я добавил storeListener в ListStore, который содержит предварительно загруженные данные сетки.и переопределить метод storeUpdate (StoreEvent se).но я не понимаю, как получить обновленные данные, используя это событие обновления ... PLS, помогите мне, ребята ....... код такой

package org.openxdata.analyzer.client;

import java.util.ArrayList;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent; 
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;  
import com.google.gwt.user.client.Element;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;  
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;  
import java.util.List;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;  
import com.extjs.gxt.ui.client.widget.grid.CellEditor;  
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;  
import com.extjs.gxt.ui.client.widget.layout.FitLayout;  
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.data.ModelData;


public class Grid1 extends Viewport {

    @Override
    protected void onRender(Element parent, int index) {
        super.onRender(parent, index);
        setStyleAttribute("margin", "10px");



        final Window w = new Window();
        w.setHeading("grids");
        w.setSize(600,250);
        w.setModal(true);
        //w.setLayout(new RowLayout(Orientation.VERTICAL)); 
        w.setLayout(new FitLayout());
        w.setResizable(false);

        FormData formData = new FormData("98%");


        List<ColumnConfig> col = new ArrayList<ColumnConfig>();
        ColumnConfig column = new ColumnConfig();
        column.setId("disease");
        column.setWidth(150);
        TextField<String> text = new TextField<String>();    
        text.setAllowBlank(false);    
        text.setAutoValidate(true);    
        //column.setEditor(new CellEditor(text));  
        col.add(column);


        column = new ColumnConfig();
        column.setId("exposure");
        column.setWidth(120);
        TextField<String> text1 = new TextField<String>();    
        text1.setAllowBlank(false);    
        text1.setAutoValidate(true);    
        //column.setEditor(new CellEditor(text1));  
        col.add(column);

        column = new ColumnConfig();
        column.setId("plus");
        column.setHeader("Exposure");
        column.setWidth(80);
    final NumberField text2 = new NumberField();    
        text2.setAllowBlank(false);    
        text2.setAutoValidate(true);    
        column.setEditor(new CellEditor(text2));  
        col.add(column);

        column = new ColumnConfig();
        column.setId("minus");
        column.setHeader("Exposure");
        column.setWidth(80);
        final NumberField text3 = new NumberField();   
        text3.setAllowBlank(false);    
        text3.setAutoValidate(true);    
        column.setEditor(new CellEditor(text3));  
        col.add(column);

        column = new ColumnConfig();
        column.setId("total");
        column.setHeader("Totals");
        column.setWidth(80);
        final NumberField text4 = new NumberField();      
        text4.setAllowBlank(false);    
        text4.setAutoValidate(true);    
        column.setEditor(new CellEditor(text4));  
        column.setAlignment(HorizontalAlignment.RIGHT);
        col.add(column);
        //ColumnModel cm = new ColumnModel(col);

        ListStore<Grid2> employeeList = new ListStore<Grid2>();    
        employeeList.add(cool.getgrid());




        employeeList.addStoreListener(new StoreListener<Grid2>() {


        @Override
        public void storeUpdate(StoreEvent<Grid2> se) {




            //doStoreUpdate(se);


        }
        });

        ColumnModel cm = new ColumnModel(col);  


        final EditorGrid<Grid2> grid = new EditorGrid<Grid2>(employeeList, cm);  
        //grid.setStyleAttribute("borderTop", "none");  
        //grid.setAutoExpandColumn("name");  
        grid.setBorders(true);  
        grid.setStripeRows(true);  

        /*
        grid.addListener(Events.OnChange, new Listener<ComponentEvent>() {
             public void handleEvent(ComponentEvent ce) {

            }
        });

            */
        w.add(grid);
        w.show();
    }
}


class Grid2 extends BaseModel {
    private static final long serialVersionUID = 1L;
    public Grid2(){
    }
    public Grid2  ( String  dis, String exp, Integer x,Integer y) {
        set("disease", dis);
        set("exposure", exp);
        set("plus", x);
        set("minus", y);
    }

    public Grid2 (String  dis, String exp, Integer x,
            Integer y, Double v) {
        // this(dis, exp, x,y,v);
        set("disease", dis);
        set("exposure", exp);
        set("plus", x);
        set("minus", y);
        set("total", v);
    }
    public Grid2(String a,String b,String c,String d,String t){
        set("disease",a);
        set("exposure",b);
        set("plus",c);
        set("minus",d);
        set("total",t);
    }
    public  Integer getplus() {
        return (Integer)get("plus");
    }
    public Integer getminus() {
        return (Integer)get("minus");
    }


    public class TestData {  

        public  List<Grid2> getData()  
        {  
            List<Grid2> lt= new ArrayList<Grid2>();  



            lt.add(new Grid2("Christina Blake","Information Technology",1,2,1.23));  
            lt.add(new Grid2("Heriberto Rush","Information Technology",3,4,1.334));  

            return lt;  
        }  
    }  

}

class cool {  

    public static  List<Grid2> getgrid()  
    {  
        List<Grid2> data = new ArrayList<Grid2>();  

        //data.add(new Grid(" "," ","Exposure"," ","Totals"));  
        data.add(new Grid2(" "," ","(+)","(-)"," "));  
        data.add(new Grid2("'Disease'","(+)",0,0,0.00));  
        data.add(new Grid2(" ","(-)",0,0,0.00));  
        data.add(new Grid2("Totals"," ",0,0,0.00));  


        return data;  
    }  

}

1 Ответ

0 голосов
/ 26 января 2012

Я просто внесу некоторые изменения в ваш код, просто проверьте его. Этот код поможет вам.

package org.openxdata.analyzer.client;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.StoreEvent;
import com.extjs.gxt.ui.client.store.StoreListener;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;


public class Grid1 extends Viewport {

@Override
protected void onRender(Element parent, int index) {
    super.onRender(parent, index);
    setStyleAttribute("margin", "10px");



    final Window w = new Window();
    w.setHeading("grids");
    w.setSize(600,250);
    w.setModal(true);
    //w.setLayout(new RowLayout(Orientation.VERTICAL)); 
    w.setLayout(new FitLayout());
    w.setResizable(false);

    FormData formData = new FormData("98%");


    List<ColumnConfig> col = new ArrayList<ColumnConfig>();
    ColumnConfig column = new ColumnConfig();
    column.setId("disease");
    column.setWidth(150);
    TextField<String> text = new TextField<String>();    
    text.setAllowBlank(false);    
    text.setAutoValidate(true);    
    //column.setEditor(new CellEditor(text));  
    col.add(column);


    column = new ColumnConfig();
    column.setId("exposure");
    column.setWidth(120);
    TextField<String> text1 = new TextField<String>();    
    text1.setAllowBlank(false);    
    text1.setAutoValidate(true);    
    //column.setEditor(new CellEditor(text1));  
    col.add(column);

    column = new ColumnConfig();
    column.setId("plus");
    column.setHeader("Exposure");
    column.setWidth(80);
final NumberField text2 = new NumberField();    
    text2.setAllowBlank(false);    
    text2.setAutoValidate(true);    
    column.setEditor(new CellEditor(text2));  
    col.add(column);

    column = new ColumnConfig();
    column.setId("minus");
    column.setHeader("Exposure");
    column.setWidth(80);
    final NumberField text3 = new NumberField();   
    text3.setAllowBlank(false);    
    text3.setAutoValidate(true);    
    column.setEditor(new CellEditor(text3));  
    col.add(column);

    column = new ColumnConfig();
    column.setId("total");
    column.setHeader("Totals");
    column.setWidth(80);
    final NumberField text4 = new NumberField();      
    text4.setAllowBlank(false);    
    text4.setAutoValidate(true);    
    column.setEditor(new CellEditor(text4));  
    column.setAlignment(HorizontalAlignment.RIGHT);
    col.add(column);
    //ColumnModel cm = new ColumnModel(col);

    ListStore<Grid2> employeeList = new ListStore<Grid2>();    
    employeeList.add(Cool.getgrid());




    employeeList.addStoreListener(new StoreListener<Grid2>() {


    @Override
    public void storeUpdate(StoreEvent<Grid2> se) {




        //doStoreUpdate(se);


    }
    });

    ColumnModel cm = new ColumnModel(col);  


    final EditorGrid<Grid2> grid = new EditorGrid<Grid2>(employeeList, cm);  
    //grid.setStyleAttribute("borderTop", "none");  
    //grid.setAutoExpandColumn("name");  
    grid.setBorders(true);  
    grid.setStripeRows(true);  

    /*
    grid.addListener(Events.OnChange, new Listener<ComponentEvent>() {
         public void handleEvent(ComponentEvent ce) {

        }
    });

        */
    w.add(grid);
    w.show();

    grid.addListener(Events.AfterEdit, new Listener<BaseEvent>() {
        @Override
        public void handleEvent(BaseEvent be) {
            GridEvent ge = (GridEvent) be;

                Double plusTotal = 0.0;
                Double minusTotal = 0.0;
                for(int i=1 ; i < grid.getStore().getCount();i++ ){
                    if(grid.getStore().getAt(i).get("disease").toString().equals("Totals")){
                        grid.getStore().getAt(i).set("plus",plusTotal);
                        grid.getStore().getAt(i).set("minus",minusTotal);
                        grid.getStore().getAt(i).calculateTotal();
                        grid.getStore().commitChanges();
                    }else{
                        plusTotal = plusTotal + (grid.getStore().getAt(i).getplus());
                        minusTotal = minusTotal + (grid.getStore().getAt(i).getminus());
                    }
                }

                grid.getSelectionModel().getSelectedItem().calculateTotal();
                grid.getStore().commitChanges();
        }
    });
}
}


package org.openxdata.analyzer.client;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModel;

class Grid2 extends BaseModel {
    private static final long serialVersionUID = 1L;
    public Grid2(){
    }
    public Grid2  ( String  dis, String exp, Integer x,Integer y) {
        set("disease", dis);
        set("exposure", exp);
        set("plus", x);
        set("minus", y);
    }

    public Grid2 (String  dis, String exp, Integer x,
            Integer y, Double v) {
        // this(dis, exp, x,y,v);
        set("disease", dis);
        set("exposure", exp);
        set("plus", x);
        set("minus", y);
        set("total", v);
    }
    public Grid2(String a,String b,String c,String d,String t){
        set("disease",a);
        set("exposure",b);
        set("plus",c);
        set("minus",d);
        set("total",t);
    }
    public  Double getplus() {
        return Double.valueOf(get("plus").toString());
    }
    public Double getminus() {
        return Double.valueOf(get("minus").toString());
    }

    public void calculateTotal(){
        set("total",(getplus() - getminus()));
    }

    public class TestData {  

        public  List<Grid2> getData()  
        {  
            List<Grid2> lt= new ArrayList<Grid2>();  



            lt.add(new Grid2("Christina Blake","Information Technology",1,2,1.23));  
            lt.add(new Grid2("Heriberto Rush","Information Technology",3,4,1.334));  

            return lt;  
        }  
    }  

}


package org.openxdata.analyzer.client;

import java.util.ArrayList;
import java.util.List;

public class Cool {

    public static  List<Grid2> getgrid()  
    {  
        List<Grid2> data = new ArrayList<Grid2>();  

        //data.add(new Grid(" "," ","Exposure"," ","Totals"));  
        data.add(new Grid2(" "," ","(+)","(-)"," "));  
        data.add(new Grid2("'Disease'","(+)",0,0,0.00));  
        data.add(new Grid2(" ","(-)",0,0,0.00));  
        data.add(new Grid2("Totals"," ",0,0,0.00));  


        return data;  
    }
}
...