In Laravel I need to specify the row ID as part of the request URL in order to update it, for example: http://localhost/contacts/16
在Laravel中,我需要将行ID指定为请求URL的一部分,以便更新它,例如:http://localhost/contacts/16
The problem is when using jQuery-Tabledit, as it requires an URL while initializing (on page load).
问题是在使用jQuery-Tabledit时,因为它在初始化(在页面加载上)时需要一个URL。
So the question is, how to set the current row ID as URL in jQuery-Tabledit?
问题是,如何将当前行ID设置为jQuery-Tabledit中的URL ?
HTML:
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:
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']]
}
});
});
As you can see, url: 'http://localhost/contacts/22' is the problem. The code above works as expected, but updates only row 22 as jQuery-Tabledit URL is fix-coded to row 22.
如您所见,url:“http://localhost/contacts/22”是问题所在。上面的代码如预期的那样工作,但是只更新第22行,因为jQuery-Tabledit URL被修改为第22行。
2 个解决方案
#1
1
I assume that your jquery its located within the current blade. Therfore these bracket: {{ }}, {!! !!}
only will be considered as templating when you use the extension .blade.php.
我假设您的jquery位于当前的blade中。前面的括号:{},{!! !当您使用扩展名.blade.php时,才会将}视为模板。
So what you could do its simply use blade to echo the contact id
using the brackets.
所以你可以做的只是使用blade来使用括号回显接触id。
<script>
$( document ).ready(function() {
$('#contactsTable').Tabledit({
url: 'http://localhost/contacts/'+{!! $contact->id !!},
saveButton: false,
columns: {
identifier: [0, 'id'],
editable: [[1, 'first_name'], [2, 'last_name']]
}
});
});
or just serialize the contact collection to json:
或者将联系人集合序列化为json:
$('#contactsTable').on('click', function(){
let contacts = {!! $contacts->toJson() !!};
// now you can access all attributes that are defined in Contacts model
// example contact id
let contact_id = contacts.id;
}
#2
1
Managed to solve it on the server side, created a separate Route just for updating the data via Tabledit which does not require the row ID as part of the URL (I included it in the POST request):
在服务器端解决了这个问题,创建了一个单独的路由,通过Tabledit更新数据,不需要行ID作为URL的一部分(我把它包含在POST请求中):
Route::post('/contacts/updatetable','ContactsController@updateTable');
Controller:
控制器:
use Illuminate\Http\Request;
class ContactsController extends Controller
{
...
public function updateTable(Request $request)
{
// Save the data.
$contact = Contact::find($request->input('id'));
}
...
}
But still would be nice to know if there is any way on client-side (jQuery-Tabledit).
但是如果知道在客户端(jQuery-Tabledit)上是否有任何方法,那就更好了。
#1
1
I assume that your jquery its located within the current blade. Therfore these bracket: {{ }}, {!! !!}
only will be considered as templating when you use the extension .blade.php.
我假设您的jquery位于当前的blade中。前面的括号:{},{!! !当您使用扩展名.blade.php时,才会将}视为模板。
So what you could do its simply use blade to echo the contact id
using the brackets.
所以你可以做的只是使用blade来使用括号回显接触id。
<script>
$( document ).ready(function() {
$('#contactsTable').Tabledit({
url: 'http://localhost/contacts/'+{!! $contact->id !!},
saveButton: false,
columns: {
identifier: [0, 'id'],
editable: [[1, 'first_name'], [2, 'last_name']]
}
});
});
or just serialize the contact collection to json:
或者将联系人集合序列化为json:
$('#contactsTable').on('click', function(){
let contacts = {!! $contacts->toJson() !!};
// now you can access all attributes that are defined in Contacts model
// example contact id
let contact_id = contacts.id;
}
#2
1
Managed to solve it on the server side, created a separate Route just for updating the data via Tabledit which does not require the row ID as part of the URL (I included it in the POST request):
在服务器端解决了这个问题,创建了一个单独的路由,通过Tabledit更新数据,不需要行ID作为URL的一部分(我把它包含在POST请求中):
Route::post('/contacts/updatetable','ContactsController@updateTable');
Controller:
控制器:
use Illuminate\Http\Request;
class ContactsController extends Controller
{
...
public function updateTable(Request $request)
{
// Save the data.
$contact = Contact::find($request->input('id'));
}
...
}
But still would be nice to know if there is any way on client-side (jQuery-Tabledit).
但是如果知道在客户端(jQuery-Tabledit)上是否有任何方法,那就更好了。