如何在yajra数据表中的编辑模式按钮中输出动态数据

时间:2019-02-24 12:42:06

标签: jquery ajax laravel bootstrap-modal yajra-datatable

我的Web应用程序具有一个带有编辑按钮的yajra数据表,该数据表在单击时会输出一个模态。问题是我不知道如何使用来自数据表的动态数据输出模态。目前,模态只为每行中生成的所有按钮输出数据表中的第一行。我希望我的模态在每一行上输出特定的数据。我正在使用引导程序4。

这是我的UsersController:

public function yajraDT()
{
    $users = DB::table('users')
    ->join('addresses', 'users.id', '=', 'addresses.user_id')
    ->get();

     return Datatables::of($users)
    ->addColumn('delete', function ( $users ) {
        return view('users.delete', compact('users'))->render();
    })
    ->addColumn('edit', function ( $users ) {
        return view('users.editmodal', compact('users'))->render();
    })
    ->escapeColumns([])
    ->make(true);
}

这是JS脚本:

<script>
     $(function() {
           $('#tableDT').DataTable({
           processing: true,
           serverSide: true,
           ajax: '{{ url('users/yajraDT') }}',
           columns: [
                    { data: 'id', name: 'id', 
                        @if(Auth::check() && Auth::user()->type == "Admin")
                            render:function(data, type, row)
                            {
                                return "<a href='/users/"+ row.id +"'>" + row.id + "</a>"
                            }
                        @endif},
                    { data: 'first_name', name: 'first_name' },
                    { data: 'last_name', name: 'last_name' },
                    { data: 'email', name: 'email' },
                    { data: 'gender', name: 'gender' },
                    @if(Auth::check() && Auth::user()->type == "Admin")
                    { data: 'delete', name: 'delete' },
                    { data: 'edit', name: 'edit' }
                    @endif
                 ]
        });
     });
</script>

如果我将laravel集合形式放置在模态之外,它将变为动态,但是如果将其放置在模态内部,它将变为静态,并且我不知道为什么。任何帮助将不胜感激。

这是editmodal.blade.php:

<a href="#" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Edit</a>


<div class="container">
  <!-- Button to Open the Modal -->
      <div class="modal" id="myModal">
        <div class="modal-dialog">
          <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header">
              <h4 class="modal-title">Modal Heading</h4>
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- Modal body -->
            <div class="modal-body">
                {!! Form::open(['action' => ['UsersController@update', $users->id], 'method' => 'POST']) !!}
                <div class="form-group">
                    {{ Form::label('first_name', 'First Name') }}
                    {{ Form::text('first_name', $users->first_name, ['class' => 'form-control', 'placeholder' => 'First Name']) }}
                </div>
                <div class="form-group">
                    {{ Form::label('last_name', 'Last Name') }}
                    {{ Form::text('last_name', $users->last_name, ['class' => 'form-control', 'placeholder' => 'Last Name']) }}
                </div>
                <div class="form-group">
                    {{ Form::label('city', 'City') }}
                    {{ Form::text('city', $users->city, ['class' => 'form-control', 'placeholder' => 'City']) }}
                </div>
                <div class="form-group">
                    {{ Form::label('barangay', 'Barangay') }}
                    {{ Form::text('barangay', $users->barangay, ['class' => 'form-control', 'placeholder' => 'Barangay']) }}
                </div>
                    {{ Form::hidden('_method', 'PUT') }}
            </div>

            <!-- Modal footer -->
            <div class="modal-footer">
                    {{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}
                {!! Form::close() !!}
            </div>
      </div>
    </div>
  </div>
</div>

0 个答案:

没有答案