NativeScript动态内容显示

时间:2018-06-14 11:15:11

标签: javascript nativescript nativescript-telerik-ui

第一次尝试使用NativeScript。我有一个SQLite数据库,想在其中一个屏幕上显示它的内容:

new Sqlite("sql.db", function (err, db) {
 json = 'companies: [';
   var promise = db.each('SELECT * FROM companies', function (err, row) {
json += '{ id:' + row[0] + ', category:' + row[1] + ', title:"' + row[2] + '", desc:"' + row[3] + '", address:"' + row[4] + '", post_code:"' + row[5] + '", logo:"' + row[12] + '"}';
content = json + ']'; });
   promise.then(function (count) { });
   var newstr = content.replace(/(\r\n\t|\n|\r\t)/gm, "");
   HomeViewModel(newstr);
}); 

我的HomeViewModel看起来像:

function HomeViewModel(details) {
     if (typeof details !== "undefined") {
       var viewModel = observableModule.fromObject({
        details,
        onItemTap: function (args) {
          console.log('Item with index: ' + args.index + ' tapped');
        },
       });
     return viewModel;
     } 
}

XML看起来像:

 <ListView id="listview" class="list-group" items="{{ companies }}" itemTap="{{ onItemTap }}" height="100%">
            <ListView.itemTemplate>
                <FlexboxLayout flexDirection="row" class="list-group-item" tap="changePage" sup="{{ id }}">
                    <Image src="{{ logo }}" class="thumb img-circle" />
                    <Label text="{{ title }}" class="list-group-item-heading" verticalAlignment="center" style="width: 60%" />
                </FlexboxLayout>
            </ListView.itemTemplate>
        </ListView>

但不显示信息。如果我将函数“HomeViewModel”中的“details”替换为以下内容:

companies: [
    { id: 1, category: 2, title: "Hello World", logo: "https://play.nativescript.org/dist/assets/img/flags/au.png" },
    { id: 2, category: 3, title: "World Hello", logo: "https://play.nativescript.org/dist/assets/img/flags/ru.png" },
  ]

一切都很好。请帮忙。

谢谢...

0 个答案:

没有答案