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;
}
4

2 に答える 2

0

アイデアは、書き込む行のインデックスをメモリに保持することです。これを実現するために多くの方法を使用できますが、最も簡単なのは、おそらくこの行インデックスをテーブルまたはlistBoxタグに書き込むことです(コードをテストしていませんが、エラーが発生しないことを願っています)。


編集:タグソリューションが機能していないようだったので、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
}
于 2012-11-20T06:57:35.177 に答える