2011-02-24 92 views
1

我創建了一個可編輯的網格,其中只有3個列的5列可編輯。我沒有得到如何從特定單元格中檢索值,以便這些值可以動態添加並顯示在同一網格的另一個單元格內。我在eclipse galileo中試過這段代碼。但它不工作。請幫幫我。如何從GXT中的網格單元格中檢索值

package org.openxdata.analyzer.client; 

import java.util.ArrayList; 
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.widget.MessageBox; 
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; 


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


     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); 
      Button calculate = new Button("Calculate"); 
      w.addButton(calculate); 



      calculate.addSelectionListener(new SelectionListener<ButtonEvent>() 

        { 


       @Override 
        public void componentSelected(ButtonEvent ce) { 
        { 

         Double p=(Double)text2.getValue(); 
         Double m=(Double)text3.getValue(); 


         Double t=p+m; 

         text4.setValue(t); 

         } 
       } 

        }); 



     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

將StoreListener添加到網格後面的ListStore。處理更新事件,並設置爲

+0

感謝您的答覆。我知道如何去做,請給我發送代碼片段...... – 2011-02-27 16:39:35

1

這會幫助你。我被這個獲得價值:

GridCellRenderer<Grid2> checkStatus = new GridCellRenderer<Grid2>() { 
    public Object render(Grid2 model, String property, 
      ColumnData config, int rowIndex, int colIndex, 
      ListStore<Grid2> store, Grid<Grid2> grid) { 
     int val = model.get(property); 
     return val; 
    } 
}; 
2

如果要修改任何可編輯的單元格,然後ü可以得到store.getModifiedRecords()。它會給你修改的記錄。這條記錄可以讓你對記錄做出改變。我認爲record.getChanges()方法在那裏會返回你的模型(記錄)的地圖。當時每個模型代表一個row.u可以得到被修改爲一個鍵的單元格號和與其對應的值。

之後,你可以做store.removeAll()。然後添加以前的數據以及必須用相應的columconfig綁定的新數據。

然後致電store.add(List of models containing whole data)。 然後grid.recalculate();

0

它適用於gxt 2.x. store.getModifiedRecords().get(0).getChanges() - 返回帶有字段名稱作爲鍵的映射和舊的單元格值作爲映射的值。 store.getModifiedRecords().get(0).get("field_name")返回新值爲字符串

相關問題