Write Hmtl Table data in the Bank | Laravel 5.1

1

I'm having a question regarding retrieving information from an html table and saving it to the database using laravel.

This part of the application consists of storing information related to the tests that this user is supposed to perform in a user_exame table.

In the table I store user_id, examen_id, dt_utlimo_exame, dt_proximo_exame

I'm populating the html table with the following code:

eventsForm: function(){	

    jQuery('#btn-adicionar-exame').click(function(){

	moment.locale('pt-br');

	var id = jQuery('#exame').find('option:selected').val();
	var exame = jQuery('#exame').find('option:selected').text();
	var periodical = jQuery('#periodical').find('option:selected').val();
	var periodicalText = jQuery('#periodical').find('option:selected').text();
	var ultimoExame = jQuery('#dtUltimoExame').val();
	var newRow = $("<tr>"); 
	var cols   = "";
	var proximoExame = '';
			
	//Define a proxima data do exame
	proximoExame = moment(ultimoExame, "DD/MM/YYYY").add(periodical, 'months');

	cols += '<td>' + id + '</td>';
	cols += '<td>' + exame + '</td>';
	cols += '<td>' + periodicalText + '</td>';
	cols += '<td>' + ultimoExame + '</td>';
	cols += '<td>' + moment(proximoExame, "DD/MM/YYYY").calendar() + '</td>';
			
	cols += '<td>';
	cols += '<button type="button" class="btn btn-link " id="btn-delet" onclick="employee.removeTableRow(this)"><i class="fa fa-trash fa-1x color-red"></i></button>';
	cols += '</td>';

	newRow.append(cols);
			
	/Adicionando a row a tabela
	jQuery("#table-list-exames").append(newRow);

        //Limpando os campos
	jQuery('#exame').val("").change();
	jQuery('#periodical').val("").change();
	jQuery('#dtUltimoExame').val('');

});
},

My question is: how to retrieve each row of the table and store it in the database using Laravel?

I know that in javascript I could do something like:

tableExames.find('tr').each(function (i) {...} 

But how to do this in the Laravel controller?

    
asked by anonymous 14.03.2017 / 21:35

1 answer

1

You will have to do an ajax for each line included in this table there. To do this, use a selector for the rows of the table: $('#table-list-exames tr') and then just use each of jQuery to get the information:

$('#table-list-exames tr').each(function(i, linha){
   // pega as colunas de cada linha
   var colunas = $(linha).find('td'); 
   // transforma em um objeto
  var registro = {};
  $(colunas).each(function(j, col){
    registro[j] = $(col).text();
  });
  console.log(registro);
  // manda por ajax pro servidor
  //cadastrar_exame(registro);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table><tbodyid="table-list-exames">
    <tr>
      <td>1</td>
      <td>2</td>
      <td>3</td>
    </tr>
    <tr>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
  </tbody>
</table>

Of course, treat the data before sending it to Ajax. Needing help with Ajax, this jQuery documentation is pretty cool: link

    
14.03.2017 / 22:12