I have a form that looks like this, all variables are present when I DD all is OK in (screenshot 1) dd screen, all ok,
But the selected variables are not displayed in the modal window picture 2 Here (screenshot 2) Not showing selected in Modal, the Form:
@foreach ($products as $product)
{!! Form::model($products, [ 'method' => 'PATCH', 'route' => ['products.update', $product->id ], 'id' => 'frmProducts', 'name' => 'frmProducts','role'=>'form', 'novalidate' => '','class'=>'form-horizontal']) !!}
<div class="form-group">
<i class="fa fa-question-circle" aria-hidden="true" title="Vælg flere med ctrl tasten nede"></i>
{!! Form::label('state_list', 'Vælg tilstande: ', ['class' => 'col-sm-4 control-label']) !!}
<div class="col-sm-7">
{!! Form::select('state_list[]', $states, $product->state_list, ['class' => 'form-control', 'multiple' => true , 'id' => 'state_list', 'data-placeholder' => 'Vaelg en tilstand']) !!}
</div>
</div>
{!! Form::close() !!}
@endforeach
I think it is something with javascript? but do not know where to search for the error ??
var url = "/products";
$('.collapse').on('show.bs.collapse', function () {
$('.collapse.in').collapse('hide');
});
//display modal form for product editing todos
$(document).on('click','.open_product_modal',function(){
var product_id = $(this).val();
$.get(url + '/' + product_id, function (data) {
//success data formmater dato
console.log(data);
$('#product_id').val(data.id);
$('#state_list').val(data.state_list);
$('#visible').val(data.visible);
$('#btn-save').val("update");
$('#myModal').modal('show');
})
});
//display modal form for creating new product
$('#btn_add').click(function(){
$('#btn-save').val("add");
$('#frmProducts').trigger("reset");
$('#myModal').modal('show');
});
//delete product and remove it from list
$(document).on('click','.delete-product',function(){
var product_id = $(this).val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
})
$.ajax({
type: "DELETE",
url: url + '/' + product_id,
success: function (data) {
console.log(data);
$("#product" + product_id).remove();
},
error: function (data) {
console.log('Error:', data);
}
});
});
//create new product / update existing product date
$("#btn-save").click(function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
})
e.preventDefault();
var formData = {
name: $('#name').val(),
details: $('#details').val(),
productgroup_id: $('#productgroup_id').val(),
state_list: $('#state_list').val(),
}
//used to determine the http verb to use [add=POST], [update=PUT]
var state = $('#btn-save').val();
var type = "POST"; //for creating new resource
var product_id = $('#product_id').val();;
var my_url = url;
if (state == "update"){
type = "PUT"; //for updating existing resource
my_url += '/' + product_id;
}
console.log(formData);
$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success: function (data) {
console.log(data);
var product = '<tr id="product' + data.id + '"><td>' + data.id + '</td><td>' + data.name + '</td><td>' + data.price + '</td><td>' + data.productunit + '</td><td>' + data.productgroup + '</td> <td>' + data.updated_at + '</td>';
product += '<td><button class="btn btn-warning btn-detail open_product_modal" value="' + data.id + '"><i class="fa fa-pencil-square-o" title="Edit" aria-hidden="true"></i></button>';
product += ' <button class="btn btn-danger btn-delete delete-product" value="' + data.id + '"><i class="fa fa-trash-o" title="Delete" aria-hidden="true"></i></button></td></tr>';
if (state == "add"){ //if user added a new record dato formater
$('#products-list').append(product);
}else{ //if user updated an existing record
$("#product" + product_id).replaceWith( product );
}
$('#frmProducts').trigger("reset");
$('#myModal').modal('hide')
},
error: function (data) {
console.log('Error:', data);
{
var errors = '';
for(datos in data.responseJSON){
errors += data.responseJSON[datos] + '<br>';
}
$('#response').show().html(errors); //this is my div with messages
}
}
});
});
PLEASE Help :-)
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire