0

In our application, I added a validation of each row that you can't have 2 same columns values. I'm done with it but Im having a problem in delete row

Example

enter image description here

There is a delete button in the right side when I clicked it, it gives me an error Cannot read property ..... even tho it is already declared in my data

Error Message

enter image description here

Vue code

    deleteRow(i, e){
    var month                   = this.added_items[i].selected_month.id
    var chart_of_account_id     = this.added_items[i].selected_chart_of_account.id
    var count                   = 0 
    // var duplicate                = false 
    this.submitStatus                                       =   ''
    this.duplicate_month                                     =   false
    this.added_items.filter(function( obj ) {
        console.log(this.submitStatus)
        if((obj.selected_month.id == month && obj.selected_chart_of_account.id == chart_of_account_id) && obj.id != i){
             count++
             if(count >= 2)
             {
                // this.added_items[i].duplicate_month                 =   true
                // this.submitStatus                                   =   'ERROR'
                // this.duplicate_month                                =   true
             }
        }
        return obj.id !== i;

    });
   
   console.log(this.added_items)

    e.preventDefault()
},

Question: How can I call my data inside of my filter function? because when I tried any of these: this.submitStatus/this.added_items[i].duplicate_month/this.duplicate_month it gives me an error

4
  • 2
    You need to add the full error details to get any meaningful help. You cut the error message right before the useful information. Commented Dec 21, 2020 at 7:55
  • 2
    aah.. You are inside a different function(){} callback, noticed that? this is different there. Usual mistake. Commented Dec 21, 2020 at 7:58
  • Yes, so how can I access my data if it is inside of a different function? Commented Dec 21, 2020 at 8:01
  • 2
    Check this answer. One other option is save this to a local variable outside the callback. then use that variable. Commented Dec 21, 2020 at 8:02

1 Answer 1

1

You can approach it in two ways

Option 1: Arrow Functions

this.added_items.filter(( obj ) => {
    console.log(this.submitStatus)

    if(
        (
            obj.selected_month.id == month && 
            obj.selected_chart_of_account.id == chart_of_account_id
        ) && 
        obj.id != i
    ){
       count++
       if(count >= 2)
       {
           // this.added_items[i].duplicate_month                 =   true
           // this.submitStatus                                   =   'ERROR'
           // this.duplicate_month                                =   true
        }
   }

    return obj.id !== i;

});

Option 2: Bind this

this.added_items.filter(function( obj ) {
    console.log(this.submitStatus)

    if(
        (
            obj.selected_month.id == month && 
            obj.selected_chart_of_account.id == chart_of_account_id
        ) && 
        obj.id != i
    ){
        count++
        
        if(count >= 2)
        {
                // this.added_items[i].duplicate_month                 =   true
                // this.submitStatus                                   =   'ERROR'
                // this.duplicate_month                                =   true
        }
    }

    return obj.id !== i;

}).bind(this);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.