【问题标题】:How to add custom actions against any button in Laravel?如何针对 Laravel 中的任何按钮添加自定义操作?
【发布时间】:2019-04-14 21:26:41
【问题描述】:

我正在制作一个 Web 应用程序,其中有两个表:类和部分。我的迁移如下所示。

Schema::create('classes', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

部分

Schema::create('sections', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->integer('class_id')->unsigned();
    $table->foreign('class_id')->references('id')->on('classes');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->timestamps();
});

在我的 Blade 文件中,我想显示所有类的按钮。当我单击该按钮时,它应该显示部分表中 class_id 属于所选按钮的所有部分。

我被语法卡住了。我是 Laravel 的新手,不知道如何为按钮添加响应。

我的部分控制器

public function index()
{
    $sections = Section::all();
    $class = Class::all();
    $users = User::all();

    return view('sections.index')->with('sections', $sections)->with('class', $class)->with('users', $users);
}

刀片

@foreach($classs as $cat)

    <button class="btn btn-info">{{$cat->title}}</button>

    //this shows all buttons with class name

@endforeach

<div class="box-body">
    <table class="table table-responsive">
        <thead>
        <tr>
            <th>Section Name</th>
            <th>Class</th>
            <th>Teacher</th>
            <th>Modify</th>
        </tr>
        </thead>

如何在这些表格下获取仅选定类的所有部分?

这是建议的代码,我在其中进行了更改,单击按钮时我没有获取数据。请告诉我我该怎么办?我必须更换控制器吗

@foreach($classs as $cat)
    <button class="btn btn-info class_btn" id="{{$cat->class_id}}">{{$cat->title}}</button>
    <div class="box-body" style="display:none" id="table_{{$cat->class_id}}">
        <table class="table table-responsive">
            <thead>
            <tr>
                <th>Section Name</th>
                <th>Class</th>
                <th>Teacher</th>
                <th>Modify</th>
            </tr>
            </thead>
            <tbody>
            @foreach($sections as $sec)
                <tr>
                    <td>{{$sec->title}}</td>
                    <td>
                        {{ \Illuminate\Support\Facades\DB::table('classses')->where('id',$sec->class_id)->value('title')}}
                    </td>
                    <td>
                        {{ \Illuminate\Support\Facades\DB::table('users')->where('id',$sec->user_id)->value('name')}}
                    </td>
                    <td>
                        <button class="btn btn-info" data-mytitle="{{$sec->title}}" data-myclassid="{{$sec->class_id}}"  data-myuserid="{{$sec->user_id}}" data-secid={{$sec->id}} data-toggle="modal" data-target="#edit">Edit</button>
                        /
                        <button class="btn btn-danger" data-secid={{$sec->id}} data-toggle="modal" data-target="#delete">Delete</button>
                    </td>
                </tr>

            @endforeach
            </tbody>
        </table>
    </div>
@endforeach
<script>
    //onclick show particular table
    $('.class_btn').on('click',function(){
        $('.box-body').hide();
        $('#table_'+$(this).attr('class_id')).show();
    });
    </script>

【问题讨论】:

    标签: php laravel laravel-5 button eloquent


    【解决方案1】:

    你使用 Jquery 和 hide() ,show() 函数将特定的表显示给特定的类 Like As

    @foreach($class as $cat)
        <button class="btn btn-info class_btn" id="{{Your_class_id}}">{{$cat->title}}</button>
        <div class="box-body" style="display:none" id="table_{{Your_class_id}}">
            <table class="table table-responsive">
                <thead>
                <tr>
                    <th>Section Name</th>
                    <th>Class</th>
                    <th>Teacher</th>
                    <th>Modify</th>
                </tr>
                </thead>
                <tbody>
                    //print your data
                </tbody>
            </table>
        </div>
    @endforeach
    <script>
    //onclick show particular table
    $('.class_btn').on('click',function(){
        $('.box-body').hide();
        $('#table_'+$(this).attr('id')).show();
    });
    <script>
    

    另一种使用 Ajax 的方式....

    【讨论】:

    • 请检查我是否已更新代码,但我没有得到任何期望类按钮
    • 你添加了 Jquery 吗?
    • 是的,你写的那个脚本函数
    • You Wrong $(this).attr('class_id') write this is correct $(this).attr('id')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2014-03-22
    • 2020-04-26
    • 2021-04-25
    • 2018-04-13
    相关资源
    最近更新 更多