在Worklight 6.0中填充JSON存储数据

时间:2013-11-18 12:56:28

标签: underscore.js ibm-mobilefirst jsonstore

我在显示推送到数据库的JSON Store数据时遇到问题。这是我的代码片段,用于显示推送到服务器的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);  
    };  

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

2 个答案:

答案 0 :(得分:1)

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

$('div#status-field').empty();

答案 1 :(得分:0)

根据我的理解,status.html(html)取代了整个内容,并且无法附加内容。

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

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