0

I have a jquery click function on a table row that takes the value of the table row and puts it into a list i then set the list equal to the vue array i have here are the arrays. Array:

 tableRow: [
                {
                    "name": "1",
                    "numSongs": "2",
                    "Year": "3"
                }
            ]

Function:

    $("#table1 tr").click(function () {
        var list = [];

        var $row = $(this).closest("tr"),
            $tds = $row.find("td");

        list.push({ name: $tds.eq(0).text(), numSongs: $tds.eq(1).text(), Year: $tds.eq(2).text() });
        this.tableRow =  list;
        console.log(this.tableRow);
    });

I also log the value of the list when the app loads and when i click it and it does change. console output

Then i have a function to test if the values have changed just a simple alert function.

greet: function () {
            // `this` inside methods points to the Vue instance
            alert('Hello ' + this.tableRow[0].name )
        },

But the value doesn't update in the function does anyone know why.

Vue Instance Code

export default {

    data() {
        return {
            tableRow: [
                {
                    "name": "1",
                    "numSongs": "2",
                    "Year": "3"
                }
            ]
        }
    },
    mounted: function () {
        console.log(this.tableRow);
        this.$nextTick(() => {
            $("#table1 tr").click(function () {
                var list = [];

                var $row = $(this).closest("tr"),
                    $tds = $row.find("td");

                list.push({ name: $tds.eq(0).text(), numSongs: $tds.eq(1).text(), Year: $tds.eq(2).text() });
                this.tableRow =  list;
                console.log(this.tableRow);
            });
        });
    },

    methods: {
        greet: function () {
            // `this` inside methods points to the Vue instance
            alert('Hello ' + this.tableRow[0].name )
        },
}
}
2
  • Can you show the vue instance code Commented Jun 29, 2017 at 10:47
  • I've added it to the question just now. Commented Jun 29, 2017 at 10:51

1 Answer 1

3

This is due to the scoping issue of this

In your click handler you are doing this.tableRow = list where tableRow is a property of data option in the vue instance but this is not pointing to the vue instance, it refers to element which invoked the event

So do it like this:

mounted: function () {
    var self = this;
    console.log(this.tableRow);
    this.$nextTick(() => {
        $("#table1 tr").click(function () {
            var list = [];

            var $row = $(this).closest("tr"),
                $tds = $row.find("td");

                list.push({ name: $tds.eq(0).text(), numSongs: $tds.eq(1).text(), Year: $tds.eq(2).text() });
                self.tableRow =  list;
                console.log(this.tableRow);
        });
    });
}, 
Sign up to request clarification or add additional context in comments.

1 Comment

@DanielM96 happy to help :)

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.