2012-09-02 26 views
2

我到處搜索過我無法想出解決方案。每次用戶單擊「提交」(該部分工作)時,我都試圖使此代碼顯示圖表。問題隨着用戶點擊多次提交而出現。它多次顯示圖表。我如何讓它刪除最後一張圖並用新圖更新它?如何刪除谷歌圖表並將其替換爲谷歌應用中的新圖表?

function doGet() { 
    var uiApp = UiApp.createApplication().setTitle("some title"); 

    var grid = uiApp.createGrid(3, 2); 
    grid.setWidget(0, 0, uiApp.createLabel('Lablel 1')); 
    grid.setWidget(1, 0, uiApp.createLabel('Lablel 2')); 

    var panel = uiApp.createVerticalPanel(); 

    panel.add(grid); 

    var button = uiApp.createButton('submit'); 
    var handler = uiApp.createServerHandler('b'); 
    handler.addCallbackElement(grid); 
    button.addClickHandler(handler); 

    panel.add(button); 
    uiApp.add(panel); 

    return uiApp; 
} 

function b(e) { 
    var app = UiApp.getActiveApplication(); 

    //gets all my chart data 
    var chart = getchartdata() 

    //Add chart 
    app.add(chart) 

    var uiApp = UiApp.getActiveApplication(); 
    uiApp.close(); 

    return uiApp; 
} 

回答

5

最簡單的方法是爲圖表製作一個單獨的面板,並在添加圖表之前每次清除面板。 VerticalPanel小部件也有一個可以使用的刪除方法。 請參見下面的代碼的第一個選項,我建議

function doGet(){ 
    /* All your other code here */ 
    uiApp.add(panel); 
    var chartPanel = uiApp.createVerticalPanel().setId('chartPanel'); 
    uiApp.add(chartPanel); 

    return uiApp; 
} 

function b(e) { 
    var app = UiApp.getActiveApplication(); 

    //gets all my chart data 
    var chart = getchartdata() 

    /* Clear chart Panel */ 
    var chartPanel = app.getElementById('chartPanel'); 
    chartPanel.clear(); 

    //Add chart 
    chartPanel.add(chart) 

    var uiApp = UiApp.getActiveApplication(); 
    uiApp.close(); 

    return uiApp; 
} 

P.S:我不知道你爲什麼同一件事的兩個不同變量的應用程序和了UiApp。雖然不是你的情況,但可能會導致混淆和問題。

+0

感謝Srik正是我所需要的! :) – Herb191

+0

很酷。請記住接受答案 – Srik