Warm tip: This article is reproduced from serverfault.com, please click

Table grouping resets selected values when adding new item

发布于 2020-12-04 10:43:57

Here we have a very basic table with an Add button and grouping activated. When I select values for the existing items and then press the button to add a new row, my selected values are getting reset, but without grouping it works fine. Am I doing something wrong or is this a bug?

sap.ui.getCore().attachInit(() => sap.ui.require([
  "sap/ui/core/mvc/XMLView",
  "sap/ui/model/json/JSONModel" // sample model. Can be also an ODataModel.
], async (XMLView, JSONModel) => {
  "use strict";
  
  const control = await XMLView.create({
    definition: `<mvc:View xmlns="sap.m" xmlns:mvc="sap.ui.core.mvc">
      <Table items="{
        path: '/tableItems',
        sorter: { path : 'group', group: true }
      }">
        <headerToolbar>
          <OverflowToolbar>
            <Title text="Test Table" />
            <ToolbarSpacer/>
            <Button id="addButton" text="Add" type="Emphasized" />
          </OverflowToolbar>
        </headerToolbar>
        <columns>
          <Column width="20rem">
            <Label text="col0"/>
          </Column>
          <Column width="20rem">
            <Label text="col1"/>
          </Column>
        </columns>
        <ColumnListItem vAlign="Middle">
          <Text text="{text}" />
          <Select forceSelection="false" width="100%" xmlns:core="sap.ui.core">
            <core:Item text="test1" key="test1" />
            <core:Item text="test2" key="test2" />
            <core:Item text="test3" key="test3" />
            <core:Item text="test4" key="test4" />
          </Select>
        </ColumnListItem>
      </Table>
    </mvc:View>`,
    afterInit: function() {
      this.byId("addButton").attachPress(onPressAdd, this);
      function onPressAdd() {
        const oModel = this.getModel();
        const aItems = oModel.getProperty("/tableItems");
        const newItems = aItems.concat({
          group: "3",
          text: "3-1",
          key: "3-1",
        });
        oModel.setProperty("/tableItems", newItems);
      }
    },
    models: new JSONModel({
      number: 0,
      tableItems: [
        {
          group: "1",
          text: "1-1",
          key: "1-1",
        },
        {
          group: "1",
          text: "1-2",
          key: "1-2",
        },
        {
          group: "2",
          text: "2-1",
          key: "2-1",
        },
        {
          group: "2",
          text: "2-2",
          key: "2-2",
        },
      ],
    }),
  });

  control.placeAt("content");
}));
<script id="sap-ui-bootstrap"
  src="https://openui5.hana.ondemand.com/1.82.2/resources/sap-ui-core.js"
  data-sap-ui-libs="sap.ui.core,sap.m"
  data-sap-ui-theme="sap_fiori_3"
  data-sap-ui-compatversion="edge"
  data-sap-ui-async="true"
  data-sap-ui-excludejquerycompat="true"
  data-sap-ui-xx-waitfortheme="init"
></script>
<body id="content" class="sapUiBody sapUiSizeCompact"></body>
Questioner
Lumpenstein
Viewed
0
Boghyon Hoffmann 2020-12-04 19:19:05

Set the growing="true" to the sap.m.Table.

This enables GrowingEnablement internally which prevents rerendering the entire ListBase (Table) when the target gets invalidated. Only the necessary item will be then appended to the table.

Generally, in order to optimize the rendering behavior, it's always a good practice to..:

  • Enable the growing property if the table / list is editable, shrinkable, or expandable.
  • Add key: <propertyName with unique values> to the ListBinding info object to benefit from the Extended Change Detection if the model is a client-side model such as JSONModel. With an ODataModel, the key is automatically added by the framework.
<Table xmlns="sap.m"
  growing="true"
  items="{
    path: 'myModel>/myCollection',
    key: <propertyName>, (if 'myModel' is not an ODataModel)
    sorter: ...
  }"
>