2
I created a table to do insert
, update
and delete
with datatables
. I want to add a new row, in the date column, which automatically fills in with the date next to the previous row. Example. On the last line I have the date 05-07-2019, when adding a line, the date field is soon filled with 06-07-2019. And also when it comes to the end of the month begin with the date of the following month.
Code:
class CellDate{
constructor( start_date ){
this.date = start_date;
}
getNextDate(){
this.date.setDate(this.date.getDate() + 1);
return this.date;
}
}
var DateIndexer = new CellDate(new Date());
$('#add').click(function(){
var html = '<tr>';
html += '<td contenteditable id="data1">'+DateIndexer.getNextDate()+'</td>';
html += '<td contenteditable id="data2"></td>';
html += '<td contenteditable id="data3"></td>';
html += '<td contenteditable id="data4"></td>';
html += '<td contenteditable id="data5"></td>';
html += '<td><button type="button" name="insert" id="insert" class="btn btn-success btn-xs"><span class="glyphicon glyphicon-send"></span></button></td>';
html += '</tr>';
$('#user_data tbody').prepend(html);
});
$(document).on('click', '#insert', function(){
var data = $('#data1').text();
var responsavel = $('#data2').text();
var apoio = $('#data3').text();
var elementos = $('#data4').text();
var elementos1 = $('#data5').text();
if(data != '' && responsavel != '' && apoio != '' && elementos != '' && elementos1 != '')
{
$.ajax({
url:"./insert",
method:"POST",
data:{data:data, responsavel:responsavel, apoio:apoio, elementos:elementos, elementos1:elementos1},
success:function(data)
{
$('#alert_message').html('<div class="alert alert-success">'+data+'</div>');
$('#user_data').DataTable().destroy();
fetch_data();
}
});
setInterval(function(){
$('#alert_message').html('');
}, 5000);
}
else
{
alert("Os campos são de preencimento obrigatório!");
}
});
But I have two problems:
1º the date format, returns like this:
Wed Jul 03 2019 14:56:57 GMT+0100 (Hora de verão da Europa Ocidental) e pretendia que fosse por exemplo: 03-07-2019
second problem:
It does not start at the date that exists in the previous line, it starts at the date of the current day
The table will always start with a row?
– Sam
@Sam The first time to be filled starts without lines, after that always has filled lines
– Bruno
And when there are no lines, what date will it be? Why won’t there be an earlier date to compare.
– Sam
@Sam when there are no lines, will pick up the first day of the next month
– Bruno