简体   繁体   English

sapui5 sap.m.table如何在单击添加行按钮时将输入添加为列

[英]sapui5 sap.m.table How to add Input as column on Click of add Row button

I have a small requirement in sap.m.Table . 我对sap.m.Table有一个小的要求。 Initially, I created a table like below.. 最初,我创建了一个如下表。

                          <Table id="reqTable" mode="MultiSelect">
                                                <columns>
                                                    <Column minScreenWidth="Tablet" demandPopin="true" hAlign="Center">
                                                        <Text text="{i18n>startDate}"/>
                                                    </Column>
                                                    <Column minScreenWidth="Tablet" demandPopin="true" hAlign="Center">
                                                        <Text text="{i18n>endDate}"/>
                                                    </Column>
                                                </columns>
                                                <items>
                                                    <ColumnListItem>
                                                        <cells>
                                                            <Text text="12/08/2002"/>
                                                            <Text text="13/09/2002"/>                                                           
                                                        </cells>
                                                    </ColumnListItem>
                                                </items>
                                            </Table>

Now if i click on "Add Button", i need to add sap.m.Input to the column , But im getting NaN/NaN/NaN in the new column instead of sap.m.Input . 现在,如果我单击“添加按钮”,则需要在该列中添加sap.m.Input ,但是我会在新列中获得NaN / NaN / NaN而不是sap.m.Input Please check the below image 请检查下图 在此处输入图片说明

In the above image, if you see, I need sap.m.Input instead of NaN and it must be editable. 在上图中,如果您看到的话,我需要sap.m.Input而不是NaN,并且它必须是可编辑的。 I'm trying with below code, but it is not working.. 我正在尝试使用下面的代码,但是它不起作用..

onAdd: function() {
            var table = this.getView().byId("reqTable");
            var itemRow = {
                StartDate: new sap.m.Input(),
                EndDate: new sap.m.Input(),
                editable: true,
                LeaveType: ""
            };

            var oModel = this.getView().getModel('userInfoTableModel').getData();

            oModel.push(itemRow);

            this.getView().getModel('userInfoTableModel').setData(oModel);
        }

Can someone please help me to make it work. 有人可以帮我使它工作。

Thank you in advance 先感谢您

You Have to use Factory Template. 您必须使用工厂模板。

<Table id="idProductsTable"
          mode="SingleSelectMaster"
          growing="true"
          growingThreshold="5"
          selectionChange="onSelectionChange"
          updateFinished="onUpdateFinished"
          items="{
                       path: '/',
                       factory: 'factoryFunc'
                  }">
            <columns>
              <Column>
                <Label text="ID" />
              </Column>
              <Column>
                <Label text="Product" />
              </Column>
              <Column>
                <Label text="Weight" />
              </Column>
              <Column>
                <Label text="Availability" />
              </Column>
            </columns>
   </Table>

On Controller: 在控制器上:

factoryFunc: function(id, context) {
        var oTemplate = new Item({
          text: "{Text}"
        });
        var oContext = context.getProperty("Control");
        if (oContext == "C") {
          return new ColumnListItem({
            cells: [new Text({
                text: "{ProductId}"
              }),
              new Text({
                text: "{Name}"
              }),
              new Text({
                text: "{Weight}"
              }),
              new Text({
                text: "{Availability}"
              })
            ]
          })
        }else if (oContext == "N") {
          return new ColumnListItem({
            cells: [new Text({
                text: "{ProductId}"
              }),
              new Text({
                text: "{Name}"
              }),
              new Text({
                text: "{Weight}"
              }),
              new Input({
                value: "{Availability}"
              })
            ]
          })
        }

} }

More on Sap Blog , Plunker Sap BlogPlunker上的更多内容

You are getting NaN because the framework is trying to parceFloat the property values expected by the binding, which are undefined , since these are taken from the sap.m.Input instance you've created. 由于框架正在尝试parceFloat绑定期望的属性值( undefined ,因此得到NaN ,因为这些值取自创建的sap.m.Input实例。

You shouldn't be storing Controls in a model, since it violates the MVC architecture of SAPUI5 framework. 您不应该将控件存储在模型中,因为它违反了SAPUI5框架的MVC体系结构。

I would suggest you to use a factory function to render table list item cells: you can decide whether to render sap.m.Text or sap.m.Input based on "editable" property. 我建议您使用工厂函数来呈现表列表项单元格:您可以基于“ editable”属性决定是呈现sap.m.Text还是sap.m.Input

You can add a Row with input field in it by creating a template of ColumnListItem and then adding that in the aggregation of the table as items. 您可以通过创建ColumnListItem模板,然后将其作为项目添加到表的聚合中来在其中添加带有输入字段的行。 The ColumnListItem can have any control. ColumnListItem可以具有任何控件。 In below example, I'm adding a date range selection and an Input field. 在下面的示例中,我添加了日期范围选择和“输入”字段。

oTable.bindAggregation("items", {
                path: "mainModel>/results",
                template: new sap.m.ColumnListItem({
                    cells: [
                        new sap.m.DateRangeSelection({
                            dateValue: "{mainModel>startDate}",
                            secondDateValue: "{mainModel>endDate}",
                            change: that.fnHandleDateChange.bind(that),
                            minDate: new Date(),
                            maxDate: new Date(9999, 11, 31)
                        }),
                        new sap.m.Input({
                            value: "{mainModel>value}",
                            type: sap.m.InputType.Number,
                            change: that.fnHandleValueChange.bind(that),
                            textAlign: sap.ui.core.TextAlign.Right,
                        })
                    ],
                    highlight: sap.ui.core.MessageType.Information
                })
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM