想要使用ajax将活动状态从1更新为0

时间:2018-03-28 05:13:05

标签: javascript ajax laravel-5

我将根据数据库中“active”列的值的状态启用或禁用按钮。我想用javascript或ajax更改按钮状态。并且还应该更新数据库中的“活动”值。

blade.php

                                 <td class="text-center">
                                     <a href="#" data-toggle="modal" data-target="#myModal" class="edit"><i class="fa fa-comment"></i></a>
                                     @if( $joins->active == "1")
                                         <a href="{{route('asset.status',$joins->id)}}" id="return" data-toggle="modal" @if($joins->active === "1") class="edit  action-button" @else class="disabled2" @endif><i class="fa fa-get-pocket"></i></a>
                                     {{--@else--}}
                                     {{--<a href="#" id="return" data-toggle="modal" class="disabled2"><i class="fa fa-get-pocket"></i></a>--}}

                                     @endif
                                 </td>

脚本

<script>
    $(".action-button").click(function(){
        swal({
            type : 'warning',
            title: 'Submit what you want',
            input: 'text',
            showCancelButton: false,
            confirmButtonText: 'Submit',
            showLoaderOnConfirm: true,
            allowOutsideClick: false

        }).$.ajax({
        type: "POST",
        url : 'profile-management/employee-assets/update/{id}',
        data: { id: id },
        success: function(data){
            swal('Confirmed!','success')
        }

        });
    })

</script>

web.php

Route::get('profile-management/employee-assets/update/{id}','AssetController@statusUpdate')->name('asset.status');

Controller.php这样

   public function statusUpdate(Request $request,$id){
        $update = Asset::find($id);
        $update->active = "1";

        $update->save();
        $request->session()->flash('alert-info', 'Product Status Updated!');
        return redirect('/profile-management/employee-assets');
    }

1 个答案:

答案 0 :(得分:1)

你只能使用一个按钮,如打击:

  

刀片

<td class="text-center">
    <a href="#" data-toggle="modal" data-target="#myModal" class="edit"><i class="fa fa-comment"></i></a>
    <a href="{{route('asset.status',$joins->id)}}" id="return" data-toggle="modal" @if($joins->active === "1") class="edit  action-button" @else class="disabled2" @endif><i class="fa fa-get-pocket"></i></a>
</td>
  

PHP控制器:

public function statusUpdate(Request $request,$id){
        $update = Asset::find($id);
        $update->active = !$update->active; // if true, it will give false

        $update->save();
        return \Resposne::json({status: true, active: $update->active});
    }
  

JavaScript的:

使用成功回调中的响应:

<script>
    $(".action-button").click(function(){
        swal({
            type : 'warning',
            title: 'Submit what you want',
            input: 'text',
            showCancelButton: false,
            confirmButtonText: 'Submit',
            showLoaderOnConfirm: true,
            allowOutsideClick: false

        }).$.ajax({
        type: "POST",
        url : 'profile-management/employee-assets/update/{id}',
        data: { id: id },
        success: function(data){
            console.log(data,'data'); // {status: true, active: true/false }

         // Here use data to manipulate the button with id return, mean add/remove classes etc. d


            swal('Confirmed!','success')
        }

        });
    })

</script>