【问题标题】:How to retrive values from Database in Titanium Alloy?如何从钛合金数据库中检索值?
【发布时间】:2014-09-22 11:33:17
【问题描述】:

我是 Titanium API 的新手。我想知道如何从数据库中检索值并在 UI 中显示。我创建了一个模型并插入了一行。我的代码如下,

型号:

var moment = require('alloy/moment');

exports.definition = {
    config : {
            "columns": {
            "id":"text",
            "LanguageName":"text"
            },
            "adapter": {
                "type": "sql",
                "collection_name": "UserLanguage"
            }
    },

    extendModel: function(Model) {      
        _.extend(Model.prototype, {

        });

        return Model;
    },

    extendCollection: function(Collection) {        
        _.extend(Collection.prototype, {

        });

        return Collection;
    }
};

查看:

<Alloy>
    <Window id="changeLangWindow" title="Plan India">
        <Picker id="langPicker">
            <PickerRow title="Select a Language"/>
            <PickerRow title="English"/>
            <PickerRow title="French"/>
            <PickerRow title="Spanish"/>
        </Picker>
        <Button class="button" onClick="saveLang">Proceed</Button>
        <Label class="question">Selected Language is: -------</Label>
    </Window>
</Alloy>

控制器:

function saveLang() {

    var UserLang = Alloy.Collections.UserLanguage;

    // Create a new model for the todo collection
    var task = Alloy.createModel('UserLanguage', {
        id : '1',
        LanguageName : 'English'
    });

    // add new model to the global collection
      UserLang.add(task);

    // save the model to persistent storage
    task.save();

    // reload the tasks
     UserLang.fetch();

}

我想从“UserLanguage”模型中选择“LanguageName”的值并显示在 XML 文件中的视图中。

请有任何解释的建议!

【问题讨论】:

    标签: javascript titanium titanium-mobile titanium-alloy titanium-modules


    【解决方案1】:

    // 试试这个代码可能对你有帮助。

    if (Alloy.Collections.UserLanguage.length) {
         Alloy.Collections.UserLanguage.map(function(obj) {
    
            Ti.API.Log(" LanguageName "+ obj.get('LanguageName'));
            Ti.API.Log(" LanguageName "+ obj.id);
    
         });
    
    }
    

    谢谢,

    【讨论】:

    • 是的,它正在工作。但是我该如何写 WHERE 条件,比如 select LanguageName where id=7;
    • 还有我们如何在 UI 上显示选定的值。那是如何将选定的值从 js 文件传递​​到 xml?
    • 你可以通过ID "var model = Alloy.Collections.UserLanguage.get("your id");"这样获取
    • 如果我们在选择查询中得到多个值,我们该如何处理呢?
    • 如果这个答案对你有用或有帮助。然后接受这个答案:) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多