Skip to Content
avatar image
Former Member

How to delete table rows for a particular table combination.

Hi,

I have table containing two columns and data is retrieved from JSON model bind to table and view. I have to delete particular combination records from oData before binding to table. Below is the code I have used for updating JSON model bind to table.

......

function(odata, response) {

data = {

Items: response.data.DHI.results

};

$.each(data, function(idx, obj) {

if((obj.var1) === "" && (obj.var2) === "0"){

data.splice(idx,1);

}

});

jsonModel.setData(data);

jsonModel.UpdateBindings();

tableID.setModel(jsonModel, "tableData");

tableData is the JSON model of view bind to table.

In this scenario I should get data record deleted from 'data' for a combination of var1 and var2 but not happening. When checked through debugger the values are populating correctly and if statement is also executed for correct combinations. But I am able to see deleted records inside the table again.

Can anyone suggest solution to this.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Apr 16, 2017 at 11:17 PM

    Agreeing with what others have mentioned (do not splice in loop)

    here is an alternative.

    data = data.filter(function(obj) {
      return !(obj.var1 === "" && obj.var2 === "0");
    });
    

    Thanks

    -D

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 30, 2017 at 12:15 AM

    $.each(data, function(idx, obj) {

    if((obj.var1) === "" && (obj.var2) === "0"){

    data.splice(idx,1);

    }

    });

    you are modifying the array while looping. your index is messed up. of course it is issue.

    just write a loop. start from the last element. hopefully you get it.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 30, 2017 at 05:41 AM

    not checked the code thoroughly, but you could fill another array which is matching your condition and bind that array to the model.

    Best Regards,
    Mahesh

    Add comment
    10|10000 characters needed characters exceeded

  • Apr 13, 2017 at 12:41 PM

    try this:

            var index = $.map(data, function(obj, index) {
                if((obj.var1) === "" && (obj.var2) === "0"){
                    return index;
                }
            })
            data.splice(index, 1);

    check this link for example : https://jsfiddle.net/Akhilesh_U/bd91zshc/

    Add comment
    10|10000 characters needed characters exceeded