Laravel X-editable

Laravel X-editable

我尝试在 laravel 5.5 中使用 X-editable 但无法正常工作。到目前为止,我有这个控制器:

public function updatecommentajax(Request $request)
    {
      return Rating::find($id)->update([
        'comment' => $request->get('comment'),
      ]);
    }

这是我的路线:

Route::post('/updatecommentajax/{id}', 'RatingController@updatecommentajax')->name('updatecommentajax');

这是我的 Blade:

<form action="{{route('updatecommentajax', $rating->id)}}" method="post">
  {{csrf_field()}}
  <td class="text-center">
    <a href="#" id="comment" class="comment" data-url="{{ route('updatecommentajax', $rating->id) }}" data-pk="{{ $rating->id }}" data-type="textarea" data-placement="right" data-title="Edit Comment">{{$rating->comment}}</a>
  </td>
</form>

还有我的JS代码:

<script type="text/javascript">
$(document).ready(function() {
  //toggle `popup` / `inline` mode
  $.fn.editable.defaults.mode = 'popup';

    rating_id = $(this).data('pk');
    url = $(this).data('url');

    //make username editable
    $('#comment').editable({
      url: url,
      pk: rating_id,
      headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
      type:"POST",
      validate:function(value){
        if($.trim(value) === '')
        {
          return 'This field is required';
        }
      }
    });
});
</script>

有人知道问题出在哪里吗?

PS: Somehow only first comment will be selected for edit and others no (guess is because id="comment" but if i change it to {{$rating->id}} I don't know later how to get it in this part $('#comment').editable({

更新

我设法让它工作了,但上面 PS 中的问题仍然存在。这是我的修复代码:

Blade:

<form action="{{route('updatecommentajax', $rating->id)}}" method="post">
  {{csrf_field()}}
  <td class="text-center">
   <label for="comment" hidden></label>
   <a href="#" id="comment" class="comment" data-url="{{ route('updatecommentajax', $rating->id) }}" data-pk="{{ $rating->id }}" data-type="textarea" data-placement="right" data-title="Edit Comment">{{$rating->comment}}</a>
 </td>
</form>

脚本:

<script type="text/javascript">
$(document).ready(function() {
  //toggle `popup` / `inline` mode
  $.fn.editable.defaults.mode = 'inline';
  $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    rating_id = $(this).data('pk');
    url = $(this).data('url');

    //make username editable
    $('#comment').editable({
      url: url,
      pk: rating_id,
      type:"textarea",
      validate:function(value){
        if($.trim(value) === '')
        {
          return 'This field is required';
        }
      }
    });


    $('#rating').editable({
      url: url,
      pk: rating_id,
      type:"select",
      source: [
      {value: 1, text: '1 Star'},
      {value: 2, text: '2 Star'},
      {value: 3, text: '3 Star'},
      {value: 4, text: '4 Star'},
      {value: 5, text: '5 Star'}
      ],
      validate:function(value){
        if($.trim(value) === '')
        {
          return 'This field is required';
        }
      }
    });
});
</script>

已解决

  1. 我的修复代码在我的问题更新部分
  2. 我的 PS 仍然存在 我通过将 $('#comment').editable({ 更改为 $('.comment').editable({ 将 ID 更改为 Class.
  3. 来修复它

希望对其他人有所帮助。