【问题标题】:Using Datatables in Laravel在 Laravel 中使用数据表
【发布时间】:2017-12-01 00:35:53
【问题描述】:

您好,我在我的 laravel 应用程序上使用来自 https://datatables.yajrabox.com/eloquent/add-edit-remove-column 的数据表

在我的代码中,我在“操作”列下设置了编辑按钮,用作保存表单的模式的数据切换。 现在我正在尝试使用当前单击的编辑按钮使用表格行中的数据填充模式。解决此问题的最佳方法是什么。我尝试了几种方法都无济于事,包括尝试使用 jquery 来获取兄弟元素的值并将它们放入模态表单中的相应输入字段中。在返回编辑按钮的标记之前,我还尝试在我的控制器中调用一个方法来查找相关用户并将其返回到视图中。一切都无济于事。下面是我的代码,最好的方法是什么?

亲切的问候

我的看法:

@extends('layouts.master')
<?php
echo $_GET['edit-2'];
?>
  @section('content')
  <table class="table table-bordered" id="users-table">
    <thead>
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Email</th>
        <th>Created At</th>
        <th>Updated At</th>
        <th>Action</th>
      </tr>
    </thead>
  </table>
  
  
  <!-- Modal -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">New Student</h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal" role="form" method="POST" action="/createStudent">
            {{ csrf_field() }}

            <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
              <label for="name" class="col-md-4 control-label">FullName</label>

              <div class="col-md-6">
                <input id="name" type="text" class="form-control" name="name" value="" required autofocus> @if ($errors->has('name'))
                <span class="help-block">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span> @endif
              </div>
            </div>

            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
              <label for="email" class="col-md-4 control-label">E-Mail Address</label>

              <div class="col-md-6">
                <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required> @if ($errors->has('email'))
                <span class="help-block">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span> @endif
              </div>
            </div>

            <div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
              <label for="phone" class="col-md-4 control-label">Phone Number</label>

              <div class="col-md-6">
                <input id="phone" type="text" class="form-control" name="phone" required> @if ($errors->has('phone'))
                <span class="help-block">
                                        <strong>{{ $errors->first('phone') }}</strong>
                                    </span> @endif
              </div>
            </div>

            <div class="form-group">
              <label for="DOB" class="col-md-4 control-label">Date of Birth</label>

              <div class="col-md-6">
                <input id="DOB" type="date" class="form-control" name="DOB" required>
              </div>
            </div>
            <fieldset class="offset-4">
              <div class="form-group">
                <label class="col-xs-3 control-label">Gender</label>
                <div class="col-xs-9">
                  <div class="radio">
                    <label>
                             <input id="inlineradio1" name="gender" value="male" type="radio">
                             Male</label>
                  </div>
                  <div class="radio">
                    <label>
                             <input id="inlineradio1" name="gender" value="female" type="radio">
                             Female</label>
                  </div>

                </div>
              </div>
            </fieldset>

            <div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
              <label for="qualification" class="col-md-4 control-label">Qualification</label>

              <div class="col-md-6">
                <input id="qualification" type="text" class="form-control" name="qualification" required> @if ($errors->has('qualification'))
                <span class="help-block">
                                        <strong>{{ $errors->first('qualification') }}</strong>
                                    </span> @endif
              </div>
            </div>

            <div class="form-group{{ $errors->has('course') ? ' has-error' : '' }}">
              <label for="Course" class="col-md-4 control-label">Course</label>

              <div class="col-md-6">
                <input id="course" type="text" class="form-control" name="course" required> @if ($errors->has('phone'))
                <span class="help-block">
                                        <strong>{{ $errors->first('course') }}</strong>
                                    </span> @endif
              </div>
            </div>

            <div class="form-group{{ $errors->has('amount') ? ' has-error' : '' }}">
              <label for="amount" class="col-md-4 control-label">Amount Paid</label>

              <div class="col-md-6">
                <input id="amount" type="number" class="form-control" name="amount" required> @if ($errors->has('amount'))
                <span class="help-block">
                                        <strong>{{ $errors->first('amount') }}</strong>
                                    </span> @endif
              </div>
            </div>



            <div class="form-group">
              <div class="col-md-6 col-md-offset-4">
                <button type="submit" class="btn btn-primary">
                                    Register
                                </button>
              </div>
            </div>
          </form>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>
  @stop @push('scripts')
  <script>
    $(function() {
      $('#users-table').DataTable({
        processing: true,
        serverSide: true,

        ajax: '{!! route('
        datatables.data ') !!}',
        columns: [{
            data: 'id',
            name: 'id'
          },
          {
            data: 'name',
            name: 'name'
          },
          {
            data: 'email',
            name: 'email'
          },
          {
            data: 'created_at',
            name: 'created_at'
          },
          {
            data: 'updated_at',
            name: 'updated_at'
          },
          {
            data: 'action',
            name: 'action',
            orderable: false,
            searchable: false
          }
        ]

      });


    });
  </script>
  @endpush

我的控制器方法:

public function getAddEditRemoveColumnData() {
  $users = User::select(['id', 'name', 'email', 'password', 'created_at', 'updated_at']);

  return Datatables:: of ($users) ->
    addColumn('action', function($user) {
      //DatatablesController::current_user($user);
      //User::current_user($user->id);

      return '<a href="#edit-'.$user - > id.
      '" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#myModal"><i class="glyphicon glyphicon-edit"></i> Edit</a>';



    }) ->
    editColumn('id', 'ID: {{$id}}') ->
    removeColumn('password') ->
    make(true);


}

【问题讨论】:

  • 我对添加和编辑按钮感到困惑?这两个按钮触发相同的模式吗?
  • 是的,但请忽略添加按钮。我只是想知道锄头我可以将数据从表格上的一行复制到模态框上的表格

标签: php laravel datatable


【解决方案1】:

为数据表中的编辑按钮添加列

return Datatables::of($User)->addColumn('action', function ($User) { return 'id.'" class="edit-modal btn btn-circle btn-xs btn-info" title="编辑">编辑 ; })->removeColumn("id")->make(true);

现在通过类名(edit-modal)调用 ajax,并在控制器的 url 中使用发送的用户 ID 调用 gat 数据

  $(document).on('click', '.edit-modal', function() {
  $('#(your form-id)').show();
  $.ajax({
    type: 'get',
    url: "{{ url('/user/') }}/"+$(this).data('id'),
    dataType: 'json',
    success: function(data) {

      $('your-input').val(data.id);
      $('your-2-input').val(data.name);
      ......


    },
    error: function(data){

    }
  });

  $('#(your modal-id)').modal('show');
});

【讨论】:

    【解决方案2】:

    从这个链接:https://datatables.net/reference/api/row().index(),你可以得到一行的索引。

    因此,对于编辑按钮的功能,您需要将行索引传递给它。

    单击编辑按钮后,您现在可以使用传递给函数的索引获取数据,并将其设置为表单上输入的值。

    我自己无法尝试,但我认为它会起作用。

    另请阅读:https://github.com/yajra/laravel-datatables/issues/699 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      相关资源
      最近更新 更多