如何使最后一个添加到可伸缩表的项目直接在前一个项目的下面? [英] How to make the last item added to a flextable return directly underneath the previous item?

查看:46
本文介绍了如何使最后一个添加到可伸缩表的项目直接在前一个项目的下面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何使添加到弹性表中的最后一个项目直接位于现有项目的下面吗?

Could someone please show me how to make the last item added to the flextable go directly underneath the existing item?

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

推荐答案

该想法是将写入的行的索引保留在内存中.一个人可以使用多种方法来实现这一目标,但最直接的方法可能是在表或listBox标记中写入该行索引,类似这样的东西(我没有测试代码,但希望我没有出错):

The idea is to keep in memory the index of the row you write to. One can use many ways to achieve this but the most straightforward is probably to write this row index in the table or listBox tag, something like this (I didn't test the code but hope I made no error):

标记解决方案似乎无效,因此我改为了hidden widget解决方案.(已测试)

EDIT : the tag solution didn't seem to work, so I changed to a hidden widget solution.(tested)

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
}

这篇关于如何使最后一个添加到可伸缩表的项目直接在前一个项目的下面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆