使用控制器内的按钮

问题描述 投票:0回答:2

我正在尝试使用ajax在面板中搜索用户。但除此之外,我还想编辑选定的用户。

我在控制器内部创建了按钮来实现此功能,但按钮不起作用。

这是我的控制器代码:

public function searchTeacher(Request $request){
if($request->ajax()){
    $output = '';
    $query = $request->get('query');
    if($query != '')
    {
        $data = DB::table('users')->where('name', $query)->get();
    }else{
        $data = DB::table('users')->get();
    }
    $total_row = $data->count();
    if($total_row > 0)
    {
        foreach($data as $row)
        {
            $output .= '
            <tr>
            <td>'.$row->name.'</td>
            <td>'.$row->email.'</td>
            <td>'.$row->teacherId.'</td>
            <td>'.$row->subject.'</td>
            <td> <button href={{route(update-teacher-view), '.$row->id.'}}> Edit</button> </td>
            </tr>
            ';
        }
    }
    else
    {
        $output = '
        <tr>
        <td align="center" colspan="5">No Data Found</td>
        </tr>
        ';
    }
    $data = array(
        'table_data'  => $output,
        'total_data'  => $total_row
    );

    echo json_encode($data);
}
}

这是搜索方法的ajax-

<script>
$(document).ready(function(){

    fetch_customer_data();

    function fetch_customer_data(query = '')
    {
        console.log(query)
        $.ajax({
            url:"{{ route('search-teacher') }}",
            method:'GET',
            data:{query:query},
            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_customer_data(query);
    });
});

这是我的HTML-

<section class="panel">
            <table class="table table-striped">
                <thead class="team-list chat-list-side info border-less-list">
                    <th>Teacher Name</th>
                    <th>Email</th>
                    <th>Teacher Id</th>
                    <th>Subject</th>
                </thead>

                <tbody>
                </tbody>
                </table>
            </section>

一个解决方案将不胜感激。

提前致谢。

ajax laravel
2个回答
0
投票

也许这是一个错误的字符串插值?应该是这样的:

<?php
$output .= '
    <tr>
    <td>'.$row->name.'</td>
    <td>'.$row->email.'</td>
    <td>'.$row->teacherId.'</td>
    <td>'.$row->subject.'</td>
    <td> <button href='.route('update-teacher-view', $row->id).'> Edit</button> </td>
    </tr>
    ';

而不是echo-ing,只返回值,它将自动转换为JSON响应:

// echo json_encode($data); // Change this line to below
return $data;

0
投票

使用数据渲染刀片文件,这是最佳解决方案。

在控制器中

return View::make('ajaxFilter',compact('data'))->render();

在ajaxFilter.blade.php上

<section class="panel">
    <table class="table table-striped">
        <thead class="team-list chat-list-side info border-less-list">
            <th>Teacher Name</th>
            <th>Email</th>
            <th>Teacher Id</th>
            <th>Subject</th>
        </thead>
        <tbody>
            @if($data->count() > 0)
                @foreach($data as $row)
                    <tr>
                        <td>{{ $row->name }}</td>
                        <td>{{ $row->email }}</td>
                        <td>{{ $row->teacherId }}</td>
                        <td>{{ $row->subject }}</td>
                        <td>
                            <a href="{{ route('update-teacher-view',$row->id) }}">Edit</a> //button design
                        </td>
                    </tr>
                @endforeach
            @else
                No data Found
            @endif
        </tbody>
    </table>
</section>

在ajax响应中,您可以附加类似的HTML数据

$('#div_or_section_id').html(data);
© www.soinside.com 2019 - 2024. All rights reserved.