cancel
Showing results for 
Search instead for 
Did you mean: 

how to update table or model on button click

Former Member
0 Kudos

I have one table on hana DB, exposed it as xsodata service


service  {

    "schema"."table" as "OrderItem";

  }

showing the in ui5 app as


var Model = new sap.ui.model.json.JSONModel("../../services/myexp.xsodata/OrderItem", false);

sap.ui.getCore().setModel(Model,'myitem');

var oTemplate = new sap.m.ColumnListItem({

        cells : [

            new sap.m.Text({

                text : "{myitem>Component}"

            }),

            new sap.m.Text({

                text : "{myitem>Customer}"

            }),

            new sap.m.Text({

                text: "{myitem>Required Date}"

            }),

            new sap.m.Text({

                text : "{myitem>Status}"

            })

        ]

    });

oTable.bindItems("myitem>/d/results", oTemplate);

Working fine

Now as in image I have two buttons added at the page footer as


footer: new sap.m.Bar({

                contentRight: [

                    new sap.m.Button({

                    text: "Accept",

                    type: sap.m.ButtonType.Accept

                  }),

                new sap.m.Button({

                    text: "Reject ",

                    type: sap.m.ButtonType.Reject ,

                   })

                ]

})

now my question is how to update the Status column on click of any button, say If i press accept status will change to accepted and same with reject. (updating the model. I guess hana table will automatically update as tyhe table is expossed as odata model)

note this table will have only one row.

Thank you

Accepted Solutions (1)

Accepted Solutions (1)

pfefferf
Active Contributor
0 Kudos

Hello Namita,

instead of a JSON Model (which is more for client side models) you should use an OData Model for the consumption of your OData service. You can use sap.ui.model.odata.ODataModel or the newer version sap.ui.model.odata.v2.ODataModel if you have an up to date UI5 version.

The OData Models have an "update" method which allows you to update a record set.

In the SHINEs UserCRUD application you can find an example for the "create" method (cloud-hana-shine-sp8/userCRUD.controller.js at master · SAP/cloud-hana-shine · GitHub). Using the "update" method is pretty much the same. The coding should give you a first insight in doing the things.

Regards, Florian

Former Member
0 Kudos

Thanks Florian,

Ok I will use odata model in place of jsonmodel, but how to add the update functionality to a button not associated with the table row, instate it is in table footer.

I bit of code will help me.

Thank you

pfefferf
Active Contributor
0 Kudos

Do you wanna do changes for all lines displayed in your table or do you wanna do changes for only a specific line?

In general you have to add a handler method for the press event of your button, e.g.


new sap.m.Button({  text: "Accept"type: sap.m.ButtonType.Accept, press: "onPressAccept"})

In your controller you have to define that method.

In case you wanna do changes for all lines, you can access the table lines, e.g. like following


onPressAccept: function(oEvent){

  var oTable = this.getView().byId("idOfYourTable");

  var aItems = oTable.getItems();

  aItems.forEach(function(oItem){

    var oCtx = oItem.getBindingContext("myItem");

    // dummy determination of Component information

    var sComponent = oCtx.getProperty("Component");

   

    // do e.g. update

    // ...

  });

}

For several entries you can use the batch mode of the ODataModel instead of updating each single records by its own.

In case you just wanna update a specific record you have to introduce a selection mode for the table. For sap.m.Table it is defined via property "mode" which can have "Single" as value which defines that just a single record can be selected. To access a selected record in the press handler method, you can call method getSelectedItem for your table.


onPressAccept: function(oEvent){

  var oTable = this.getView().byId("idOfYourTable");

  var oSelectedItem = oTable.getSelectedItem();

  var sComponent = oSelectedItem.getBindingContext("myItem").getProperty("Component");

}

In general I would recommend you to have a look on the UI5 developer guide (at least the essentials) to understand what you are doing.

Regards, Florian

Former Member
0 Kudos

Hi, thank for your reply.

I just want to change the STATUS field. will try this out.

Thnx again and Merry Christmas

Former Member
0 Kudos

Hi,

For update I need odata model right? But I am using a filter on the service url, which is working fine with Json model, when i tried with odata model it's returning error as bed request.


var Model1 = new sap.ui.model.json.JSONModel({

            serviceUrl: "../../services/myexp.xsodata/OrderItem?$filter=AUFNR eq "+id

        });

sap.ui.getCore().setModel(Model1,'myitem'); // working fine

// i am using the below code, for update to work

var Model1 = new sap.ui.model.odata.v2.ODataModel({

            serviceUrl: "../../services/myexp.xsodata/OrderItem?$filter=AUFNR eq "+id

        }); // returning bed request, also adding $metadata befor the $filter(when reading)

how to ovecome this issue, as I am tring the below code to update


onPressAccept: function(){ 

       // var oTable = sap.ui.getCore().byId("detail1"); 

        var dModel = sap.ui.getCore().getModel("myitem");

        //var oSelectedItem = oTable.getSelectedItem(); 

       // var sComponent = oSelectedItem.getBindingContext("myitem").getProperty("ASTNR"); 

        console.log(dModel);

        var oEntry = {};

        oEntry.ASTNR = "Approved";

        dModel.update('', oEntry, null, function(){

                 alert("Update successful");

             },function(){

                alert("Update failed");});

      } 

which is not working as dModel is returing a Json model.

Thanks

pfefferf
Active Contributor
0 Kudos

Hi,

you have to use a sap.ui.model.Filter (here is an example: SAPUI5 SDK - Demo Kit).

You can apply the filter to the binding of your table with the same steps as displayed in the example.

Best Regards,

Florian

Former Member
0 Kudos

Thanks for your response, I figured the filter with the odata model itself.

Now I am able to update the model also, but it's strange that not showing the success or error function Alarts.

code


dModel.update('/OrderItem(AUFNR='+ sComponent +')', oEntry, null, function(){

                            alert("Success!");

                        },function(){

                            alert("Update failed");});

update is working fine. Just the alart inside success and errror functions are not showing.

Do you have any idea?

pfefferf
Active Contributor
0 Kudos

Did you set a break-point to check if the function is called at least?

Answers (0)