0

I have a jquery function that converts an table row into an array.

 $("#table1 tr").click(function () {
                // alert($(this).text());
                //  alert($(this).children("td").html());
                // app.greet();
                var $row = $(this).closest("tr"),       // Finds the closest row <tr> 
                    $tds = $row.find("td");             // Finds all children <td> elements

                $.each($tds, function () {               // Visits every single <td> element
                    // console.log($(this).text());
                    list.push($tds);
                    // Prints out the text within the <td>
                });
                console.log(list.length)
            });

I want to then convert the jquery array into my vue array which is structured this way.

 data() {
        return {
            tableRow: [
                {
                    "name": "name1",
                    "numSongs": "joker",
                    "Year": "year"
                }
            ]
        }
    },

This is the way i was trying to do it but it im getting a unexpected end of json input error.

 this.tableRow = JSON.parse(list);

Does anyone have any suggestions.

1
  • show pls console.log(list) Commented Jun 28, 2017 at 13:40

1 Answer 1

1

Json.Parse converts string to object, so you can't use it, try this:

       $("#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()});
        });

You can change $tds.eq(index).text() to retrieve cell value.

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.