cancel
Showing results for 
Search instead for 
Did you mean: 

Error While Adding 11th Record in table

Former Member
0 Kudos

Hi All,

I have a form in which there are 10 fields in which i am taking input from user and as soon as user click on add button i am adding all those 10 field values in the table which have associated columns for each field. As soon as i add 11th time application shows me following error.

Cannot read property 'getCells' of undefined

I dont understand why it is happening

Please guide.

regards

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

i have made follwing changes in my code and my problem get resolved


if(trCount>0){

    var lastLineNo = aData[trCount-1]["POSNR"];

    lineItemNo = oIntegerFormat.parse(lastLineNo.toString()) + 10;

    console.log(lineItemNo);

   //lineItemNo = oIntegerFormat.parse(oTable2.getRows()[trCount-1].getCells()[15].getValue()) + 10;

    }

The commented out line was giving error on getCells() method i dont understand why, i just removed this line and played in the data array and my problem get resolved.

Thanks Robin for your support your discussion give me new ways and thanks to all others too who tried to resolve my issue

Regards

Answers (2)

Answers (2)

Former Member
0 Kudos

hi muhammad,

Please share the code so it will be easy to understand.

Former Member
0 Kudos

This is my button code fragment

var oBT_SO_AddItem = new sap.ui.commons.Button("oBT_SO_AddItem" , {

      text: "Add Item",

      icon: "images/AddRecord.png",

      press: function(e) {

        if (!oController.validateDetailFields())

          return;

        AddItemInDetailTable();       

      }

  });

and this my addition logic

function AddItemInDetailTable(){

    var aData = [];

   

  var trCount = oTable2.mBindingInfos.rows.binding.iLength;       

    if(trCount>0)

    for(var x = 0; x < trCount; x++){

    if (x==10)

    console.log(x);

    aData.push({

            MATNR   : oTable2.getRows()[x].getCells()[0].getValue(),

            ARKTX   : oTable2.getRows()[x].getCells()[1].getValue(),

            VKAUS   : oTable2.getRows()[x].getCells()[2].getValue(),

            DIA     : oTable2.getRows()[x].getCells()[3].getValue(),

            SPH     : oTable2.getRows()[x].getCells()[4].getValue(),

            CYL     : oTable2.getRows()[x].getCells()[5].getValue(),

            ZADD    : oTable2.getRows()[x].getCells()[6].getValue(),

            KWMENG  : oTable2.getRows()[x].getCells()[7].getValue(),

            NETPR   : oTable2.getRows()[x].getCells()[8].getValue(),

            ZEXCH   : oTable2.getRows()[x].getCells()[9].getValue(),

            ZREASON : oTable2.getRows()[x].getCells()[10].getValue(),

            NETWR   : oTable2.getRows()[x].getCells()[11].getValue(),

            VRKME   : oTable2.getRows()[x].getCells()[12].getValue(),

            OLDQTY  : oTable2.getRows()[x].getCells()[13].getValue(),

            PROQTY  : oTable2.getRows()[x].getCells()[14].getValue(),

            POSNR   : oTable2.getRows()[x].getCells()[15].getValue(),

            AUFLG   : oTable2.getRows()[x].getCells()[16].getValue()==""?"PUT":oTable2.getRows()[x].getCells()[16].getValue(),

            WERKS   : oTable2.getRows()[x].getCells()[17].getValue(),

        });

    }

    var lineItemNo = 10;

    if(trCount>0){

      lineItemNo = oIntegerFormat.parse(oTable2.getRows()[trCount-1].getCells()[15].getValue()) + 10;

      oTable2.unbindRows();

    }

    var oModel1 = new sap.ui.model.json.JSONModel();

    var oMV_SO_LensAmnt = oIntegerFormat.parse(oTF_SO_Quantity.getValue()) * oFloatFormat.parse(oTF_SO_LensPric.getValue()) + oFloatFormat.parse(oTF_SO_ExtraChg.getValue());

    aData.push(

        {

          MATNR   : sap.ui.getCore().byId("oTF_SO_Var_Code").getValue(),

          ARKTX   : oCB_SO_LensType.getLiveValue(),

          VKAUS   : oCB_SO_LensSide.getValue(),

          DIA     : oCB_SO_LensSize.getLiveValue(),

          SPH     : oCB_SO_Lens_SPH.getLiveValue(),

          CYL     : oCB_SO_Lens_CYL.getLiveValue(),

          ZADD    : oCB_SO_Lens_ADD.getLiveValue(),

          KWMENG  : oTF_SO_Quantity.getValue(),

          NETPR   : oTF_SO_LensPric.getValue(),

          ZEXCH   : oFloatFormat.parse(oTF_SO_ExtraChg.getValue()).toFixed(2),

          ZREASON : oTF_SO_ReasonEC.getValue(),

          NETWR   : oMV_SO_LensAmnt.toFixed(3),

          VRKME   : oCB_SO_LensUnit.getSelectedKey(),

          OLDQTY  : oTF_SO_Quantity.getValue(),

          PROQTY  : oTF_SO_Quantity.getValue(),

          POSNR   : lineItemNo,

          AUFLG   : "POST",

          WERKS   : oCB_SO_DelvSite.getSelectedKey()

        }

      );

   

    oModel1.setData({modelData : aData});

    oTable2.setModel(oModel1);

    oTable2.bindRows("/modelData");

    var otfnv_totQty = sap.ui.getCore().byId("oTF_NV_TotQty");

    var otfnv_totVal = sap.ui.getCore().byId("oTF_NV_TotVal");

    if(otfnv_totQty.getValue()=="")

      otfnv_totQty.setValue("0");

    if(otfnv_totVal.getValue()=="")

      otfnv_totVal.setValue("0");

    var total_qty = oIntegerFormat.parse(otfnv_totQty.getValue()) + oIntegerFormat.parse(oTF_SO_Quantity.getValue());

    var total_val = oFloatFormat.parse(otfnv_totVal.getValue()) + oFloatFormat.parse(oMV_SO_LensAmnt.toFixed(3));

    otfnv_totQty.setValue(total_qty);

    otfnv_totVal.setValue(total_val);

    //.

    oCB_SO_LensType.focus();

}

Error Description

Uncaught TypeError: Cannot read property 'getCells' of undefined

    at eval (eval at evaluate (unknown source), <anonymous>:1:21)

    at Object.InjectedScript._evaluateOn (<anonymous>:895:55)

    at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34)

    at Object.InjectedScript.evaluateOnCallFrame (<anonymous>:954:21)

    at AddItemInDetailTable (http://localhost:52125/Deopti_Order_Stock/deopti/StockOrder.view.js:820:11)

    at sap.ui.commons.Button.press (http://localhost:52125/Deopti_Order_Stock/deopti/StockOrder.view.js:468:9)

    at a.fireEvent (http://localhost:52125/Deopti_Order_Stock/resources/sap-ui-core.js:135:59661)

    at a.fireEvent (http://localhost:52125/Deopti_Order_Stock/resources/sap-ui-core.js:144:121162)

    at null.firePress (http://localhost:52125/Deopti_Order_Stock/resources/sap-ui-core.js:144:40628)

    at B.onclick (http://localhost:52125/Deopti_Order_Stock/resources/sap/ui/commons/library-preload.json/sap/ui/commo...)

Qualiture
Active Contributor
0 Kudos

Why aren't you using a model to access your table data?

This will be much easier to read / add data too, instead of using these awful getCells() / getRows() methods (what happens if you sort, or change column positions?)

Former Member
0 Kudos

you are correct i recently made this change in it

 

   var aData = [];

   var tableModel = oTable2.getModel();

   var table_Data = tableModel.getData();

   var trCount = oTable2.mBindingInfos.rows.binding.iLength;      

   if(trCount>0)

      aData = table_Data.modelData;

and removed the for loop but problem is still there

Regards

Qualiture
Active Contributor
0 Kudos

No, what I mean is, why are you accessing your table in the first place? Why not just access your model directly?

After all, SAPUI5 is a model-driven framework, and you should avoid accessing UI controls directly as much as possible..


For instance:



var tableModel = oTable2.getModel();

var tableData = tableModel.getData();


var someData = {

     MATNR : "somevalue",

     ARKTX : "etc",

     //etc

}


tableData.push(someObject);


tableModel.setData(tableData);


will add a new object 'someObject' to your table data, and it will be visible as a new row in your table (provided this object has the same signature as the other data objects in your model)

Former Member
0 Kudos

Dear Robin,

as far as i understand you are saying that instead of adding data directly into any control add into the model, and i think this is what i am doing, if you see the Add AddItemInDetailTable method i am reteriving the old records of the table into an array and then adding new record in that array and again setting the model with the control with new set of records. please eloborate a little how should i do it if you feel that there could be some other better way to do this.

Thx and Regards

Qualiture
Active Contributor
0 Kudos

Let's say your table is bound to a model property "/somedata", which holds an array of objects which are visible in your table.

You then simply do:

var aData = this.getView().getModel().getProperty("/somedata");

aData.push(someObject);

this.getView().getModel().setProperty("/somedata", aData);

that's it, no need to reference any table or other UI element.

Just use the model's getProperty / setProperty methods for manipulating data

Former Member
0 Kudos

ok let me try it then i will getback to you if i get fail... thx again

I tried the suggested way in the following manner


var oBT_SO_AddItem = new sap.ui.commons.Button("oBT_SO_AddItem" , {

      text: "Add Item",

      icon: "images/AddRecord.png",

      press: function(e) {

        if (!oController.validateDetailFields())

          return;

        //AddItemInDetailTable();

        var aData=[];

    aData = this.getView().getModel().getProperty("/modeldata");

    aData.push(

           {

             MATNR   : sap.ui.getCore().byId("oTF_SO_Var_Code").getValue(),

             ARKTX   : oCB_SO_LensType.getLiveValue(),

             VKAUS   : oCB_SO_LensSide.getValue(),

             DIA     : oCB_SO_LensSize.getLiveValue(),

             SPH     : oCB_SO_Lens_SPH.getLiveValue(),

             CYL     : oCB_SO_Lens_CYL.getLiveValue(),

             ZADD    : oCB_SO_Lens_ADD.getLiveValue(),

             KWMENG  : oTF_SO_Quantity.getValue(),

             NETPR   : oTF_SO_LensPric.getValue(),

             ZEXCH   : oFloatFormat.parse(oTF_SO_ExtraChg.getValue()).toFixed(2),

             ZREASON : oTF_SO_ReasonEC.getValue(),

             NETWR   : oMV_SO_LensAmnt.toFixed(3),

             VRKME   : oCB_SO_LensUnit.getSelectedKey(),

             OLDQTY  : oTF_SO_Quantity.getValue(),

             PROQTY  : oTF_SO_Quantity.getValue(),

             POSNR   : lineItemNo,

             AUFLG   : "POST",

             WERKS   : oCB_SO_DelvSite.getSelectedKey()

           }

         );

    this.getView().getModel().setProperty("/modeldata", aData);

and received following message

Uncaught TypeError: this.getView is not a function

former_member182862
Active Contributor
0 Kudos

Hi Muhammad

It is hard to tell without know what you have done. There are many ways to do this.

Here is one

Sample

-D

Former Member
0 Kudos

your example is not mapping my issue you have created 10 rows in table however in my case all the fields creating one record in the table and after doing the same addition process for 10 times its showing error, please look it again

thx.