【问题标题】:How to set true the ui/switches in list-view (NativeScript)?如何在列表视图(NativeScript)中将 ui/switches 设置为 true?
【发布时间】:2017-03-07 16:43:40
【问题描述】:

我有一个 listview,每个都有一个开关组件,在页脚有一个按钮,其功能是设置所有开关的真假。 下一个代码用于将开关设置为 true:

var listView1 = view.getViewById(page, "listView");
var label = listView.getViewById('id_check');  
label.checked = true; 

但是我只能设置一个项目的状态,我需要改变所有项目的状态。怎么办?

谢谢

已编辑:

我写了以下代码,但我有一个错误:

for (var i = 0, size = historiqueList.length; i < size ; i++) {
var listView = view.getViewById(page, "listView1");
var label = listView.getItem(i).id_check;
label.checked = true; 
}

错误:无法设置属性“已检查”

【问题讨论】:

    标签: listview nativescript uiswitch


    【解决方案1】:

    对于您的描述,我认为您引用了具有相同 ID 的所有开关,因此当您尝试访问它们时,您只能访问最后一个引用的开关。尝试使用一个数组,您可以将它们全部存储并循环它以在需要时更改所有值。

    【讨论】:

    • 如果是这种情况 - 在列表项的 xml 定义中更容易做到。将选中的属性设置为 true。
    【解决方案2】:

    你必须拦截 onItemLoading 事件。

    以下是 NS 与 TS 的伪代码。

    public onItemLoading(args) {
    let item : ListItem = this._listItemArray.getItem( args.itemIndex );
    
    let switch = args.view.getViewById("switch");
    
    switch.enabled = true;  // this could be incorrect
    }
    

    这是 RadListView - 但我确信 ListView 是相似的。

    this._listItemArray 是 ListView 中显示的数据数组。

    【讨论】:

    • 谢谢。我这样写: for (var i = 0, size = historiqueList.length; i 但我有一个错误:无法设置属性“已检查”
    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 2015-05-18
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多