javascriptsapui5hana-xs

How to create a new table in database using sapui5?


I'm looking for your help. I created a webapp. Here I can create a table and control entries. Now I'm in frustration because I have no idea how I can save this table to database. Here is the save function:

onSave: function() {
    //Create all the records added to table via Json model
    var oTable = this.getView().byId("packItem");
    // Get the table Model
    var oModel = oTable.getModel();
    // Get Items of the Table
    var aItems = oTable.getItems();
    // Define an empty Array
    var itemData = [];
    for (var iRowIndex = 0; iRowIndex < aItems.length; iRowIndex++) {
        var l_material = oModel.getProperty("Material", aItems[iRowIndex].getBindingContext());
        var l_batch = oModel.getProperty("Batch", aItems[iRowIndex].getBindingContext());
        var l_quantity = oModel.getProperty("Quantity", aItems[iRowIndex].getBindingContext());
        var l_unit = oModel.getProperty("Unit", aItems[iRowIndex].getBindingContext());
        itemData.push({
            Batch: l_batch,
            Matnr: l_material,
            Qty: l_quantity,
            Uom: l_unit,
        });
    }
    // Get the values of the header input fields
    var ComCode = this.getView().byId("inputCC").getValue();
    var Plant = this.getView().byId("inputPlant").getValue();

    // Create one emtpy Object
    var oEntry1 = {};
    oEntry1.Bukrs = ComCode;
    oEntry1.Werks = Plant;

    var oModel1 = new sap.ui.model.odata.ODataModel(""); // Define the model
    this.getView().setModel(oModel1);// set the model
    oModel1.setHeaders({"X-Requested-With": "X"});

    oModel1.create("", oEntry1, { // Call the OData Service (.creat Function)
        success: function(oData, oResponse) {

        },
        error: function(oError) {
            alert("Failure - OData Service could not be called. Please check the Network Tab at Debug.");
        }
    });
}

Solution

  • UI5 apps are client side applications. So the tables you create, are only UI table. A nice representation of data. But don't mix this with DB tables. Your DB is managed by your backend system. This means that you should have a system listening in whatever port of your server, so it can attend your request and manipulate the DB.

    Usually the UI5 apps send data to a backend endpoint via OData call or AJAX call. But both are just protocols to send a bunch of data in the body or header of a HTTP request to an specific URL.

    To create a new Table in your DB (I'll guess it is a SQL database) you should expose a service in your server that execute a CREATE SQL query agains your database whenever you call that service. But this is not UI5 at all. This is backend staff, and it depends on which kind of backend you have, which kind of DB, etc etc