【发布时间】:2018-05-30 18:55:33
【问题描述】:
在 Laravel 中,我需要将行 ID 指定为请求 URL 的一部分以便更新它,例如:http://localhost/contacts/16
问题在于使用 jQuery-Tabledit 时,因为它在初始化时需要一个 URL(在页面加载时)。
那么问题来了,如何在jQuery-Tabledit中将当前行ID设置为URL?
HTML:
<table class="table table-striped table-bordered" id="contactsTable">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th class="tabledit-toolbar-column"></th>
</tr>
</thead>
<tbody>
@if($contacts)
@foreach ($contacts as $contact)
<tr id="{{$contact->id}}">
<td><span class="tabledit-span tabledit-identifier">{{$contact->id}}</span><input class="tabledit-input tabledit-identifier" type="hidden" name="id" value="{{$contact->id}}" disabled=""></td>
<td class="tabledit-view-mode"><span class="tabledit-span">{{$contact->first_name}}</span><input class="tabledit-input form-control input-sm" type="text" name="first_name" value="{{$contact->first_name}}" style="display: none;" disabled=""></td>
<td class="tabledit-view-mode"><span class="tabledit-span">{{$contact->last_name}}</span><input class="tabledit-input form-control input-sm" type="text" name="last_name" value="{{$contact->last_name}}" style="display: none;" disabled=""></td>
</tr>
@endforeach
@endif
</tbody>
JavaScript:
<script>
$( document ).ready(function() {
$('#contactsTable').Tabledit({
url: 'http://localhost/contacts/22',
saveButton: false,
columns: {
identifier: [0, 'id'],
editable: [[1, 'first_name'], [2, 'last_name']]
}
});
});
如您所见,url: 'http://localhost/contacts/22' 是问题所在。上面的代码按预期工作,但只更新第 22 行,因为 jQuery-Tabledit URL 被修复编码到第 22 行。
【问题讨论】:
标签: javascript jquery ajax laravel tabledit