2012-11-20 27 views
0

是否有人可以告訴我如何使最後一項添加到可伸縮直接在現有項目的下方?如何將最後一個項目添加到前一個項目正下方的可靈活返回?

function doGet() { 
    var app = UiApp.createApplication(); 
    var listBox = app.createListBox(); 
    listBox.addItem("item 1").addItem("item 2").addItem("item 3").setName("myListBox"); 

    var handler = app.createServerHandler("buttonHandler"); 
    // pass the listbox into the handler function as a parameter 
    handler.addCallbackElement(listBox); 

    var table = app.createFlexTable().setId("myTable"); 

    var button = app.createButton("+", handler); 
    app.add(listBox); 
    app.add(button); 
    app.add(table); 
    return app; 
} 

function buttonHandler(e) { 
    var app = UiApp.getActiveApplication(); 
    app.getElementById("myTable").insertRow(0).insertCell(0, 0).setText(0, 0, e.parameter.myListBox); 
    return app; 
} 

回答

0

這個想法是保存在你寫入的行的索引。人們可以用很多方式來實現這一點,但最直接的可能是寫在表格或列表框標籤此行的索引,像這樣的東西(我沒有測試的代碼,但希望我做任何錯誤):


編輯:似乎標籤解決方案沒有工作,所以我改爲hidden widget溶液(測試)

function doGet() { 
    var app = UiApp.createApplication(); 
    var listBox = app.createListBox(); 
    listBox.addItem("item 1").addItem("item 2").addItem("item 3").setName("myListBox"); 
    var hidden = app.createHidden().setName('hidden').setId('hidden') 
    var handler = app.createServerHandler("buttonHandler"); 
    // pass the listbox into the handler function as a parameter and the hidden widget as well 
    handler.addCallbackElement(listBox).addCallbackElement(hidden); 

    var table = app.createFlexTable().setId("myTable"); 

    var button = app.createButton("+", handler); 

    app.add(listBox).add(button).add(table).add(hidden);// add all widgets to the app 
    return app; 
} 

function buttonHandler(e) { 
    var app = UiApp.getActiveApplication(); 
    var pos = e.parameter.hidden;// get the position (is a string) 
    if(pos==null){pos='0'};// initial condition, hidden widget is empty 
    pos=Number(pos);// convert to number 
    var table = app.getElementById("myTable") 
    table.insertRow(pos).insertCell(pos, 0).setText(pos, 0, e.parameter.myListBox);// add the new item at the right place 
    ++pos ;// increment position 
    app.getElementById('hidden').setValue(pos);// save value 
    return app;// update app 
} 
+0

這工作完美。再次感謝Serge。 – user1833055

+0

您好Serge.I不得不編輯您以前的代碼。它基本上與您的代碼相同,但在flextable中添加了一個標頭。它一直說我無法將該代碼作爲新問題提交。它說,我需要「解釋」更多的代碼,因爲它與之前創建的代碼基本相同,我解釋了添加的額外代碼,所以這些代碼沒有意義。我想知道如何將項目添加到標題下的Flexbox我知道這不是提交問題的標準方式,但我沒有選擇。請幫助我理解爲什麼我得到這個錯誤以及 – user1833055

0

下面的代碼部分所做的工作是相同的,但是使用了一個使用ScriptProperties方法。沒有什麼特別,它像一個魅力:

function doGet() { 
    var app = UiApp.createApplication(); 

    // create listBox and add items 
    var listBox = app.createListBox().setName("myListBox") 
    .addItem("item 1") 
    .addItem("item 2") 
    .addItem("item 3"); 

    // set position 
    ScriptProperties.setProperty('position', '0'); 

    // create handle for button 
    var handler = app.createServerHandler("buttonHandler").addCallbackElement(listBox); 

    // create flexTable and button 
    var table = app.createFlexTable().setId("myTable"); 
    var button = app.createButton("+", handler); 

    // add all to application 
    app.add(listBox) 
    .add(button) 
    .add(table); 

    // return to application 
    return app; 
} 

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

    // get position 
    var position = parseInt(ScriptProperties.getProperty('position'),10); 

    // get myTable and add 
    app.getElementById("myTable").insertRow(position).insertCell(position, 0).setText(position, 0, e.parameter.myListBox); 

    // increment position 
    var newPosition = position++; 

    // set position 
    ScriptProperties.setProperty('position', newPosition.toString()); 

    // return to application 
    return app; 
} 

請刪除代碼,以便啓動授權過程。

參考:Script and User Properties

+0

您好Jacab,這與我需要的接近,但我只需要項目在flextable中插入之前添加的項目下。 – user1833055

+0

我向flexTable添加了setBorderWidth(1),並且Serge的結果是相同的。我將Hidden元素更改爲一個TextBox,據我所知,這是存儲最後一個位置。 @Serge:這就是你使用Hidden元素的原因,不是嗎? –

+0

的確如此......並且正如我所提到的,有很多方法可以在記憶中保存這個值。腳本屬性當然是其中之一 –

相關問題