如何在引导模式中添加功能- Laravel


How to add functions in Bootstrap modal - Laravel

我目前正在用Laravel框架开发一个系统。不过,我对MVC架构还不是很熟悉。

当我开发系统时,我遇到了一个问题,它与这个bootstrap模态有关,其中有一个形式。我想在它里面放一些函数,这样它就可以提交给数据库了。

我遵循了几个教程,但没有想出一个解决方案,因为它们都使用页面而不是情态

那么,这是模态的HTML:

<div class="modal hide fade" id="linkSettings">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">×</button>
    <h3>Add New Link</h3>
  </div>
  <div class="modal-body">
      <div class="control-group">
      <label class="control-label" for="focusedInput">Add A Link:</label>
      <div class="controls">
        <textarea class="autogrow"></textarea>
      </div>
      </div>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <a href="#" class="btn btn-primary">Save changes</a>
  </div>
</div>

下面是上面模态的Javascript代码:

$('.btn-settingLink').click(function(e){
    e.preventDefault();
    $('#linkSettings').modal('show');
});

现在,我如何将函数放在控制器中,以便每当用户按Save Changes按钮时,它将<textarea>中的值存储到数据库中?有什么与Javascript代码有关吗?我是否需要在表单中使用{{Form::open('main/addLink','POST')}}将其值传递给控制器和模型?

基本上,我想要的就是通过使用modal来拥有CRUD函数。

首先你需要创建一个带有路由....的表单

<form method="post" action="{{URL::to('Link/addlink')}}>

或者像你上面展示的那样,用你的刀片{{Form}}…无论哪种方式

你的textarea需要一个id和名称

<textarea id="mytext" name="mytext"></textarea>

那么你的HTML就变成了。

<form method="post" action="{{URL::to('Link/addlink')}}>
  <div class="modal-body">
      <div class="control-group">
      <label class="control-label" for="focusedInput">Add A Link:</label>
      <div class="controls">
        <textarea id="mytext" name="mytext" class="autogrow"></textarea>
      </div>
      </div>
  </div>
  <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <input type="submit" id="submit" name="submit" class="btn btn-primary">Save changes</input>
  </div>
</form>

然后在你的Link控制器中…或者无论你在哪里发送这个表单。不知道你的控制器的名字:)

public function addlink(){
$input = Input::all();
//whatever validation you wanna do, with error handling etc...not gonna provide that,            thats up to you
$yourmodel = New Model;
$yourmodel->text = $input['mytext'];
$yourmodel->save();
Return Redirect::back();
 }

编辑(Ajax)

将此脚本添加到页面底部…

<script>
$(document).ready(function(){
$('input#submit').click(function(e){
   e.preventDefault();
   var text = $('#mytext').val();
   var url = 'your url' //we were using Link/addlink in the other example
  $.ajax({
   type: "POST",
   url: url,
   data: { mytext: text },
   success: function(){
        //Probably add the code to close your modal box here if successful
        $('#linkSettings').hide();
   },
   });
 });
});
</script>

PS完全未经测试....你将需要一些调整,加上你的控制器有重定向的事实可能会使它不工作,因为这将只是期待一个返回Json字符串

但这些都是具体细节,我认为你可以自己继续做下去。