i have a table that generated from yii2, i want to make a tabular input but before sending submit, there are a client validation to the input field. Consider i dont know the input id, because it is generated by yii2. Here's the code snippet of first row
<tr class="kv-tabform-row" data-key="4">
<td class="kv-align-center kv-align-middle">1</td>
<td class="kv-grid-hide kv-align-top">
<div class="form-group field-kegbulan-4-id">
<input type="hidden" id="kegbulan-4-id" class="form-control" name="KegBulan[4][id]" value="4">
<div class="help-block"></div>
</div>
</td>
<td class="kv-grid-hide kv-align-top">
<div class="form-group field-kegbulan-4-id_keg_ta_uk required">
<input type="hidden" id="kegbulan-4-id_keg_ta_uk" class="form-control" name="KegBulan[4][id_keg_ta_uk]" value="6">
<div class="help-block"></div>
</div>
</td>
<td class="kv-align-middle" style="width:300px;">Pengadaan Barang Kuasi, Buku Uji, Plat Uji dan Stiker Uji</td>
<td class="kv-align-middle">
<input type="text" id="kegbulan-4-anggaran" class="anggaran" name="KegBulan-[4][anggaran]" value="300,000,000" disabled="disabled" style="width:100px;"></td>
<td class="kv-align-top">
<div class="form-group field-kegbulan-4-sp2d required">
<input type="text" id="kegbulan-4-sp2d" class="form-control sp2d" name="KegBulan[4][sp2d]" value="680000" onchange="tesbos()">
<div class="help-block"></div>
</div>
</td>
<td class="kv-align-top" style="width:70px;">
<div class="form-group field-kegbulan-4-persen_sp2d required">
<input type="text" id="kegbulan-4-persen_sp2d" class="form-control" name="KegBulan[4][persen_sp2d]" value="2">
<div class="help-block"></div>
</div>
</td>
<td class="kv-align-top">
<div class="form-group field-kegbulan-4-spj required">
<input type="text" id="kegbulan-4-spj" class="form-control" name="KegBulan[4][spj]" value="680000">
<div class="help-block"></div>
</div>
</td>
<td class="kv-align-top" style="width:70px;">
<div class="form-group field-kegbulan-4-persen_spj required">
<input type="text" id="kegbulan-4-persen_spj" class="form-control" name="KegBulan[4][persen_spj]" value="2">
<div class="help-block"></div>
</div>
</td>
<td class="kv-align-top" style="width:70px;">
<div class="form-group field-kegbulan-4-target required">
<input type="text" id="kegbulan-4-target" class="form-control" name="KegBulan[4][target]" value="0">
<div class="help-block"></div>
</div>
</td>
<td class="kv-align-top" style="width:70px;">
<div class="form-group field-kegbulan-4-pfisik required">
<input type="text" id="kegbulan-4-pfisik" class="form-control" name="KegBulan[4][pfisik]" value="10">
<div class="help-block"></div>
</div>
</td>
<td class="skip-export kv-align-center kv-align-middle" style="width:60px;"><a href="/yii2/yii-application/frontend/web/keg-bulan/view?id=4" title="View" data-pjax="0">
<span class="glyphicon glyphicon-eye-open"></span></a> <a href="/yii2/yii-application/frontend/web/keg-bulan/update?id=4" title="Update" data-pjax="0" style="display:none;">
<span class="glyphicon glyphicon-pencil"></span></a> <a href="/yii2/yii-application/frontend/web/keg-bulan/delete?id=4" title="Delete" data-confirm="Are you sure to delete this item?" data-method="post" data-pjax="0">
<span class="glyphicon glyphicon-trash"></span></a></td>
<td class="skip-export kv-align-center kv-align-middle kv-row-select" style="width:50px;">
<input type="checkbox" name="selection[]" value="4"></td>
</tr>
screenshoot : http://www.imagebam.com/image/569de2398154258
the input sp2d will check input anggaran and do some validation if (sp2d > anggaran) then "sp2d exceed anggaran limit"
Here the initial javascript function to check that function is triggered via onchange
function tesbos(){
var sp2d = $(".sp2d").attr("id");
console.log(sp2d);
}
when i go to row no 2 in sp2d input, still when i check my console log, it still print the sp2d input id of row #1, how to get my input id automatically/dynamically when i go to any row? any help would be appreciated
Assuming you can change the markup, you need to pass the element refernce to the click handler
<input type="text" id="kegbulan-4-sp2d" class="form-control sp2d" name="KegBulan[4][sp2d]" value="680000" onchange="tesbos(this)">
then
function tesbos(el) {
alert(el.id)
}
But I would recommend using jQuery event handlers instead of inlined one, so remove onchange="" from the markup
<input type="text" id="kegbulan-4-sp2d" class="form-control sp2d" name="KegBulan[4][sp2d]" value="680000">
then
jQuery(function($){
$(".sp2d").change(function(){
alert(this.id)
})
})
If I understand correctly, each row of your table includes :
an anggaran (budget) field [fixed value]
a sp2d (spend to date) field [user entered].
And you want to perform a check, on change of every sp2d (spend to date) value against its corresponding anggaran (budget) value.
For this, you do not need to know either of the the fields' IDs. Simply find the anggaran field relative to whichever sp2d field triggers the change event.
First, delete onchange="tesbos()" from the HTML.
Then paste this code between <script></script> tags in your document's HEAD (or in a .js file if your code is organised that way).
jQuery(function($) {
$("#containerID").on('change', ".sp2d", tesbos); // where containerID is the ID of eg. a DIV in which the YII table sits
function tesbos() {
var sp2d_value = $(this).val();
var anggaran_value = $(this).closest("tr").find(".anggaran").val();//find the anggaran field in the same row, and grab its value.
console.log(anggaran_value, sp2d_value);
if(sp2d_value > anggaran_value) {
//anggaran is exceeded
...
} else {
//anggaran is not exceeded
...
}
}
});
Related
I am a beginner at Laravel, I want to insert a more dynamic address field to the database and I want to select one primary address using the radio button. I can able to add more address fields, but, I am unable to select one primary address because it's selecting all radio button values. I have given below using my code
This is My Blade File
<div>
<form action="{{ route('register.post') }}" enctype="multipart/form-data" accept-charset="utf-8" method="POST" >
#csrf
<div class="form-group row">
<label for="address" class="col-md-4 col-form-label text-md-left">Address</label>
<div>
<table id="dynamicAddRemove">
<tr>
<td><button type="button" name="add" id="add-btn" class="btn btn-success">Add More Details</button></td>
</tr>
<tr>
<td>
</td>
<tr>
</table>
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</form>
<div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
var i = 0;
$("#add-btn").click(function(){
//alert("I am an alert box!");
++i;
$("#dynamicAddRemove").append('<tr><td><input type="text" name="moreFields['+i+'][house_no]" placeholder="House No" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][street_name]" placeholder="Street Name" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][area]" placeholder="Area" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][pincode]" placeholder="Pincode" class="form-control" /></td><td style="padding-left:50px;"><input type="radio" class="form-check-input" name="moreFields['+i+'][primary_address]" value="'+i+'"></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>');
});
$(document).on('click', '.remove-tr', function(){
$(this).parents('tr').remove();
});
</script>
This is My Controller File
public function postRegistration(Request $request)
{
$id = DB::getPdo()->lastInsertId();
foreach ($request->moreFields as $key => $value) {
Address::create([
'user_id'=>$id,
'house_no'=>$value['house_no'],
'street_name'=>$value['street_name'],
'area'=>$value['area'],
'pincode'=>$value['pincode'],
'primary_address'=>$value['primary_address'],
]);
}
You dont need to make different name radio buttons use same name for all radio buttons so that user can not select other radio button after selecting one
<input type="radio" class="form-check-input" name="moreFields" value="'+i+'">
Laravel 8 Dynamic Radio Button Edit Page
This worked for me
#foreach($lease_stores as $lease_store)
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="lease_store_id" id="inlineRadio1" #if($general_details->lease_store_id==$lease_store->id) checked value="{{$lease_store->id}}"
#else value="{{$lease_store->id}}"
#endif>
<label class="form-check-label" for="inlineRadio2">{{$lease_store->name}} </label>
</div>
#endforeach
Controller master fetch
$lease_stores=Leasestore::all();
I have the following problem with javascript integrated into thymeleaf template.
I have the following table with different input fields which are filled up with data from a model.
Furthermore i have for each of this input field a hidden additional input field.
Idea is that when I change not hidden input tag, the hidden ones will receive the new value
<tr th:each=" item : ${products}">
<td>
<input class="form-control" type="text" th:value="${item.product.name}" id="productName" name="productName" />
</td>
<td>
<input class="form-control" type="text" th:value="${{item.product.price}}" id="productPrice" name="productPrice" />
</td>
<td>
<input class="form-control" type="text" th:onchange="substituteOnClick()" th:value="${item.quantity}" id="quantityItem" name="quantityItem" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.MinQuantity}" th:onchange="substituteOnClick()" id="minquantityItem" name="minquantityItem" />
</td>
<td>
<form th:action="#{/deleteWarehouseItem/{id_del}/(id_del=${item.id})}" method="post" role="form" class="ui form">
<button type="submit" class="ui form" name = "itemDel" th:value="${item.id}">Löschen</button>
</form>
</td>
<td>
<form class="ui form" method="post" th:action="#{/updateItem}" >
<input type="hidden" name="productName_mvc" id="productName_mvc" value="0" th:value="${item.product.name}"/>
<input type="hidden" name="productPrice_mvc" id="productPrice_mvc" value="0" th:value="${{item.product.price}}"/>
<input type="hidden" name="quantityItem_mvc" id="quantityItem_mvc" value="0" th:value="${item.quantity}"/>
<input name="minquantityItem_mvc" id="minquantityItem_mvc" value="0" />
<input type="hidden" name="inventoryIdentifier_mvc" id="inventoryIdentifier_mvc" th:value="${item.id}"/>
<button type="submit" class="ui labeled icon button" name = "updateItem" th:text="#{management.updateItem}">
</button>
</form>
</td>
</tr>
Here is my javascript file
function substituteOnClick() {
var pN=document.getElementById("productName").value;
var pP=document.getElementById("productPrice").value;
var qI=document.getElementById("quantityItem").value;
var MqI=document.getElementById("minquantityItem").value;
document.getElementById("productName_mvc").value=pN;
document.getElementById("productPrice_mvc").value=pP;
document.getElementById("quantityItem_mvc").value=qI;
document.getElementById("minquantityItem_mvc").value=MqI;
}
As you see my problem is that for each row I need 2 buttons for different actions, since that I decided that It would be useful just to change values of hidden inputs. It is possible because I use hidden inputs only if I need to update an object and it happens mainly on change event. But my problem is that even if I write anything new in not hidden inputs, it does not influence my hidden ones. I have cheked it by not hidding one of them.
How is it possible to change values in other inputs by changing them firstly in other ones.
Best wishes
first of all, you need to remove these extra brackets from this line
<input class="form-control" type="text" th:value="${{item.product.price}}" id="productPrice" name="productPrice" />
and then in your javascript use console.log() to check whether on onchange event its fetching the change value or not like
console.log("In js function");
console.log(qI);
if you are not getting these values then Instead of onchange event add a button for each input or for all input use a single button inside form and then use the onClick function or onsubmit like this:
<form onsubmit="return substituteOnClick()">
<td>
<input class="form-control" type="text" th:value="${item.product.name}" id="productName" name="productName" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.product.price}" id="productPrice" name="productPrice" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.quantity}" id="quantityItem" name="quantityItem" />
</td>
<td>
<input class="form-control" type="text" th:value="${item.MinQuantity}" id="minquantityItem" name="minquantityItem" />
</td>
<input type="submit" name="Submit" value="Submit"/>
</form>
Let me know if this helps
I update input with id='total' with event onchange. When I fill quantity input, the input total is updated with correct value but when i try to get the value from controller, it returns null. Any idea? Thanks
This is the code:
<div class="form-group" >
<input type="text" name="quantity" class="form-control" id="quantity" onchange="updateInput(this.value)" >
</div>
</td>
<td align="center">
<div class="form-group ">
<input type="text" class="form-control" id="total" name="total" value="" disabled >
</div>
</td>
And this is function onchange
function updateInput(x)
{
var price=parseFloat(document.getElementById('price').value);
document.getElementById('total').value=parseFloat(x)*price;
}
Solved it, just removed disabled attribute and it works.
I have a dynamic input form like this (open the link): this is the
form
The option value is "Debit" and "Kredit".
In my case, I wanna get the Jumlah's value based on the option selected. As you can see in that picture, in first row, I input the Jumlah's value with 10 and then I select the "Debit", so the Total Debit value will change to 10 and Total Kredit value still 0.
And then, in the next row, I input the Jumlah's value with 3 and then I select "Kredit", so the Total Debit value still 10 and Total Kredit change to 3.
Then, when I change the second row to Debit, the Total Debit should be 13 and Total Kredit is 0. But, it makes Total Debit to 13 and Total Kredit still 3.
So, how can I solve this? Any suggestions or another alternatives?
Here is my form's code:
<form action="" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="nojurnal">Nomor Jurnal:</label>
<input type="text" class="form-control" id="nojurnal" value="{{ $noJurnal+1 }}" name="no_jurnal" readonly>
</div>
<div class="form-group">
<label for="tgljurnal">Tanggal Jurnal:</label>
<input type="date" class="form-control" id="tgljurnal" name="tgl_jurnal">
</div>
<div class="form-group">
<label for="keterangan">Keterangan:</label>
<textarea name="keterangan_jurnal" id="keterangan" class="form-control" rows="8" cols="80"></textarea>
</div>
<hr>
<div class="form-group table-responsive">
<table class="table table-striped">
<thead>
<th>Nomor Rekening</th>
<th>Nama Rekening</th>
<th>Keterangan</th>
<th>Jumlah</th>
<th>Jenis Rekening</th>
<th></th>
</thead>
<tbody class="tabel-body">
<tr class="row-rekening">
<td>
<div class="form-group">
<select class="form-control kodeRekening" name="kode_rekening[]">
#foreach($rekenings as $rekening)
<option class="listKodeRekening" value="{{ $rekening->kode_rekening }}" nm="{{ $rekening->nama_rekening }}">{{ $rekening->kode_rekening }}</option>
#endforeach
</select>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control namaRekening" type="text" name="" value="" readonly>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control keterangan" type="text" name="keterangan[]" value="">
</div>
</td>
<td>
<div class="form-group">
<input class="form-control jumlahDK" type="text" name="jumlah[]" value="">
</div>
</td>
<td>
<div class="form-group">
<select class="form-control jenisRekening" name="d_k[]">
<option value="">-- Pilih --</option>
<option value="D" class="debit">Debit</option>
<option value="K" class="kredit">Kredit</option>
</select>
</div>
</td>
<td>
<div class="form-group">
<button type="button" class="form-control glyphicon glyphicon-plus btn-success more-input" name="button"></button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="form-group col-sm-12">
<label for="total-debit" class="col-sm-2">Total Debit</label>
<span class="col-sm-4"><input type="text" id="jumlah-debit" class="form-control" name="" value="" readonly></span>
</div>
<div class="form-group col-sm-12">
<label for="total-kredit" class="col-sm-2">Total Kredit</label>
<span class="col-sm-4"><input type="text" id="jumlah-kredit" class="form-control" name="" value="" readonly></span>
</div>
<input type="submit" class="btn btn-success" value="Simpan">
</form>
And this is my javascript code:
$(document).on('change', '.jenisRekening', function(e){
if ($(this).val()=='D' && $(this).parent().parent().prev().find('.jumlahDK').val() !==''){
totalDebit += parseInt($(this).parent().parent().prev().find('.jumlahDK').val())
}
else if($(this).val()=='K' && $(this).parent().parent().prev().find('.jumlahDK').val() !==''){
totalKredit += parseInt($(this).parent().parent().prev().find('.jumlahDK').val())
}
$('#jumlah-debit').val(totalDebit)
$('#jumlah-kredit').val(totalKredit)
});
The variables totalDebit and totalCredit should be reinitialised to zero in your jQuery code before they're recomputed. It seems that their old value is retained and therefore a simple solution would be:
$(document).on('change', '.jenisRekening', function(e){
totalDebit = 0;
totalKredit = 0;
if ($(this).val()=='D' && $(this).parent().parent().prev().find('.jumlahDK').val() !==''){
totalDebit += parseInt($(this).parent().parent().prev().find('.jumlahDK').val());
}
else if($(this).val()=='K' && $(this).parent().parent().prev().find('.jumlahDK').val() !==''){
totalKredit += parseInt($(this).parent().parent().prev().find('.jumlahDK').val());
}
$('#jumlah-debit').val(totalDebit);
$('#jumlah-kredit').val(totalKredit);
});
I need to update datetimepicker[i] script inside ng-repeat but I do not know how to do that.
<div class='input-group date' id='datetimepicker{{$index+1}}'>
work successfully and made list of
<div class='input-group date' id='datetimepicker1'>
<div class='input-group date' id='datetimepicker2'>
but problem is script not update from model data
$('#datetimepicker{{$index+1}}').datetimepicker();
result is
$('#datetimepicker{{$index+1}}').datetimepicker();
I want to '#datetimepicker{{$index+1}}' change to'#datetimepicker1'
<tr data-ng-repeat="row in game.result">
<td>{{$index+1}}</td>
<td ng-bind="row.host"></td>
#*<td>
<input class="form-control text-center" data-ng-model="row.dateStart" />
</td>*#
<td ng-controller="DatepickerDemoCtrl">
<div class="input-group w-360">
<div class='input-group date' id='datetimepicker{{$index+1}}'>
<input data-format="yyyy/MM/dd hh:mm:ss" ng-model="row.dateStart" type='text' class="form-control" ng-required="true" />
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker{{$index+1}}').datetimepicker();
});
</script>
</div>
</td>
<td>
<select ng-model="row.gameState"
ng-options="c.Value as c.Text for c in game.states" class="form-control" required></select>
</td>
<td>
<input class="form-control text-center" data-ng-model="row.hostGoals" type="number" required />
</td>
<td>
<input class="form-control text-center" data-ng-model="row.guestGoals" type="number" required />
</td>
<td ng-bind="row.guest"></td>
</tr>
Update
I move my code inside controller and make html in controller
Controller - MVC
onlineResult.dateHtml = #"<script type=""text/javascript"">$(function () {$('#datetimepicker"+(i + 1).ToString()+ "').datetimepicker({format: 'YYYY/MM/DD HH:mm:ss'});});</script>";
and update view to
<p compile data-ng-bind-html="to_trusted(row.dateHtml)"></p>
My new problem is model bind in Get but not changed in Post.
When i submit form my Get data sent to controller not changed data
I used angularjs directive datetimepicker
https://material.angularjs.org/latest/demo/datepicker