gpt4 book ai didi

javascript - 动态生成的表显示错误

转载 作者:行者123 更新时间:2023-11-28 08:10:11 25 4
gpt4 key购买 nike

在我当前的 spring 项目中,我有这样的结构:一个仪表板页面,其中有一个菜单,其中包含指向我的应用程序的所有子页面的链接。每个链接都在 <div> 内打开它显示在 jquery-ui 对话框中。

仪表板.jsp:

<%@ include file="../include/header.jsp" %>

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li> <c:url value="/Usuario/listagem" var="usuario"/> <a href="#" class="popup" data-action="${usuario}/1/10/1" data-target="popup-usuario">Usu&aacute;rios</a></li>
<li> <c:url value="/Permissao/listagem" var="permissao"/> <a href="#" class="popup" data-action="${permissao}/1/10/1" data-target="popup-permissao">Permiss&otilde;es</a></li>
<li> <c:url value="/Grupo/listagem" var="grupo"/> <a href="#" class="popup" data-action="${grupo}/1/10/1" data-target="popup-grupo">Grupos</a></li>
<li> <c:url value="/logout" var="logoutUrl"/> <a href="${logoutUrl}">Logout</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>

<div class="dialog" id="popup-usuario">
<div id="text"> </div>
</div>

<div class="dialog" id="popup-permissao">
<div id="text"> </div>
</div>

<div class="dialog" id="popup-grupo">
<div id="text"> </div>
</div>

<%@ include file="../include/footer.jsp" %>

正如我对上面的页面所做的那样,所有页面都有外部文件中的页眉和页脚,它们是:

标题

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page session="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>
<head>
<title>${param.name}</title>

<link href="${pageContext.servletContext.contextPath}/resources/jquery/css/custom/jquery-ui-1.10.4.custom.min.css" rel="stylesheet">
<link href="${pageContext.servletContext.contextPath}/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="${pageContext.servletContext.contextPath}/resources/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">

<link href="${pageContext.servletContext.contextPath}/resources/extra/css/starter-template.css" rel="stylesheet">
<link href="${pageContext.servletContext.contextPath}/resources/extra/css/signin.css" rel="stylesheet">
<link href="${pageContext.servletContext.contextPath}/resources/extra/css/table.css" rel="stylesheet">

</head>
<body>

页脚

<script src="${pageContext.servletContext.contextPath}/resources/jquery/js/jquery-2.1.1.min.js"></script>
<script src="${pageContext.servletContext.contextPath}/resources/jquery/js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="${pageContext.servletContext.contextPath}/resources/bootstrap/js/bootstrap.min.js"></script>

<script src="${pageContext.servletContext.contextPath}/resources/extra/js/jquery.md5.min.js"></script>
<script src="${pageContext.servletContext.contextPath}/resources/extra/js/form_submit.js"></script>
<script src="${pageContext.servletContext.contextPath}/resources/extra/js/form_valida.js"></script>
<script src="${pageContext.servletContext.contextPath}/resources/extra/js/page_link.js"></script>
<script src="${pageContext.servletContext.contextPath}/resources/extra/js/page_load.js"></script>

</body>
</html>

我的 Controller 映射的每个 View 都会将客户端发送到与此类似的 jsp 页面:

<jsp:include page="../../common/listagem.jsp">
<jsp:param name="name" value="Usuario"/>

<jsp:param name="elements" value="login"/>
<jsp:param name="elements" value="pnome"/>
<jsp:param name="elements" value="unome"/>
<jsp:param name="elements" value="email"/>

<jsp:param name="label" value="Login"/>
<jsp:param name="label" value="Nome"/>
<jsp:param name="label" value="Sobrenome"/>
<jsp:param name="label" value="E-mail"/>
</jsp:include>

其中 common/listagem.jsp 是这样的:

<%@ include file="../include/header.jsp" %>

<c:url value="/${param.name}/cadastra" var="cadastra"/>
<c:url value="/${param.name}/altera" var="altera"/>
<c:url value="/${param.name}/remove" var="remove"/>

<input type="hidden" name="pagina" value="${pagina}">
<input type="hidden" name="items" value="${items}">
<input type="hidden" name="ordem" value="${ordem}">

<sec:authorize access="hasPermission(#user, 'cadastra_${param.name}')">
<p>
<button type="button" class="btn btn-sm btn-link action" data-action="${cadastra}" data-target="cadastro">Cadastrar ${param.name}</button>
</p>
</sec:authorize>

<!-- Nav tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#listagem" data-toggle="tab">Listagem</a></li>

<sec:authorize access="hasPermission(#user, 'cadastra_${param.name}')">
<li><a href="#cadastro" data-toggle="tab">Cadastro</a></li>
</sec:authorize>

<sec:authorize access="hasPermission(#user, 'altera_${param.name}')">
<li><a href="#alteracao" data-toggle="tab">Altera&ccedil;&atilde;o</a></li>
</sec:authorize>

<sec:authorize access="hasPermission(#user, 'remove_${param.name}')">
<li><a href="#remocao" data-toggle="tab">Remo&ccedil;&atilde;o</a></li>
</sec:authorize>
</ul>

<!-- Tab panes -->
<div class="tab-content">
<div class="tab-pane active" id="listagem">
<div class="panel panel-default">

<div class="panel-heading">
Listagem
</div>

<div class="panel-body">
<p>...</p>
</div>

<table class="bordered">
<thead>
<tr>
<th class="col" data-property="#"></th>
<c:forEach var="item" items="${paramValues.elements}" varStatus="status">
<th class="col" data-property="<c:out value="${item}"/>">
<c:out value="${paramValues.label[status.index]}"/>
</th>
</c:forEach>
<th class="col" data-property=""></th>
</tr>
</thead>

<tbody class="content">
</tbody>

<tfoot>
<tr>
<sec:authorize access="hasPermission(#user, 'altera_${param.name}')">
<td class="comando" data-nome="altera" data-action="${altera}"></td>
</sec:authorize>
<sec:authorize access="hasPermission(#user, 'remove_${param.name}')">
<td class="comando" data-nome="remove" data-action="${altera}"></td>
</sec:authorize>
</tr>
</tfoot>
</table>
</div>
</div>

<div class="tab-pane" id="cadastro">
...
</div>

<div class="tab-pane" id="alteracao">
...
</div>

<div class="tab-pane" id="remocao">
...
</div>
</div>

<c:url value="/${param.name}/listagem.json" var="listagem"/>
<script>
$(document).ready(function(){
load_content("${listagem}", $('table'));
});
</script>

<%@ include file="../include/footer.jsp" %>

我的问题:

当我运行应用程序并打开每个子页面时,第一次一切都很好(除了表格仅显示一个项目,何时应显示所有项目)。如果我关闭该页面,则在每次打开该页面时,该表都会显示重复的列。

谁能看出这里出了什么问题吗?

ps.:处理仪表板链接点击的 JS 代码是这样的:

$( ".dialog" ).dialog({
autoOpen: false,
closeOnEscape: true,
closeText: "fechar",
show: {
effect: "fadeIn",
duration: 1000
},
hide: {
effect: "fadeOut",
duration: 1000
},
close: function( event, ui ) {
$(this).remove();
}
});
function add_dialog(container_div) {
var id_dialog_div = Math.floor(Math.random() * 1000000);
var dialog_div = $('<div id="dialog-'+id_dialog_div+'" class="dialog" title="Basic dialog"> <p> <span id="text'+id_dialog_div+'"></span> </p> </div>');
$(container_div).append(dialog_div);
return id_dialog_div;
}

function open_dialog(url, dialog_div) {
$.ajax({
type: "GET",
url: url
}).done(function(data){
var id_dialog_div = add_dialog(dialog_div);
var dialog_box = $('#dialog-'+id_dialog_div);

var $temp = $('<div/>', {html:data});

$( dialog_box ).dialog( { title: $temp.find('title').text() } );
$( dialog_box ).find('#text'+id_dialog_div).empty();
$( dialog_box ).find('#text'+id_dialog_div).html( $temp.remove('head').html() );
$( dialog_box ).dialog( { height: 680 } );
$( dialog_box ).dialog( { width: 1046 } );
$( dialog_box ).dialog( "open" );
});
}
$(document).on('click', '.popup', function (event) {
event.preventDefault();
var action = $(this).data('action');
var target = $(this).data('target');
var div = $("#"+target);
open_dialog(action, div);
});

动态生成表格的代码是这样的:

function load_content(lista, target) {
var atributos = [];

$(".col").each(function(){
var property = $(this).data('property');
atributos.push(property);
});

var pagina = $('input[name=pagina]').val();
var items = $('input[name=items]').val();
var ordem = $('input[name=ordem]').val();

var url = lista;
$.ajax({
type: 'GET',
url: url,
data: {pagina: pagina, items: items, ordem: ordem}
}).done(function(data){
var json = jQuery.parseJSON( data );
$.each(json.item, function(index, item){
target.find("tbody.content").empty();

var row = $('<tr id='+item.id+'>');
var counter = 0;

for(var i=0; i<atributos.length; i++) {
if(atributos[i] == '#') {
row.append('<td></td>');
}
else if(atributos[i] == '') {
var col = $('<td>');
$(".comando").each(function(){
var nome = $(this).data("nome");
var action = $(this).data("action");
col.append('<button type="button" class="btn btn-sm btn-primary action" data-target="'+nome+'" data-action="'+action+'/'+item.id+'">'+nome+'</button>');
});
row.append(col);
}
else {
var token = item[atributos[i]];
row.append('<td>'+token+'</td>');
}
}

target.find("tbody.content").append(row);
target.find("tfoot").hide();
counter++;
});
});
}

最佳答案

然后我终于解决了本主题中提出的问题。页面的部分仅显示一项,我解决了替换行: target.find("tbody.content").empty();出 block $.each(json.item, function(index, item) 。对于列的重复,我想这是通过对函数 open_dialog 进行一些修改来解决的,现在的代码是这样的:

function open_dialog(url, dialog_box) {
$.ajax({
type: "GET",
url: url
}).done(function(data){
var $temp = $('<div/>', {html:data});

$(dialog_box).find('.text').empty();
$(dialog_box).find('.text').html( $temp.remove('head').html() );
$(dialog_box).dialog( { title: $temp.find('title').text() } );
$(dialog_box).dialog( "open" );
});
}

在dashboard.jsp中, block <div>与对话框相关的是:

<div class="dialog" id="popup">
<div class="text"> </div>
</div>

关于javascript - 动态生成的表显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24308948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com