Skip to Content
0

How to delete table rows for a particular table combination.

Mar 29, 2017 at 09:30 PM

122

avatar image
Former Member

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

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

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

Share
10 |10000 characters needed characters left characters exceeded
Jun Wu Mar 30, 2017 at 12:15 AM
0

$.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.

Share
10 |10000 characters needed characters left characters exceeded
Mahesh kumar palavalli Mar 30, 2017 at 05:41 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded
Akhilesh Upadhyay Apr 13, 2017 at 12:41 PM
0

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/

Share
10 |10000 characters needed characters left characters exceeded