Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

sapui5 - Add a New Item to a Table / List

I have a fragment / view written in XML which contains a simple table with some columns and one ColumnListItem:

<m:Table id="CorrectiveActionsTable">
  <m:columns>
    <m:Column>
      <m:Text text="Lfd. Nr"/>
    </m:Column>
    <m:Column width="30%">
      <m:Text text=""/>
    </m:Column>
    <m:Column>
      <m:Text text="gefordert von"/>
    </m:Column>
    <m:Column>
      <m:Text text="Durchführungsverantwortung"/>
    </m:Column>
    <m:Column>
      <m:Text text="Planungstermin"/>
    </m:Column>
    <m:Column>
      <m:Text text="IST-Termin"/>
    </m:Column>
  </m:columns>
  <m:ColumnListItem id="ListItem_00">
    <m:Text text="1"/>
    <m:TextArea
      value="senf"
      rows="6"
      width="100%"
    />
    <m:Input placeholder="bla"/>
    <m:Input placeholder="bla2"/>
    <m:DatePicker placeholder="bla3"/>
    <m:DatePicker placeholder="bla4"/>
  </m:ColumnListItem>
</m:Table>
<m:HBox>
  <m:Button
    text="Add Button"
    visible="true"
    press="onAddButton"
    icon="sap-icon://add"
  />
</m:HBox>

The Button should be used to add a new ColumnListItem to the Table.
I think I should write the onAddButton function in the controller but I don't know where to start.

For now, my controller looks like this:

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/m/ColumnListItem",
  "sap/m/Text",
  "sap/m/TextArea",
  "sap/m/Input",
  "sap/m/DatePicker"
], function(Controller, ColumnListItem, Text, TextArea, Input, DatePicker) {
  "use strict";

  return Controller.extend("...", {
    onAddButton: function(oEvent) {
      var columnListItemNewLine = new ColumnListItem({
        cells: [
          new Text({
            text: "1"
          }),
          new TextArea({
            value: "senf",
            rows: "6",
            width: "30%"
          }),
          new Input({
            type: "text",
            placeholder: "bla"
          }),
          new Input({
            type: "text",
            placeholder: "bla2"
          }),
          new DatePicker({
            placeholder: "bla3"
          }),
          new Datepicker({
            placeholder: "bla4"
          })
        ]
      });
      this._oTable.addItem(columnListItemNewLine);
    }
  });
});

And I'm pretty sure there is a better way to do this than my approach.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Bind the collection of data to the <items> aggregation of table.
  2. Add a new entry to the model (instead of to the UI directly) when the user clicks on Add.

Thanks to the aggregation binding, UI5 will create a new ColumnListItem for you and you did not break the MVC pattern. Here are some examples, using..:

v2.ODataModel

v4.ODataModel

JSONModel

sap.ui.getCore().attachInit(() => sap.ui.require([
  "sap/ui/core/mvc/XMLView",
  "sap/ui/model/json/JSONModel",
], (XMLView, JSONModel) => XMLView.create({
  definition: `<mvc:View
    xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m"
    height="100%"
    displayBlock="true"
  >
    <Table sticky="ColumnHeaders" items="{/}">
      <headerToolbar>
        <OverflowToolbar>
          <Title text="My Items ({= %{/}.length})"/>
          <ToolbarSpacer/>
          <Button
            icon="sap-icon://add"
            press="onAddItemPress"
          />
        </OverflowToolbar>
      </headerToolbar>
      <columns>
        <Column>
          <Text text="Column 1" />
        </Column>
        <Column>
          <Text text="Column 2" />
        </Column>
      </columns>
      <ColumnListItem>
        <Text text="{col1}" />
        <Text text="{col2}" />
      </ColumnListItem>
    </Table>
  </mvc:View>`,
  models: new JSONModel([]),
}).then(view => view.placeAt("content"))));

function onAddItemPress(event) {
  const model = event.getSource().getModel();
  model.setProperty("/", model.getProperty("/").concat({
    col1: "newFoo",
    col2: "newBar",
  }));
}
<script id="sap-ui-bootstrap"
  src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
  data-sap-ui-libs="sap.ui.core, sap.m"
  data-sap-ui-preload="async"
  data-sap-ui-async="true"
  data-sap-ui-theme="sap_belize"
  data-sap-ui-compatversion="edge"
  data-sap-ui-xx-waitfortheme="true"
  data-sap-ui-xx-xml-processing="sequential"
></script>
<body id="content" class="sapUiBody sapUiSizeCompact"></body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...