Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

view model and view not syncing #9

Closed
rawbknalb opened this issue Jul 29, 2016 · 7 comments
Closed

view model and view not syncing #9

rawbknalb opened this issue Jul 29, 2016 · 7 comments

Comments

@rawbknalb
Copy link

rawbknalb commented Jul 29, 2016

Hi David,

i have a problem with the syncing to the model. I tested v-dragable-for with

data: {
    list:[
        {name:"John"},
        {name:"Joao"},
        {name:"Jean"}
    ]

which works fine. It is a Array of objects. Now my Model is not a Array of objects. It is a Object of Objects.

Then the syncing to the view doesn't work. Here is how i get my Questiongroups:

getQuestiongroups: function(){
    this.$http.get('admin/osamaker/api/group/get')
    .then(function (data){
        this.$set('questiongroups', data.data);
    })
    .catch(function (err){
        console.log(err);
    });

The result is a Object with the name 'questiongroups'. Here is a Screenshot

bildschirmfoto 2016-07-29 um 12 08 48

now there is an empty questions-array in each questiongroup-object. I have a method where i create and save new questions to a questiongroup. After the saveAction is done the questiongroups-object looks like:

bildschirmfoto 2016-07-29 um 12 15 57

So now its Object(questiongroups) > Object(questiongroup) > Object(questions) > Object(question)

I want to reorder the Questions so that every key = "question_order" (marked red) gets the new $index. This doesnt work. The $index is not changing after draging/sorting the list.

So thats for the beginning. I will provide a jsfiddle as soon as possible :-)

edit: here is the promised jsfiddle
https://jsfiddle.net/38a9sq4t/

best regards,
Robin

@David-Desmaisons
Copy link
Member

Hello Robin, v-dragable-for diretive works when binding with array but not with objects. In you case you can transform your questiongroups object into an array and it will work:. Here is a workling example: https://jsfiddle.net/dede89/t81zfo98/

@rawbknalb
Copy link
Author

how do i transform my questiongroups object into an array? i see, it works as an array.
this would help me a lot. thanks already

@David-Desmaisons
Copy link
Member

@rawbknalb
Copy link
Author

rawbknalb commented Jul 29, 2016

thanks. is there a way in vueJS to set data to an array instead of an object? I use

                getQuestiongroups: function(){
                    this.$http.get('admin/osamaker/api/group/get')
                        .then(function (data){
                            this.$set('questiongroups', data.data);

                    })
                    .catch(function (err){
                        console.log(err);
                    });

@rawbknalb
Copy link
Author

I found a way:
the data comes from a model query through my php controller. with array_values() it is possible to get the object as an array.

    public function getAction(){
        $questiongroups = Questiongroup::query()->related('questions')->get();

        foreach($questiongroups as $questiongroup){
            $questiongroup->questions = array_values($questiongroup->questions);
        }


        return array_values($questiongroups);
    }
                           Questions Json 

                            [
  {
    "id": 77,
    "questiongroup_id": 1,
    "text": "Question 1",
    "question_order": 1,
    "options": [],
    "data": [],
    "created_at": {
      "date": "2016-07-30 09:03:17.000000",
      "timezone_type": 3,
      "timezone": "UTC"
    },
    "modified_at": null
  },
  {
    "id": 78,
    "questiongroup_id": 1,
    "text": "Question 2",
    "question_order": 1,
    "options": [],
    "data": [],
    "created_at": {
      "date": "2016-07-30 09:03:21.000000",
      "timezone_type": 3,
      "timezone": "UTC"
    },
    "modified_at": null
  }
]

@David-Desmaisons
Copy link
Member

Better to receive directly the data as array from the backend!
I will document the limitation of v-dragable-for and close the issue.

@David-Desmaisons
Copy link
Member

Documentation updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants