Vue.Draggable icon indicating copy to clipboard operation
Vue.Draggable copied to clipboard

view model and view not syncing

Open rawbknalb opened this issue 10 years ago • 7 comments

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

rawbknalb avatar Jul 29 '16 10:07 rawbknalb

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/

David-Desmaisons avatar Jul 29 '16 13:07 David-Desmaisons

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

rawbknalb avatar Jul 29 '16 14:07 rawbknalb

You can take a look at here: http://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array

David-Desmaisons avatar Jul 29 '16 14:07 David-Desmaisons

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 avatar Jul 29 '16 15:07 rawbknalb

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
  }
]

rawbknalb avatar Jul 30 '16 09:07 rawbknalb

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 avatar Jul 30 '16 15:07 David-Desmaisons

Documentation updated.

David-Desmaisons avatar Jul 30 '16 15:07 David-Desmaisons