有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript如何将数据从动态表存储到数据库

我试图找出如何将数据从动态表存储到数据库

我的想法是:我正在开发一个系统来控制司机的行程。它基本上会存储驾驶员的日常工作,并按不同的时间间隔进行划分,例如:驾驶时间、用餐时间、等待时间等(所有这些时间都以HH:MM表示)。它们将添加到不同的表中,如下图所示

Journey Control

每个表的输入数据(小时)可能不同。这将取决于驾驶员一天的行程。我需要捕捉这些时间并将其存储在数据库中

我最好的办法是在用户结束输入后获取所有这些小时,但是如果我没有字段的名称,我该如何存储这些时间呢

附加信息:我正在使用Java、SpringBoot和Thymeleaf来开发它

JourneyControl.RegisterHourJourney = (function() { var counterDriving = 0; var counterInterval = 0; var counterMeal = 0; var counterWaiting = 0; var counterRest = 0; function RegisterJourneyHour() { this.novaDrivingBtn = $('.js-add-new-direcao'); this.novoIntervalBtn = $('.js-add-new-intervalo'); this.novaMealBtn = $('.js-add-new-refeicao'); this.novaWaitingBtn = $('.js-add-new-espera'); this.novoRestBtn = $('.js-add-new-pernoite'); this.deleteDrivingBtn = $('#direcaot'); this.deleteIntervalBtn = $('#intervalot'); this.deleteMealBtn = $('#refeicaot'); this.deleteWaitingBtn = $('#esperat'); this.deleteRestBtn = $('#pernoitet'); } RegisterHourJourney.prototype.start = function() { this.novaDrivingBtn.on('click', onAdicionarHourDriving.bind()); this.novoIntervalBtn.on('click', onAdicionarHourInterval.bind()); this.novaMealBtn.on('click', onAdicionarHourMeal.bind()); this.novaWaitingBtn.on('click', onAdicionarHourWaiting.bind()); this.novoRestBtn.on('click', onAdicionarHourRest.bind()); this.deleteDrivingBtn.on('click', '.js-delete-btn', onDeleteHourDriving.bind()); this.deleteIntervalBtn.on('click', '.js-delete-btn', onDeleteHourInterval.bind()); this.deleteMealBtn.on('click', '.js-delete-btn', onDeleteHourMeal.bind()); this.deleteWaitingBtn.on('click', '.js-delete-btn', onDeleteHourWaiting.bind()); this.deleteRestBtn.on('click', '.js-delete-btn', onDeleteHourRest.bind()); } function onAdicionarHourDriving() { /*var newRow = $("<tr id=\" " + + counterDriving + " \">");*/ var newRow = $("<tr>"); var cols = ""; var inputInicio = $('#inicioDirecao').val(); var inputFim = $('#fimDirecao').val(); var direcao = $('#direcao').val(); if (!inputInicio || !inputFim) { alert('Os campos início e fim devem ser preenchidos.'); } else if (inputInicio.length != 5 || inputFim.length != 5) { alert('Preencha as horas corretamente (HH:MM).'); } else { cols += '<td>' + inputInicio + '</td>'; cols += '<td>' + inputFim + '</td>'; cols += '<td class="text-center"><a class=" btn btn-link btn-xs js-tooltip js-delete-btn" title="Delete" href="#" ><i class="glyphicon glyphicon-remove"></i></a></td>'; newRow.append(cols); $("#direcaot").append(newRow); counterDriving++; $('#inicioDirecao').val(''); $('#fimDirecao').val(''); } } function onAdicionarHourInterval() { var newRow = $("<tr>"); var cols = ""; var inputInicio = $('#inicioIntervalo').val(); var inputFim = $('#fimIntervalo').val(); if (!inputInicio || !inputFim) { alert('Os campos início e fim devem ser preenchidos.'); } else if (inputInicio.length != 5 || inputFim.length != 5) { alert('Preencha as horas corretamente (HH:MM).'); } else { cols += '<td>' + inputInicio + '</td>'; cols += '<td>' + inputFim + '</td>'; cols += '<td class="text-center"><a class="btn btn-link btn-xs js-tooltip js-delete-btn" title="Delete" href="#" ><i class="glyphicon glyphicon-remove"></i></a></td>'; newRow.append(cols); $("#intervalot").append(newRow); counterInterval++; $('#inicioIntervalo').val(''); $('#fimIntervalo').val(''); } } function onAdicionarHourMeal() { var newRow = $("<tr>"); var cols = ""; var inputInicio = $('#inicioRefeicao').val(); var inputFim = $('#fimRefeicao').val(); if (!inputInicio || !inputFim) { alert('Os campos início e fim devem ser preenchidos.'); } else if (inputInicio.length != 5 || inputFim.length != 5) { alert('Preencha as horas corretamente (HH:MM).'); } else { cols += '<td>' + inputInicio + '</td>'; cols += '<td>' + inputFim + '</td>'; cols += '<td class="text-center"><a class="btn btn-link btn-xs js-tooltip js-delete-btn" title="Delete" href="#" ><i class="glyphicon glyphicon-remove"></i></a></td>'; newRow.append(cols); $("#refeicaot").append(newRow); counterMeal++; $('#inicioRefeicao').val(''); $('#fimRefeicao').val(''); } } function onAdicionarHourWaiting() { var newRow = $("<tr>"); var cols = ""; var inputInicio = $('#inicioEspera').val(); var inputFim = $('#fimEspera').val(); if (!inputInicio || !inputFim) { alert('Os campos início e fim devem ser preenchidos.'); } else if (inputInicio.length != 5 || inputFim.length != 5) { alert('Preencha as horas corretamente (HH:MM).'); } else { cols += '<td>' + inputInicio + '</td>'; cols += '<td>' + inputFim + '</td>'; cols += '<td class="text-center"><a class="btn btn-link btn-xs js-tooltip js-delete-btn" title="Delete" href="#" ><i class="glyphicon glyphicon-remove"></i></a></td>'; newRow.append(cols); $("#esperat").append(newRow); counterWaiting++; $('#inicioEspera').val(''); $('#fimEspera').val(''); } } function onAdicionarHourRest() { var newRow = $("<tr>"); var cols = ""; var inputInicio = $('#inicioPernoite').val(); var inputFim = $('#fimPernoite').val(); if (!inputInicio || !inputFim) { alert('Os campos início e fim devem ser preenchidos.'); } else if (inputInicio.length != 5 || inputFim.length != 5) { alert('Preencha as horas corretamente (HH:MM).'); } else { cols += '<td>' + inputInicio + '</td>'; cols += '<td>' + inputFim + '</td>'; cols += '<td class="text-center"><a class="btn btn-link btn-xs js-tooltip js-delete-btn" title="Delete" href="#" ><i class="glyphicon glyphicon-remove"></i></a></td>'; newRow.append(cols); $("#pernoitet").append(newRow); counterRest++; $('#inicioPernoite').val(''); $('#fimPernoite').val(''); } } function onDeleteHourDriving() { var item = document.activeElement; item.closest("tr").remove(); counterDriving -= 1; } function onDeleteHourInterval() { var item = document.activeElement; item.closest("tr").remove(); counterInterval -= 1; } function onDeleteHourMeal() { var item = document.activeElement; item.closest("tr").remove(); counterMeal -= 1; } function onDeleteHourWaiting() { var item = document.activeElement; item.closest("tr").remove(); counterWaiting -= 1; } function onDeleteHourRest() { var item = document.activeElement; item.closest("tr").remove(); counterRest -= 1; } return RegisterHourJourney; }()); $(function() { var RegisterHourJourney = new JourneyControl.RegisterHourJourney(); RegisterHourJourney.start(); });
<body>
	<section layout:fragment="conteudo">
		<div class="page-header">
			<div class="container-fluid">
				<div class="row">
					<div class="col-xs-10">
						<h1>Journey Control</h1>
						<!-- <h1 th:if="${empresa.nova}">Cadastrar jornada</h1> -->
						<!-- <h1 th:unless="${empresa.nova}" th:text="|Editar empresa - ${empresa.razaoSocial}|">Editar jornada</h1> -->
					</div>

					<div class="col-xs-2">
						<div class="aw-page-header-controls">
							<!-- <a class="btn btn-default" th:href="@{/empresas}"> -->
							<a class="btn btn-default">
								<i class="glyphicon glyphicon-plus-sign"></i>
								<span class="hidden-xs hidden-sm">Search Journey</span>
							</a>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="container-fluid">
			<!-- <form method="POST" th:object="${jornada}" class="form-vertical  js-form-loading"
				th:action="${empresa.nova} ? @{/empresas/nova} : @{/empresas/{codigo}(codigo=${empresa.codigo})}"> -->
			<form class="form-vertical js-form-loading">
				
				<!-- <cj:message/> -->
				
				<!-- <input type="hidden" th:field="*{codigo}" /> -->

				<div class="row">
					<div class="col-sm-2 form-group field-required">
						<label for="data" class="control-label">Date</label>
						<input id="data" type="text" class="form-control js-date" />
					</div>
					
					<div class="col-sm-5 form-group field-required">
						<label for="motorista" class="control-label">Driver</label>
						<input id="motorista" type="text" class="form-control" autofocus="autofocus" />
					</div>

					<div class="col-sm-1 form-group">
						<label class="control-label">Day off</label>
						<div>
							<input type="checkbox" class="js-status" data-size="small" data-off-color="danger"
									data-on-text="Yes" data-off-text="No" />
						</div>
					</div>
				</div>
				
				<div class="row">
					<div class="col-sm-7 form-group">
						<fieldset class="fieldset-border">
							<legend class="legend-border">Driving Time</legend>

							<div class="form-group row">
								<!-- <label for="inicioDirecao" class="col-sm-1 col-form-label">Start</label> -->
								<label for="inicioDirecao" class="col-sm-1 col-form-label">Start</label>
								<div class="col-sm-3">
									<input id="inicioDirecao" type="text" class="form-control js-hora">
									<input type="hidden" value="DIRECAO"/>
								</div>

								<label for="EndDirecao" class="col-sm-1 col-form-label">End</label>
								<div class="col-sm-3">
									<input id="EndDirecao" type="text" class="form-control js-hora">
									<input type="hidden" value="DIRECAO"/>
								</div>
								
								<div class="form-group col-sm-2">
									<button type="button" class="btn  btn-primary js-add-new-direcao">Add</button>
								</div>
							</div>
							<div class="form-group row">
								<div class="table-responsive bw-tabela-simples col-sm-10">
									<table class="table table-hover">
										<thead>
											<tr>
												<th>Start</th>
												<th>End</th>
												<th></th>
											</tr>
										</thead>
										<tbody id="direcaot">
											
										</tbody>
									</table>
								</div>
							</div>
						</fieldset>
					</div>
				</div>
				
				<div class="row">
					<div class="col-sm-7 form-group">
						<fieldset class="fieldset-border">
							<legend class="legend-border">Driving Interval</legend>

							<div class="form-group row">
								<label for="inicioIntervalo" class="col-sm-1 col-form-label">Start</label>
								<div class="col-sm-3">
									<input id="inicioIntervalo" type="text" class="form-control js-hora">
									<input type="hidden" value="INTERVALO"/>
								</div>

								<label for="EndIntervalo" class="col-sm-1 col-form-label">End</label>
								<div class="col-sm-3">
									<input id="EndIntervalo" type="text" class="form-control js-hora">
									<input type="hidden" value="INTERVALO"/>
								</div>
								
								
								<div class="form-group col-sm-2">
									<button type="button" class="btn  btn-primary js-add-new-intervalo">Add</button>
								</div>
							</div>
							<div class="form-group row">
								<div class="table-responsive bw-tabela-simples col-sm-10">
									<table class="table table-hover">
										<thead>
											<tr>
												<th>Start</th>
												<th>End</th>
												<th></th>
											</tr>
										</thead>
										<tbody id="intervalot">
											
										</tbody>
									</table>
								</div>
							</div>
						</fieldset>
					</div>
				</div>
				
				<div class="row">
					<div class="col-sm-7 form-group">
						<fieldset class="fieldset-border">
							<legend class="legend-border">Meal</legend>

							<div class="form-group row">
								<label for="inicioRefeicao" class="col-sm-1 col-form-label">Start</label>
								<div class="col-sm-3">
									<input id="inicioRefeicao" type="text" class="form-control js-hora">
									<input type="hidden" value="REFEICAO"/>
								</div>

								<label for="EndRefeicao" class="col-sm-1 col-form-label">End</label>
								<div class="col-sm-3">
									<input id="EndRefeicao" type="text" class="form-control js-hora">
									<input type="hidden" value="REFEICAO"/>
								</div>
								
								
								<div class="form-group col-sm-2">
									<button type="button" class="btn  btn-primary js-add-new-refeicao">Add</button>
								</div>
							</div>
							<div class="form-group row">
								<div class="table-responsive bw-tabela-simples col-sm-10">
									<table class="table table-hover">
										<thead>
											<tr>
												<th>Start</th>
												<th>End</th>
												<th></th>
											</tr>
										</thead>
										<tbody id="refeicaot">
											
										</tbody>
									</table>
								</div>
							</div>
						</fieldset>
					</div>
				</div>
				
				<div class="row">
					<div class="col-sm-7 form-group">
						<fieldset class="fieldset-border">
							<legend class="legend-border">Waiting Time</legend>

							<div class="form-group row">
								<label for="inicioEspera" class="col-sm-1 col-form-label">Start</label>
								<div class="col-sm-3">
									<input id="inicioEspera" type="text" class="form-control js-hora">
									<input type="hidden" value="ESPERA"/>
								</div>

								<label for="EndEspera" class="col-sm-1 col-form-label">End</label>
								<div class="col-sm-3">
									<input id="EndEspera" type="text" class="form-control js-hora">
									<input type="hidden" value="ESPERA"/>
								</div>
								
								
								<div class="form-group col-sm-2">
									<button type="button" class="btn  btn-primary js-add-new-espera">Add</button>
								</div>
							</div>
							<div class="form-group row">
								<div class="table-responsive bw-tabela-simples col-sm-10">
									<table class="table table-hover">
										<thead>
											<tr>
												<th>Start</th>
												<th>End</th>
												<th></th>
											</tr>
										</thead>
										<tbody id="esperat">
											
										</tbody>
									</table>
								</div>
							</div>
						</fieldset>
					</div>
				</div>
				
				<div class="row">
					<div class="col-sm-7 form-group">
						<fieldset class="fieldset-border">
							<legend class="legend-border">Rest</legend>

							<div class="form-group row">
								<label for="inicioPernoite" class="col-sm-1 col-form-label">Start</label>
								<div class="col-sm-3">
									<input id="inicioPernoite" type="text" class="form-control js-hora">
									<input type="hidden" value="PERNOITE"/>
								</div>

								<label for="EndPernoite" class="col-sm-1 col-form-label">End</label>
								<div class="col-sm-3">
									<input id="EndPernoite" type="text" class="form-control js-hora">
									<input type="hidden" value="PERNOITE"/>
								</div>
								
								
								<div class="form-group col-sm-2">
									<button type="button" class="btn  btn-primary js-add-new-pernoite">Add</button>
								</div>
							</div>
							<div class="form-group row">
								<div class="table-responsive bw-tabela-simples col-sm-10">
									<table class="table table-hover">
										<thead>
											<tr>
												<th>Start</th>
												<th>End</th>
												<th></th>
											</tr>
										</thead>
										<tbody id="pernoitet">
											
										</tbody>
									</table>
								</div>
							</div>
						</fieldset>
					</div>
				</div>

				<div class="form-group">
					<button class="btn  btn-primary" type="submit">Salvar</button>
				</div>
			</form>
		</div>
	</section>
	<th:block layout:fragment="javascript-extra">
		<script th:src="@{/javascripts/jornada.mascara-horas.js}"></script>
		<script th:src="@{/javascripts/vendors/bootstrap-switch.min.js}"></script>
		<script th:src="@{/javascripts/cadastro_jornada.js}"></script>
		<script>
			$('.js-status').bootstrapSwitch();
		</script>
		
	</th:block>
</body>

共 (1) 个答案

  1. # 1 楼答案

    用餐的一个例子:

    当您添加新行时,以name="meal[" + nextMeal + "]"的方式为其命名,此命名法将在表单提交时将您的新膳食映射到控制器中的膳食列表

    要知道nextMeal的正确值是多少,请执行以下操作:

    var nextMeal = 0;           
    while($("tr[name='nextMeal[" + nextMeal + "]']").length){
        nextMeal++;
    }