动态生成的表显示有错误

时间:2014-06-19 14:23:02

标签: javascript jquery html jsp

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

dashboard.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>

我的控制器映射的每个视图都将客户端发送到类似于此的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++;
        });
    });
}

1 个答案:

答案 0 :(得分:0)

然后我终于解决了本主题中提出的问题。页面中只显示一个项目的部分,我解决了从块target.find("tbody.content").empty();中替换行$.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中,与对话框相关的块<div>是:

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