php - 在复选框中更改管理面板上的角色

标签 php jquery mysql laravel permissions

在呈现的 Controller 中,它显示所有用户及其属性(?),例如姓名、姓氏、电话和角色。当我在复选框中选择角色并按“potvrdi”(提交)它提交到数据库时,我该如何做到这一点?我必须使用 jquery、ajax 吗? 谢谢

Controller

public function action(Request $request)
    {
        if ($request->ajax()) {
            $query = $request->get('query');
            if ($query != '') {
                $data = User::where('surname', 'like', '%'.$query.'%')
                    ->orWhere('name', 'like', '%'.$query.'%')
                    ->orWhere('phone', 'like', '%'.$query.'%')
                    ->orderBy('id')
                    ->get();
            } else {
                $data = User::orderBy('id')
                    ->get();
            }
            return json_encode($this->generateUserTable($data));
        }
    }

    public function generateUserTable($data)
    {
        $total_row = $data->count();
        $output = "";
        if ($total_row > 0) {
            foreach ($data as $row) {
                $roleNames = '';
                $userRoles = $row->roles()->pluck('id')->toArray();
                // var_dump($userRoles);
                $checked = '';
                foreach (Role::all() as $roles1) {
                    if (in_array($roles1->id, $userRoles)) {
                        $checked = 'checked="checked"';
                    }
                    $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
                }
                $output .= '
                    <tr>
                        <td>'.$row->surname.'</td>
                        <td>'.$row->name.'</td>
                        <td>'.$row->phone.'</td>
                        <td>'.$roleNames.'</td>
                        <td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
                        <div class="potvrdi">Potvrdi</div>
                        </button></td>
                        <td><button type="button" id="rowId" class="remove-button btn btn-danger" data-id="'.$row->id.'">
                        <div class="close">&#120;</div>
                        </button></td>
                    </tr>
                ';
            }
        } else {
            $output = '
                <tr>
                    <td align="center" colspan="5">Nema podataka</td>
                </tr>
            ';
        }
        return array(
            'table_data'  => $output,
            'total_data'  => $total_row,
        );
    }

编辑:

部分 html 在 generateUserTable 中 $output 之后的 Controller 中

用js查看

<!-- Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">

            </div>
            <div class="modal-body">
                <h2>{{ $modal }}</h2>
            </div>
            <div class="modal-footer">
                <button type="button" class="rem-mod btn btn-secondary" data-dismiss="modal">Zatvori</button>
                <button type="button" class="bck-mod test btn btn-danger" data-dismiss="modal">Obriši korisnika</button>
            </div>
        </div>
    </div>
</div>
<!-- users and search bar -->
<div class="container">
    <div class="panel panel-default">
    <div class="panel-heading">Pretraži korisnike</div>
        <div class="panel-body">
            <div class="form-group">
                <input type="text" name="search" id="search" class="form-control" placeholder="Pretraži korisnike" />
            </div>
            <div class="table-responsive">
                <h3 align="center">Broj korisnika: <span id="total_records"></span></h3>
                <table id="users" class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Prezime</th>
                            <th>Ime</th>
                            <th>Telefon</th>
                            <th>Rola</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                <tbody>

                </tbody>
                </table>
            </div>
        </div>    
    </div>
</div>

从数据库中获取数据

<script>   
    $(document).ready(function(){
        fetch_user_data();
        function fetch_user_data(query = ''){
            $.ajax({
                url:"{{ route('live_search.action') }}",
                method:'GET',
                data:{query:query},
                dataType:'json',
                success:function(data)
                {
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                }
            })
        }

        $(document).on('click', '.potvrdi-button', function(){
            post_user_role();
            var id = $(this).data('id');
            function post_user_role(id){
                $.ajax({
                    url:"{{ route('live_search.action') }}",
                    method:"GET",
                    data:{id:id},
                    dataType:'json',
                    success:function(data)
                    {
                        $('tbody').html(data.table_data);
                        $('#total_records').text(data.total_data);
                    }
                })
            }
        })

        $(document).on('keyup', '#search', function(){
            var query = $(this).val();
            fetch_user_data(query);
        });

模态js

        $('#users').on('click', '.remove-button', function(){
            var id = $(this).data('id');
            $(".test").attr('data-id', id);
            $("#deleteModal").modal("show");
        });

        $(document).on('click', '.bck-mod', function(){
            var id = $(this).data('id');
            $.ajax({
                //url:"{{ route('live_search.destroy') }}",
                url:"/live_search/destroy",
                method:"get",
                data:{id:id},
                dataType:'json',
                success:function(data)
                {
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                },
                error: function(data)
                {
                    console.log(data);
                }
            })
        });
    });
</script>

最佳答案

我准备了 sample fiddle为你。我为你的用户表使用了虚拟值,我也使用了选择而不是复选框,但你应该遵循相同的方法。

$(document).on('click', '.potvrdi-button', function(e) {
    var value = $(this).closest('tr').find('select[name="role"]').val();
    $.ajax({
      url: "{{ route('live_search.action') }}",
      method: "GET",
      data: {
        value: value
      },
      dataType: 'json',
      success: function(data) {
        $('tbody').html(data.table_data);
        $('#total_records').text(data.total_data);
      }
    })

  })

关于php - 在复选框中更改管理面板上的角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53373045/

相关文章:

javascript - 提交表单时 AJAX Jquery 验证不起作用

mysql - SQL Server 同步到 MySQL 程序

python - 无法在Windows64上安装mysqlclient

javascript - jQuery 地址在 init 上双重加载

Python 在 MySQL 数据库中插入值 <= 1e-7

php - 通过 HTTP 跨域安全地发送数据

javascript - 使用 JavaScript 刷新 div 标签时刷新整个页面

php - 从 php 字符串中删除特定标签

php - Laravel:验证表单中输入的名称

jquery - 如何在asp.net(vb.net)中从母版页调用jquery