view model and view not syncing
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

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:

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
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/
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
You can take a look at here: http://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array
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);
});
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
}
]
Better to receive directly the data as array from the backend! I will document the limitation of v-dragable-for and close the issue.
Documentation updated.