如何从下拉列表中添加项目,例如“项目 1”、“项目 2”和“项目 3”在 flextable 中现有标签的下方?弹性表中的现有标签是:“产品类型”和“尺寸”。
function doGet() {
var app = UiApp.createApplication();
//Create listBox
var listBox = app.createListBox();
//Add items to listBox
listBox.addItem("item 1").addItem("item 2").addItem("item 3").setName("myListBox");
//Create hidden app
var hidden = app.createHidden().setName('hidden').setId('hidden')
//Create button handler
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);
//Create flextable
var table = app.createFlexTable().setId("myTable");
//Create flex table style attributes
table.setStyleAttribute("border-style", "solid")
table.setStyleAttribute("border-width", "1px")
table.setCellPadding(1);
//Create flex table headers
table.setWidget(0, 0, app.createLabel("Product Type")).setStyleAttribute("color", "blue");
table.setWidget(0, 5, app.createLabel("Size"));
var button = app.createButton("+", handler);
// add all widgets to the app
app.add(table).add(hidden);
app.add(listBox).add(button)
return app;
}
function buttonHandler(e) {
var app = UiApp.getActiveApplication();
// get the position (is a string)
var pos = e.parameter.hidden;
// initial condition, hidden widget is empty
if(pos==null){pos='0'};
// convert to number
pos=Number(pos);
var table = app.getElementById("myTable");
// add the new item at the right place
table.insertRow(pos).insertCell( pos, 0).setText(pos, 0, e.parameter.myListBox);
// increment position
++pos;
// save value
app.getElementById('hidden').setValue(pos);
// update app
return app;
}