Hi can somebody please guide me how to implement inline table edit with select2 in laravel livewire?
I am having problem in my select as I can change the selected option in my select2 but the value I am getting upon submitting the form is the same as the original value.
Please see code below.
Table
#foreach ($purchaserequestitems as $index => $purchaserequestitem)
<tr >
<td><label class='label label-primary'>{{ $loop->iteration }}</label></td>
<td>
#if($editedPRItemsIndex !== $index)
<label>
{{ $purchaserequestitem['item']['name'] }}
</label>
<br/>
<small>
<label>{{ $purchaserequestitem['item']['code'] }}</label>
</small>
#else
<select wire:model.defer="purchaserequestitems.{{ $index }}.item_id" data-index="{{ $index }}" wire:change="getUomByItemId" class="form-control select2 item">
<option value="">-- Please Select -- </option>
#foreach ($items as $item)
<option value="{{ $item->id }}" >{{ $item->name }}</option>
#endforeach
</select>
#endif
</td>
<td>
#if ($editedPRItemsIndex !== $index)
{{ $purchaserequestitem['quantity'] }}
#else
<input type="text" wire:model.defer="purchaserequestitems.{{ $index }}.quantity" class="form-control">
#endif
</td>
<td>
#if ($editedPRItemsIndex !== $index)
{{ $purchaserequestitem['itemuomconversion']['unitmeasure']['code'] }}
#else
<select wire:model.defer="purchaserequestitems.{{ $index }}.item_uom_conversion_id" id="edituom" class="form-control select2">
<option value="">-- Please Select -- </option>
#foreach ($uom_options as $uom_option)
<option value="{{ $uom_option->id }}">{{ $uom_option->unitmeasure->code }}</option>
#endforeach
</select>
#endif
</td>
<td>
#if ($editedPRItemsIndex !== $index)
{{ $purchaserequestitem['comment'] }}
#else
<textarea wire:model.defer="purchaserequestitems.{{ $index }}.comment" class="form-control"></textarea>
#endif
</td>
<td>
<span class='pull-right'>
#if ($editedPRItemsIndex !== $index)
<button
class='btn btn-primary btn-sm rounded-0 py-0'
wire:click.prevent="editPRItem({{ $index }})">
Edit
</button>
<button
wire:click="deleteConfirm({{ $purchaserequestitem['id'] }})"
class='btn btn-danger btn-sm rounded-0 py-0'>
Delete
</button>
#else
<button wire:click.prevent="update({{ $index }})" class="btn btn-primary">Update</button>
#endif
</span>
</td>
</tr>
#endforeach
JS that sets the value of the select
$('.item').on('change', function (e) {
let data = $(this).val();
let idx = $(this).data('index');
#this.set(`purchaserequestitems.${idx}.item_id`, data);
});
Function that updates the data
public function update($prItemIndex)
{
$pritem = $this->purchaserequestitems[$prItemIndex] ?? NULL;
if(!is_null($pritem)){
$editedItem = PurchaseRequestItem::find($pritem['id']);
if($editedItem){
// dd($pritem);
$editedItem->update([
'item_id' => $pritem['item_id'],
'comment' => $pritem['comment'],
'quantity' => $pritem['quantity'],
'item_uom_conversion_id' => $pritem['item_uom_conversion_id']
]);
}
}
$this->editedPRItemsIndex = null;
}
Any help would be appreciated, or if you guys have any other way to implement an inline table editing with a select2 in it. Thank you
Tried to put the js inside an eventlistener but everytime I change the an option on the select box, it will just return to its original value.
Related
Hello how do i check if database row insert exist in laravel? i want to display something entries if > 0 and i want to display else if entries = 0. but i don t know how to do that. i tried with forelse, if else and i got same error. Undefined variable $istProj.
<div class="card-body">
#if ($istoric->isEmpty())
#forelse ($istoric as $istProj)
<div class="mb-3">
<table class='table'>
<tr class="table-row-heads">
<th>Id Proiect</th>
<th>Tip actiune </th>
<th>Colaborator </th>
<th>Suma </th>
<th>Data </th>
</tr>
<tr class="table-row-data">
<td>{{ $istProj->id_proiect }}</td>
<td>{{ $istProj->action_type }}</td>
<td>{{ $istProj->colaborator_id }}</td>
<td>{{ $istProj->suma }}</td>
<td>{{ $istProj->data }}</td>
</tr>
</table>
</div>
#empty
<div class="card-body">
<h1>Nu au fost gasite inregistrari</h1>
</div>
#endforelse
#endif
</div>
<form action="{{ url('/') }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3">
<label class="form-label">Id proiect</label>
<input type="text" class='form-control' value="{{ $proiecte->id }}" name='id_proiect' id='id_proiect' placeholder="Id proiect">
</div>
<div class="mb-3">
<label class="form-label">Tip actiune</label>
<select class="form-select" aria-label="Default select example" name='Status_Tranzactii'>
<option selected>Alege tipul actiunii (0 = cheltuiala, 1 = plata, 2 = incasare)</option>
<option value="cheltuiala">0</option>
<option value="plata">1</option>
<option value="incasare">2</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Colaborator</label>
<select class="form-select" aria-label="Default select example" name="Colab_id">
<option selected>Alege colaboratorul (daca este cazul)</option>
#foreach ($colaborator as $colaboratori)
<option value="{{ $colaboratori->id }}">{{ $colaboratori->id }} </option>
#endforeach
</select>
</div>
<div class="mb-3">
<label class="form-label">Suma</label>
<input type="text" class='form-control' value="{{ $istProj->suma }}" name='suma' placeholder="Introduceti suma">
</div>
<div class="mb-3">
<label class="form-label">Data</label>
<input type="text" class='form-control' value="{{ $istProj->data }}" name='data' placeholder="Introduceti data">
</div>
<button type='submit' class='btn btn-primary' style="float: right;">Adauga</button>
</form>
How can i make that to work?
Error on line with "Suma"
If I understand well, you already have a Collection result (From Eloquent or anywhere) and you need to do different action based on that result.
I'm,right? If yes....
To check the amount or entries you can use $result->count(), so in Blade you can:
#if($result->count() > 0)
Do something
#else
Do anotherthing
#endif
I have a button that adds a new language section to the form for the users to add a new language to their profile and it was working fine until I added that part which gets the existing user languages from DB to show in case they want to change the language or update it. I have been struggling for hours and can't figure out why is this happening why it stops working when the getUserLanguages function is being called. and when remove the getUserLanguages function from the render method it will start working again.
component controller:
public function addLanguage($i)
{
$i = $this->i;
if ($i <= 3)
{
$i = $i + 1;
$this->i = $i;
array_push($this->languages , ['language_name'=>'', 'language_level'=>'']);
}
else
{
$this->sweetAlert('error', 'You only can add 3 langauges.');
}
}
public function getUserLanguages()
{
if (empty(!UserLanguage::where('user_id', auth()->user())))
{
$this->languages = UserLanguage::where('user_id', auth()->user()->id)->get(['language_name', 'language_level'])->toArray();
$this->i = count($this->languages);
}
}
the view:
#foreach ($languages as $index)
<div class="card card-body mb-4">
<div class="row">
<div class="form-group col-md-6">
<label class="" for="languageName">Language</label>
<select class="form-control form-control-alternative" name="language-name" {{-- id="languageName" --}} wire:model="languages.{{ $loop->index }}.language_name">
<option value="" class="form-control" selected disabled>Select Language</option>
#foreach ($language_names as $name)
<option value="{{ $name->abbreviation }}" class="form-control">{{ $name->language }}</option>
#endforeach
</select>
</div>
<div class="form-group col-md-6">
<label class="" for="languageProficiency">Proficiency</label>
<select class="form-control form-control-alternative" name="language-proficiency" {{-- id="languageProficiency" --}} wire:model="languages.{{ $loop->index }}.language_level">
<option value="" class="form-control" selected disabled>Proficinecy Level</option>
#foreach ($language_levels as $level)
<option value="{{ $level->level }}" class="form-control">{{ $level->name }}</option>
#endforeach
</select>
</div>
</div>
</div>
#error('languages.*.level')
<small class="text-warning">{{ $message }}</small>
#enderror
#error('languages.*.language')
<small class="text-warning">{{ $message }}</small>
#enderror
#endforeach
#if (count($languages) < 3)
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-outline-secondary btn-round btn-block" wire:click="addLanguage({{$i}})"><span class="btn-inner--icon"><i class="fa fa-plus fa-2x"></i></span></button>
</div>
</div>
#endif
so finally after i called the getUserLanguages() from the mount method instead of the render method everything works as it should be.
I am trying to remove the refresh time after I click the end button to submit the form into the database.
I am not that experienced when it comes to jquery and I rely only to video tutorials and forums at this current time but I am on my learning curve. as you can see I already converted the start button into jquery to remove the refresh time when I click start button.
<tbody class="table table-bordered table-striped">
#foreach($user as $task)
<tr class="form-group">
{{-- <form action="{{ route('amber.timestone.home.start') }}" method="POST" class="align-center"> --}}
{{csrf_field()}}
<td class="d-flex">
<input type="hidden" name="id" id="hidden-id_{{$task->id}}" value="{{ $task->id }}">
<button type="button" id="str-btn" data-id="{{$task->id}}" class="btn btn-primary"
#if(!empty($task->start))
disabled
#elseif(!empty($task->duration))
readonly
#endif
>Start</button>
{{-- <span class="btn btn-primary" onclick="myAlert({{$task->id}})">Test</span> --}}
</td>
{{-- </form> --}}
<form action="{{ route('amber.timestone.home.end', $task->id) }}" method="POST">
{{csrf_field()}}
<td>
<input class="my-2" type="text" id="ref_{{$task->id}}" name="ref" value="{{ $task->ref }}"
#if(empty($task->start))
readonly
#elseif(!empty($task->duration))
readonly
#endif
>
{{-- <input class="my-2" type="hidden" name="ref" value="{{ $task->ref }}"> --}}
</td>
<td>
<textarea name="remarks" id="remarks_{{$task->id}}" cols="30" rows="4"
#if(empty($task->start))
readonly
#elseif(!empty($task->duration))
readonly
#endif
>{{ $task->remarks }}</textarea>
</td>
<td>
<select id="campaign_{{$task->id}}" class="custom-select custom-select-lg mb-3" name="campaign"
#if(empty($task->start))
disabled
#elseif(!empty($task->duration))
disabled
#endif
>
<option #if($task->campaign == 'cert') selected #endif value="cert">Cert Focus</option>
<option #if($task->campaign == 'omni') selected #endif value="omni">Omni</option>
<option #if($task->campaign == 'textblast') selected #endif value="textblast">TextBlast</option>
</select>
</td>
<td>
<select id="type_{{$task->id}}" class="custom-select custom-select-lg mb-3" name="type"
#if(empty($task->start))
disabled
#elseif(!empty($task->duration))
disabled
#endif
>
<option #if($task->type == 'cert') selected #endif value="cert">Cert</option>
<option #if($task->type == 'email') selected #endif value="email">Email Support</option>
<option #if($task->type == 'chat') selected #endif value="chat">Chat Support</option>
</select>
</td>
<td>
<p id="startTime"></p>
{{ $task->start }}
</td>
<td>
{{ $task->end }}
</td>
<td>
{{ $task->duration }}
</td>
<td class="d-flex">
<input type="hidden" name="id" value="{{ $task->id }}">
<button type="submit" id="end-btn_{{$task->id}}" class="btn btn-danger float-right ml-3"
#if(!empty($task->end))
disabled
#elseif(empty($task->start))
disabled
#endif
>End</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
#section('reporter')
<script>
$(document).ready(function() {
$('#report').DataTable( {
"bLengthChange": false,
"lengthMenu": [ 3, 10 ],
searching: false
});
$('#str-btn').click(function(e){
var id = $(this).data('id');
$.ajax({
url: "{{ url('amber/timestone/start') }}"+'/'+id,
type: "GET",
success: function(data){
console.log(data)
$("#startTime").text(data.start)
$("#end-btn_"+id).removeAttr("disabled");
$("#ref_"+id).removeAttr("readonly");
$("#remarks_"+id).removeAttr("readonly");
$("#campaign_"+id).removeAttr("disabled");
$("#type_"+id).removeAttr("disabled");
$("#str-btn").Attr("disabled");
}
})
$(this).prop("disabled", true);
});
});
You can get your form and add listener on submit, then you can call to event.preventDefault() to avoid refreshing.
Would be something like this:
<form action="{{ route('amber.timestone.home.start') }}" method="POST" class=" form-data-table align-center">
...
Javascript part:
$('.form-data-table').on('submit', function(event){
event.preventDefault();
... remaining code
})
I want to retrieve all the users and associated roles to the one table in laravel. but i couldn't continue because i'm getting error called
This is my controller function
public function index(){
if(Auth::user()->isAdmin==1){
$users = User::with('roles')->get();
return view('users', campact($users));
} else {
abort(403, 'Unauthorized action.');
}
}
This is my User Model's roles function
public function roles()
{
return $this->belongsToMany(Role::class, 'role_users');
}
This is my Role Class's Users function
public function users()
{
return $this->belongsToMany(User::class,'role_users');
}
This is my data table
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
<form method="post">
{{ csrf_field() }}
<div class="form-group">
<select class="form-control" data-id="{{$user->id}}" id="role" name="role">
<option value="0">Select a Role</option>
#foreach ($users->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
</select>
</div>
</form>
</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td><div class="form-group">
<form method="post" id="userActivate">
{{ csrf_field() }}
<label>
<input type="checkbox" class="flat-red isActive" data-id="{{$user->id}}" #if ($user->isActive) checked #endif>
</label>
</form>
</div>
</td>
<td>
<form id="userDelete" method="post" >
{{ csrf_field() }}
<button type="button" id="delete" data-id="{{$user->id}}" class="btn btn-block btn-danger btn-flat">Delete</button>
</form>
</td>
</tr>
#endforeach
Please help me to solve this.
Change your Controller code
public function index(){
$users = User::with('roles') // Eager loading
->get();
return view('users')->with('users', $users);
}
Then, change your blade code from
#foreach ($users->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
to
#if(count($user->roles) > 0)
#foreach ($user->roles as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
#endif
Edit this line:
return view('users', campact($users));
to:
return view('users', compact('users'));
Here I'm trying to loop supplier names using a foreach loop but I'm getting following error
<tbody style="font-size:small">
#foreach($material as $material)
<tr class="odd" role="row">
{!! Form::open(['route'=>'materialsupplier.store','class'=>'form-horizontal p-t-20']) !!}
<td>{{ $material->name }}<input type="hidden" value="{{ $material->id }}" name="mat_id"></td>
<td></td>
<td>
<div class="form-group{{ $errors->has('sup_id') ? ' has-error' : '' }}">
<div >
<select class="form-control js-cities" name="sup_id">
<option value="" selected disabled>Select Supplier</option>
#foreach ($supplier as $supplier)
<option value="{{ $supplier->id}}">{{$supplier->company_name}}</option>
#endforeach
</select>
</div>
</div>
</td>
<td>
{{ Form::submit('Save',['class'=>'far fa-trash-alt btn btn-info btn-sm btn-rounded m-b-1 m-l-5']) }}
</td>
{!! Form::close() !!}
</tr>
#endforeach
</tbody>
after that i chaged my code like this
<select class="form-control js-cities" name="sup_id">
<option value="" selected disabled>Select Supplier</option>
#foreach ($supplier as $supplier)
<option value="{{ $supplier}}">{{$supplier}}</option>
#endforeach
</select>
output
controller
public function index()
{
$material = Material::all();
$supplier = Supplier::all();
return view('materialMgmt.material_suppliyer.create')->withMaterial($material)->withSupplier($supplier);
}
How can I avoid getting this error?
I changed the code as following and its worked
<select class="form-control js-cities" name="sup_id">
<option value="" selected disabled>Select Supplier</option>
#foreach ($supplier as $sup)
<option value="{{ $sup->id}}">{{$sup->company_name}}</option>
#endforeach
</select>
Sir FatemehNB, Thank for your suggestion.