在Laravel 5.1和Vue JS中保存多个数据动态表单 [英] Saving multiple data dynamic form in Laravel 5.1 and Vue JS

查看:214
本文介绍了在Laravel 5.1和Vue JS中保存多个数据动态表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须添加/发布数据表单。但是,当用户点击按钮时,表单动态地增加。我已经浏览了它,并得到了一些答案,我得到像使用$ request-> all()从输入表单获取所有数据。

然后我的问题是,我的应用程序使用VueJS作为前端。 VueJS脚本中是否有任何配置来发布动态表单中的所有数据?



我的刀片模板将会动态增加:

 < div id =form-message> 
{!! Form :: text('rows [0] [DestinationNumber]',null,[
'id'=>'recipient',
'class'=>'form-control',
'v-model'=>'newMessage.DestinationNumber'
])
!!}
{!! Form :: textarea('rows [0] [TextDecoded]',null,[
'rows'=>'3',
'id'=>'recipient',
'class'=>'form-control',
'v-model'=>'newMessage.TextDecoded'
])
!!}
< / div> ;

零数字会增加取决于用户点击添加按钮的数量。
然后在这里我的VueJS脚本

  var newSingleMessage = new Vue({
el:'#newsinglemsg ',
data:{
newMessage:{
DestinationNumber:'',
TextDecoded:''
},
},
方法:{
onSubmitForm:function(e){
e.preventDefault();
var message = this.newMessage;

this。$ http.post(' api / outbox',message);
message = {DestinationNumber:'',TextDecoded:''};
this.submitted = true;
}
}

});

在laravel控制器上,我有简单的逻辑来测试数据如何传递。

  $ input = $ request-> all(); 
$ output = dd($ input);
return $ output;

然后,我使用2个额外的表单测试它。所以,数据应该是3行。结果(从FireBug检查)是这样的

  {DestinationNumber:1234567890,TextDecoded:qwertyuio } 

数据只传递一次,然后类型为JSON。即使我使用return $ output-> toArray(),仍然输入JSON。

哦,是的,再一次。 Idk如何使用JavaScript动态增加零号码。测试时,我只是手动添加表单。在这里,我的添加点击功能javascript

  var i = 0,
clone = $('#form-message') .clone(),
recipient = document.getElementById('recipient');
recipient.setAttribute('name','rows ['+ i +'] [DestinationNumber]');
clone.appendTo('。form-message:last');
i ++;

对于第二行和下一行,name属性不会添加到输入元素上。
谢谢

解决方案

您正在以令人困惑的方式混合blade和jquery和vue。看看这个JS小提琴用Vue完成所有这些:



https://jsfiddle.net/cr8vfgrz/10/



您基本上拥有一组使用<$ c自动映射到输入的消息$ C> v-用于。随着这些输入变化,您的消息数组发生变化。然后,当按下提交时,您只需发布 this.messages ,并将消息数组发送到服务器。然后你可以清除数组重置表单。



模板代码:

 < div id =form-message> 
< button class =btn btn-default@ click =addNewMessage>新信息< / button>
<模板v-for =消息中的消息>
< input type =textv-model =message.DestinationNumberclass =form-control>