I'm trying to append multiple data (from date form) into table, using Jquery and MomentJS. But the result not showing what I want.
Here's my code :
$(".btnNext").on('click', function(e) {
var Day = 1;
var newDay = Day++;
var fromDate = $('#campaign_start_period_id').val(),
toDate = $('#campaign_end_period_id').val(),
from, to, druation;
from = moment(fromDate, 'YYYY-MM-DD');
to = moment(toDate, 'YYYY-MM-DD');
/* using diff */
duration = to.diff(from, 'days')
let date = [];
for (var m = moment(fromDate); m.isBefore(toDate); m.add(1, 'days')) {
date.push(m.format('MMM. DD, YYYY'));
}
var tr = '<tr class="v-middle">';
tr += '<td>' + newDay + '</td>';
tr += '<td class="text-color">' + date + '</td>';
tr += "</tr>";
$('#result tbody').append(tr);
});
<script src="https://unpkg.com/moment#2.10.6/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<input class="form-control" type="date" id="campaign_start_period_id" maxlength="None" name="campaign_start_period" placeholder="dd M yyyy" type="text" value="{{ campaign_start_period }}" />
<input class="form-control" type="date" id="campaign_start_period_id" maxlength="None" name="campaign_end_period" placeholder="dd M yyyy" type="text" value="{{ campaign_end_period }}" />
<button class="btnNext" value="" type="button">Next</button>
<table class="table toggle-circle table-hover table-striped" id="result">
<thead>
<tr>
<th>Days</th>
<th>Date</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
My code append "date" inside one "<tr><td></td></tr>" and not adding other "<tr></tr>".
What I expect is to show data like this :
You should iterate on the date array and add each date to a 'tr' tag
$(".btnNext").on('click', function(e) {
var Day = 1;
var newDay = Day++;
var fromDate = $('#campaign_start_period_id').val(),
toDate = $('#campaign_end_period_id').val(),
from, to, druation;
from = moment(fromDate, 'YYYY-MM-DD');
to = moment(toDate, 'YYYY-MM-DD');
/* using diff */
duration = to.diff(from, 'days')
let date = [];
for (var m = moment(fromDate); m.isBefore(toDate); m.add(1, 'days')) {
date.push(m.format('MMM. DD, YYYY'));
}
var tr = '';
date.forEach((newDay,index) => {
tr += '<tr class="v-middle">';
tr += '<td>' + (index + 1) + '</td>';
tr += '<td class="text-color">' + newDay + '</td>';
tr += "</tr>";
});
$('#result tbody').append(tr);
});
<script src="https://unpkg.com/moment#2.10.6/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<input class="form-control" type="date" id="campaign_start_period_id" maxlength="None" name="campaign_start_period" placeholder="dd M yyyy" type="text" value="{{ campaign_start_period }}" />
<input class="form-control" type="date" id="campaign_start_period_id" maxlength="None" name="campaign_end_period" placeholder="dd M yyyy" type="text" value="{{ campaign_end_period }}" />
<button class="btnNext" value="" type="button">Next</button>
<table class="table toggle-circle table-hover table-striped" id="result">
<thead>
<tr>
<th>Days</th>
<th>Date</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
Related
I'm tyring to display a "result table"(showing the area value and calculated time) only after a user inputs both width and height value and presse the button.
What I attempted was display input values on a sheet so the inputs are correctly passed to the function setParam().
My problems are
How to display a table only after a button is pressed.
How to set a calculated are value returned from a function to the web application.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div id="area">
<table>
<thead>
<tr>
<th>Area</th>
</tr>
</thead>
<tbody>
<tr>
<td>width</td>
<td><input id="width" type="number" ></input></td>
</tr>
<tr>
<td>height</td>
<td><input id="height" type="number"></input></td>
</tr>
</tbody>
</table>
</div>
<div>
<input type="button" onclick="calculateArea()" id="" value="Calcuate This!">
</div>
<div id="createdTable"></div>
<script>
function calculateArea() {
const width = document.getElementById('width').value;
const height = document.getElementById('height').value;
google.script.run.setParam(width, height);
var htmlTable = '<table id="xxx" border=1>' +
'<thead>' +
'<tr> +
'<th>Result</th> +
'</tr>' +
'</thead>' +
'<tbody>' +
'<tr>' +
'<td>Area</td>' +
'<td><input value=""><?=getArea();?></input></td>' +
'</tr>' +
'<tr>' +
'<td>Calculation Time</td>' +
'<td><input value=""></input></td>' +
'</tr>' +
'</tbody>' +
'</table>';
document.getElementById("createdTable").innerHTML = htmlTable;
}
</script>
</body>
</html>
let width = 0;
let height = 0;
let area = 0;
let time = 0;
function doGet() {
return HtmlService.createTemplateFromFile("index").evaluate();
}
function setParam(width, height) {
this.width = width;
this.height = height;
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/xxxx/edit#gid=0");
var ws = ss.getSheetByName("sheet1");
ws.getRange(1,1).setValue(width);
ws.getRange(2,1).setValue(height);
}
function getArea() {
area = width * height;
return area;
}
function getTime() {
return time;
}
I'm stuck in the jQuery code, I want to make dynamic fields, but the problem is when I want to print the following equation in the total value, it only appears in the first value of the dynamic values, what is the solution to this problem
<div class="container">
<table class="table table-responsive" id="tal">
<thead>
<tr>
<td>price</td>
<td>quantity</td>
<td>total $</td>
<td><button type="button" class="btn-outline-success" id="add-new-row">addRow</button></td>
</tr>
</thead>
<tbody id="addNewTR">
<tr>
</tr>
</tbody>
</table>
</div>
jquery code
$(document).ready(function(){
var x = 1;
var min = 1;
var max = 4;
var html = '<tr><td><input type="text" name="price[]" id="price" ></td><td><input type="text" name="qty[]" id="qty" ></td><td><input type="text" name="total[]" id="total" disabled=""></td><td><button type="button" class="btn-outline-danger" id="remove">remove</button></td></tr>';
$("#add-new-row").click(function(i){
i.preventDefault();
if (x <= max) {
var price = $("#price").val();
var qty = $("#qty").val();
var sum = parseInt($("#price").val()) * parseInt($("#qty").val()) ;
$("#total").val(sum);
$("#addNewTR").append(html);
x++;
}
});
$("#addNewTR").on('click','#remove',function(){
$(this).closest("tr").remove();
x--;
});
});
here is the updated working code. you must need a unique id for each append element.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var x = 1;
var min = 1;
var max = 4;
$("#add-new-row").click(function (i) {
i.preventDefault();
if (x <= max) {
var i = x - 1;
var price = $("#price" + i).val();
var qty = $("#qty" + i).val();
var sum = parseInt($("#price" + i).val()) * parseInt($("#qty" + i).val());
$("#total" + i).val(sum);
var html = "";
html = '<tr><td><input type="text" name="price[]" id="price' + x + '" ></td><td><input type="text" name="qty[]" id="qty' + x + '" ></td><td><input type="text" name="total[]" id="total' + x + '" disabled=""></td><td><button type="button" class="btn-outline-danger" id="remove">remove</button></td></tr>';
$("#addNewTR").append(html);
x++;
}
});
$("#addNewTR").on('click', '#remove', function () {
$(this).closest("tr").remove();
x--;
});
});
</script>
<body>
<div class="container">
<table class="table table-responsive" id="tal">
<thead>
<tr>
<td>price</td>
<td>quantity</td>
<td>total $</td>
<td><button type="button" class="btn-outline-success" id="add-new-row">addRow</button></td>
</tr>
</thead>
<tbody id="addNewTR">
<tr>
</tr>
</tbody>
</table>
</div>
<body/>
function fn_onblulr(e){
var price = $(e.target).closest('tr').find('.quantity').val();
let quantity = $(e.target).closest('tr').find('.quantity').val();
let total = $(e.target).closest('tr').find('.total');
$(total).val(price*quantity);
}
$(document).ready(function(){
var x = 1;
var min = 1;
var max = 4;
var html = '<tr><td><input type="text" class="price" name="price[]" id="price" ></td><td><input type="text" class="quantity" name="qty[]" onblur="fn_onblulr(event)" id="qty" ></td><td><input type="text" class="total" name="total[]" id="total" disabled=""></td><td><button type="button" class="btn-outline-danger" id="remove">remove</button></td></tr>';
$("#add-new-row").click(function(i){
i.preventDefault();
if (x <= max) {
var price = $("#price").val();
var qty = $("#qty").val();
var sum = parseInt($("#price").val()) * parseInt($("#qty").val()) ;
$("#total").val(sum);
$("#addNewTR").append(html);
x++;
}
});
$("#addNewTR").on('click','#remove',function(){
$(this).closest("tr").remove();
x--;
});
});
Change Script
I have create a function that adds rows based on value of a dropdown list.
The user selects a project, clicks the Add button, and a row with the name of the project and a set of cells are added to the table.
The name of the project is inside a 'th' tag.
I want to add a condition that the add button does not add a row if the project has already been added:
<script>
$(document).ready(function() {
var i = 1
var tbody = $('#myTable').children('tbody');
//Then if no tbody just select your table
var table = tbody.length ? tbody : $('#myTable');
/* table.find('th').each(function(){
if (th.value = projval)
alert("project already in");
});*/
$('#addrow').click(function() {
//Add row
var projval = $('#projSel').val();
$('#myTable > tbody > tr > th').each(function(index, th) {
console.log(index);
// if($("th").eq(index).attr('id') = projval) {
// alert("Exist");
// }
});
table.append('<tr><th scope="row" id="' + projval + '">' + projval + '</th>\
<td><input class="form-control" type="text" id="inMon' + i + '" placeholder="00:00"></td>\
<td><input class="form-control" type="text" id="inTue' + i + '" placeholder="00:00"></td>\
<td><input class="form-control" type="text" id="inWed' + i + '" placeholder="00:00"></td>\
<td><input class="form-control" type="text" id="inThu' + i + '" placeholder="00:00"></td>\
<td><input class="form-control" type="text" id="inFri' + i + '" placeholder="00:00"></td>\
<td><input class="form-control" type="text" id="inSat' + i + '" placeholder="00:00"></td>\
<td><input class="form-control" type="text" id="inSun' + i + '" placeholder="00:00"></td>\
<td><button name="remove" id="' + i + '" class="btn btn-danger btn_remove btn-sm" onclick="deleteRow(this)">X</td></tr>');
i++;
})
});
projVal variable is the value selected from the dropdown list
myTable is the ID of the table.
I cannot figure how to write the if condition so that the index retrieves the value of 'th' ID and compares it to the already exisiting 'th' added by user.
Check with the below code:
$('#addrow').click(function () {
var projval = $('#projSel').val();
var projectAdded = $('tr>th[scope=row][id="' + projval + '"]', table).length > 0;
if (projectAdded) {
alert('project already in');
return false;
}
// ... Add Row Code
i++;
})
Need to prevent the user from entering more than the existing quantity. It should check the total quantities entered on change event as well as on submit click.
For example, the first row(Part 1) has 50 quantities when the user is adding rows and the total quantities entered against "Part 1" should not exceed 50 and a text should display above the row. Any help would be appreciated.
HTML:
<form name="req" method="post">
<table id="exampleTbl">
<tr>
<td>Part 1</td>
<td><input type="number" name="quantity" value="50"></td>
<td>
<button type="button" class="addRow">+</button>
</td>
</tr>
<tr>
<td>Part 2</td>
<td><input type="number" name="quantity" value="100"></td>
<td>
<button type="button" class="addRow">+</button>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit">
</form>
Jquery:
$(function() {
$('#exampleTbl').delegate('button.addRow', 'click', function() {
var row = $(this).closest('tr'); // get the parent row of the clicked button
var html = $(this).closest('tr').clone();
$(html).insertAfter(row); // insert content
let btn = $('Remove');
btn.click(function () {
$(html).remove(); // Remove row on click
})
$(html).find('.addRow').replaceWith(btn);
});
});
In below code snippet i have given class to your trs to differentiate them with original quantity and the quantity which user can change . Also , whenever user will change input then we need find sum of all quantity of Part 1 or Part 2 .So , i have use tr class which will have either Part 1 or Part 2 to iterate over trs input .
Demo Code :
$(function() {
$('#exampleTbl').delegate('button.addRow', 'click', function() {
var row = $(this).closest('tr'); // get the parent row of the clicked button
var html = $(this).closest('tr').clone();
$(html).insertAfter(row); // insert content
let btn = $('Remove');
btn.click(function() {
$(html).remove(); // Remove row on click
})
$(html).find('.addRow').replaceWith(btn);
});
});
$(document).on("change", "input", function() {
var sum = 0;
$(".total_qty_entered").remove(); //remove div
var name = $(this).closest('tr').attr('class'); //get class
//get original quantity
var qty = $("." + name + "_original").find('td:eq(2)').text();
//loop through tr with same class > inputs
$("." + name + " input[name=quantity]").each(function() {
sum += +$(this).val();
});
console.log("Quantity : " + qty + " | Sum : " + sum)
if (sum > qty) {
$(this).before('<div class="alert alert-danger total_qty_entered" role="alert">Total Quantity should not exceed ' + qty + '</div>');
}
});
//call when on submit
function mySubmitFunction(e) {
//e.preventDefault();
var result = true;
$("tr[data-value ='original']").each(function() {
var name = $(this).find('td:eq(0)').text();
var qty = $(this).find('td:eq(2)').text();
var new_name = name.replace(/\s/g, '');
var sum = 0;
$("." + new_name + " input[name=quantity]").each(function() {
sum += +$(this).val();
});
console.log("Name : " +name+ " Sum : "+sum + " || Qty : " + qty)
if (sum > qty) {
//do something
alert("please check quanity is greater for "+name)
result = false;
}
});
return result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="req" method="post" onsubmit="return mySubmitFunction(event)">
<table id="exampleTbl">
<!--added this class-->
<tr data-value="original" class="Part1_original">
<td>Part 1 </td>
<td>Original Quantity : </td>
<td>50</td>
</tr>
<!--added this class-->
<tr class="Part1">
<td>Part 1</td>
<td><input type="number" name="quantity" value="50"></td>
<td>
<button type="button" class="addRow">+</button>
</td>
</tr>
<tr data-value="original" class="Part2_original">
<td>Part 2</td>
<td>Original Quantity : </td>
<td>100</td>
</tr>
<tr class="Part2">
<td>Part 2</td>
<td><input type="number" name="quantity" value="100"></td>
<td>
<button type="button" class="addRow">+</button>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit">
</form>
Update 1 :
You can give class to your input so that it can use in each to iterate over the inputs having same class and check if against the total quantity .
Demo code :
$(function() {
$('#exampleTbl').delegate('button.addRow', 'click', function() {
var row = $(this).closest('tr'); // get the parent row of the clicked button
var html = $(this).closest('tr').clone();
$(html).insertAfter(row); // insert content
let btn = $('Remove');
btn.click(function() {
$(html).remove(); // Remove row on click
})
$(html).find('.addRow').replaceWith(btn);
});
});
$(document).on("change", "input", function() {
var sum = 0;
//get total quantity
var qty = $(this).closest('td').next('td').text(); //get class
var name = $(this).attr('class'); //get class
//loop through tr with same class > inputs
$("tr > td input[class=" + name + "]").each(function() {
sum += +$(this).val();
});
$(".total_qty_entered").remove(); //remove div
if (sum > qty) {
$(this).before('<div class="alert alert-danger total_qty_entered" role="alert">Total Quantity should not exceed ' + qty + '</div>');
}
});
//call when on submit
function mySubmitFunction(e) {
//e.preventDefault();
var result = true;
//loop through td input
$("tr > td input").each(function() {
var name = $(this).attr('class');
var qty = $(this).closest('td').next('td').text(); //get class
var sum = 0;
//iterate through all inputs with same class
$("." + name).each(function() {
sum += +$(this).val();
});
if (sum > qty) {
//do something
$(this).before('<div class="alert alert-danger total_qty_entered" role="alert">Total Quantity should not exceed ' + qty + '</div>');
result = false;
}
});
return result;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="req" method="post" onsubmit="return mySubmitFunction(event)">
<table id="exampleTbl">
<tr>
<td>Part 1</td>
<td><input type="number" name="quantity" value="50" class="Part1"></td>
<td>50</td>
<td>
<button type="button" class="addRow">+</button>
</td>
</tr>
<tr>
<td>Part 2</td>
<td><input type="number" name="quantity" value="100" class="Part2"></td>
<td>100</td>
<td>
<button type="button" class="addRow">+</button>
</td>
</tr>
</table>
<input type="submit" name="Submit" value="Submit">
</form>
help please
I just learned codeigniter, I have a problem when inserting data.
I want to input or insert data that is looking for from the database, for example can be seen in the picture.
when I insert it into the database, the error message is the data that I inserted NULL
Search by kode
{
"result":
[
{
"id_runsheet":"6",
"kd_runsheet":"RUNSHEET-0000-0003",
"reciver_name":"arif",
"tracking_number":"RJC-0000-0003",
"status":"Consignee Unknown",
"id_runsheet_detail":"6",
"id_outdes":"3"
},
{
"id_runsheet":"6",
"kd_runsheet":"RUNSHEET-0000-0003",
"reciver_name":"hendra",
"tracking_number":"RJC-0000-0004",
"status":"Closed",
"id_runsheet_detail":"6",
"id_outdes":"3"
}
]
}
Controller
public function cari(){
$id=$_POST['kd_runsheet'];
$data['result']=$this->M_inbound_undel->caridb($id);
echo json_encode($data);
}
Model
public function save(){
$data = array(
'id_runsheet' => $this->input->post('id_runsheet'),
'id_runsheet_detail' => $this->input->post('id_runsheet_detail'),
'id_outdes' => $this->input->post('id_outdes')
);
$this->M_inbound_undel->db_save($data);
echo $this->session->set_flashdata('message','success');
redirect('backend/inbound_undel');
}
View
<form class="form-signin" method="post" action="<?php echo base_url().'backend/inbound_undel/save';?>">
<div class="box">
<div class="box-header">
<div class="row">
<div class="form-group col-md-2">
<label for="field-1" class="control-label">Tanggal</label>
<input name="" value="<?php echo(date('Y-m-d H:i:s')) ?>" class="form-control" type="text" placeholder="" readonly required="">
</div>
<div class="form-group col-md-4">
<label for="field-1" class="control-label">KODE RUNSHEET</label>
<input name="kd_runsheet" class="form-control" id="kode" type="text" placeholder="Masukan Kode Runsheet" style="" required="">
<span style="font-size:11px; color:#00a65a;">No resi hanya bisa tampil apabila resi sudah di Inbound</span>
</div>
</div>
</div>
</div>
<div class="box">
<div class="box-body">
<table id="databel" class="table table-bordered table-striped" style="width: 100%; cellspacing: 0;">
<thead>
<tr>
<th>Kode Runsheet</th>
<th>Kode Resi</th>
<th>Penerima</th>
<th>status terakhir</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
<div class="box-footer">
<button type="submit" class="btn bg-orange btn-flat margin">Simpan</button>
</div>
</div>
</div>
</form>
AJAX
$(document).ready(function(){
$('#kode').on('input',function(){
var kode=$(this).val(); //serach by kode
$.ajax({
url : "<?php echo base_url('backend/inbound_undel/cari')?>",
type: 'POST',
dataType: 'JSON',
data : {kd_runsheet: kode},
success:function (data) {
//result
var result = data.result;''
var row = "";
for(i=0; i < result.length; i++) {
row += "<tr>";
row += "<td>"+result[i].kd_runsheet+"</td>";
row += "<td>"+result[i].tracking_number+"</td>";
row += "<td>"+result[i].reciver_name+"</td>";
row += "<td class='label bg-red'>"+result[i].status+"</td>";
row += "</tr>";
}
$('#tbody').html(row);
}
});
event.preventDefault();
});
});
when I insert it into the database, the error message is the data that I insert all NULL
It happens because your save() model expected 3 post parameters to be received, yet none of them are present.
After looking at the returned json data that you've provided, you could make a hidden input for each of the required save() parameters :
<script>
$(document).ready(function() {
$('#kode').on('input', function() {
var kode = $(this).val(); //serach by kode
$.ajax({
url: "<?php echo base_url('backend/inbound_undel/cari') ?>",
type: 'POST',
dataType: 'JSON',
data: {
kd_runsheet: kode
},
success: function(data) {
//result
var result = data.result;
var row = "";
for (i = 0; i < result.length; i++) {
row += "<tr>";
row += "<td>" + result[i].kd_runsheet + "</td>";
row += "<td>" + result[i].tracking_number + "</td>";
row += "<td>" + result[i].reciver_name + "</td>";
row += "<td class='label bg-red'>" + result[i].status + "</td>";
row += "</tr>";
}
$('#tbody').html(row);
// begin appended hidden inputs
let input = "";
for (i = 0; i < result.length; i++) {
input += "<input type='hidden' name='id_runsheet' value='" + result[i].id_runsheet + "' />";
input += "<input type='hidden' name='id_runsheet_detail' value='" + result[i].id_runsheet_detail + "' />";
input += "<input type='hidden' name='id_outdes' value='" + result[i].id_outdes + "' />";
}
$('#databel').html(input);
// end appended hidden inputs
}
});
event.preventDefault();
});
});
</script>