0

我在显示推送到数据库的 JSON 存储数据时遇到问题。这是我的代码片段,用于显示推送到服务器的 json 存储文档。

var _showTable = function (arr) {  

if (_.isArray(arr) && arr.length < 1) {  
return _logMessage(EMPTY_TABLE_MSG);  
}  

    //Log to the console  
        WL.Logger.ctx({stringify: true, pretty: true}).info(arr);  
        var  
    //Get reference to the status field  
        status = $('div#status-field'),  
    //Table HTML template  

    table = ['<table id="bu_table" >', 
        '<tr>',  
        '<td><b>JSON ID</b></td>',  
        '<td><b>BU NAME</b></td>',  
        '<td><b>BU DESC</b></td>',  
        '</tr>',  
    '<% _.each(bu, function(results) { %>',  
    '<tr>',  
    '<td> <%= results._id %> </td>',  
    '<td> <%= results.json.buname %> </td>',  
    '<td> <%= results.json.budesc %> </td>',  
    '</tr>',  
    '<% }); %>',  
    '</table>'  
    ].join(''),  
    //Populate the HTML template with content  
    html = _.template(table, {bu : arr});
    //Put the generated HTML table into the DOM  
    status.html(html);  
    };  

我正在尝试通过单击按钮来显示数据库中的文档。每次单击按钮时,整个文档都会附加到表格中,而不是替换。每次单击按钮时,如何替换文档而不是重复?

4

2 回答 2

1

在追加新数据之前清除表。

$('div#status-field').empty();
于 2013-11-18T16:25:30.510 回答
0

据我了解,status.html (html) 替换了整个内容,它不可能追加内容。

您是否确认“arr”变量仅包含您已经尝试显示的内容?(我想知道它是否只是比您预期的要多得多)。

最后,我对下划线不是很熟悉,但是“每个”指令是否有可能生成这些重复项?

于 2013-11-29T23:48:12.403 回答