如何从RadListView中的标签更改属性

时间:2019-04-15 06:53:24

标签: javascript android nativescript

我正在尝试访问RadListView中标签的属性以对其进行更改。根据是真还是假,颜色应该改变。

我试图通过getViewById获取引用以更改属性。 我得到的结果是不确定的。如何访问RadListView中的属性? 看到的是当前设置为true的变量

.JS

function createViewModel(args) {
    const viewModel = new Observable();

    //Prüft, ob die Artikel gesehen wurden 

    viewModel.set("itemss", itemss);

    if(Seen==true){
        const page = args.page;
        const label = page.getViewById("test");
        label.color = "green";

    }
    if(Seen==false){
      const page = args.page;
      const label = page.getViewById("test");
      label.color = "red";
    }








    return viewModel;
}

XML

<Page xmlns:lv="nativescript-ui-listview" xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:FAB="nativescript-floatingactionbutton" xmlns:sd="nativescript-ui-sidedrawer" class="page" loaded="pageLoaded" xmlns:customControls="shared">
    <Page.actionBar>
        <ActionBar icon="" class="action-bar">
        <label text="ZorrasBistro" class="actionlabel"/>
        <ActionItem tap="NewButtonTap" class="btn" ios.position="right" android.position="right"> 
        <label class="fas" style ="padding-right:20em; font-size:20em; color:grey" text="&#xf067;"/>
        </ActionItem>
        </ActionBar>
    </Page.actionBar>
    <sd:RadSideDrawer id="sideDrawer">
    <sd:RadSideDrawer.drawerContent>
        <customControls:sidedrawer/>
    </sd:RadSideDrawer.drawerContent>
    <sd:RadSideDrawer.mainContent>
        <StackLayout>
            <StackLayout>
                <lv:RadListView items="{{itemss}}" id="listview" height="350" row="1" class="list-group" selectionBehavior="Press" multipleSelection="false" itemSelected ="onItemSelected" >
                    <lv:RadListView.listViewLayout>
                        <lv:ListViewLinearLayout scrollDirection="Vertical" itemInsertAnimation="Slide" />
                    </lv:RadListView.listViewLayout>
                    <lv:RadListView.itemTemplate>
                        <GridLayout class="list-group-item" rows="auto" columns="auto,*" backgroundColor="White">
                            <Image row="0" col="0" src="{{itemSrc}}" class="thumb img-rounded" />
                            <StackLayout  row="0" col="1">
                                <Label  text="{{itemName}}" class="list-group-item-heading" />
                                <label text="{{itemDesc}}" class="list-group-item-text" />
                                <label  id="itemid" text="{{id}}" class="list-group-item-text"/>
                            </StackLayout>
                            <label id="test" row="0" col="3" text="&#xf056;" class="fas" color="green" style="padding-top:25em;color:black;opacity:0.2;"  verticalAligment="bottom" horizontalAlignment="right" />
                            </GridLayout>
                    </lv:RadListView.itemTemplate>
                    <lv:RadListView.itemSwipeTemplate class="Grid">
                        <GridLayout columns="auto, *, auto">
                            <StackLayout col="0" id="save" class="save-btn" tap="onLeftSwipe" orientation="horizontal">
                                <Label text="Details" id="save-text" verticalAlignment="center" horizontalAlignment="center" />
                            </StackLayout>
                            <StackLayout col="2" id="delete" class="delete-btn" tap="onRightSwipe" orientation="horizontal">
                                <Label text="Delete" id="delete-text" verticalAlignment="center" horizontalAlignment="center" />
                            </StackLayout>
                        </GridLayout>
                    </lv:RadListView.itemSwipeTemplate>
                </lv:RadListView>
            </StackLayout>
        </StackLayout>
    </sd:RadSideDrawer.mainContent>
    </sd:RadSideDrawer>
</Page>


TypeError: Cannot read property 'getViewById' of undefined
File: "file:///data/data/org.nativescript.preview/files/app/bestellung-overview/bestellung-overview-view.js, line: 40, column: 27

StackTrace:
        Frame: function:'createViewModel', file:'file:///data/data/org.nativescript.preview/files/app/bestellung-overview/bestellung-overview-view.js', line: 40, column: 28
        Frame: function:'_pageLoaded', file:'file:///data/data/org.nativescript.preview/files/app/bestellung-overview/bestellung-overview.js', line: 13, column: 27
        Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 110, column: 23
        Frame: function:'Observable._emit', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 127, column: 18
        Frame: function:'ViewBase.onLoaded', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 236, column: 14
        Frame: function:'View.onLoaded', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view/view.js', line: 235, column: 35
        Frame: function:'Page.onLoaded', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/page/page.js', line: 43, column: 35
        Frame: function:'', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 311, column: 90
        Frame: function:'ViewBase.callFunctionWithSuper', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 304, column: 9
        Frame: function:'ViewBase.callLoaded', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 311, column: 14
        Frame: function:'ViewBase.loadView', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 439, column: 18
        Frame: function:'ViewBase._addViewCore', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 434, column: 18
        Frame: function:'ViewBase._addView', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/core/view-base/view-base.js', line: 420, column: 14
        Frame: function:'FragmentCallbacksImplementation.onCreateView', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 658, column: 19
        Frame: function:'FragmentClass.onCreateView', file:'file:///data/data/org.nativescript.preview/files/app/tns_modules/tns-core-modules/ui/frame/fragment.js', line: 26, column: 38


        at com.tns.Runtime.callJSMethodNative(Native Method)
        at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1203)
        at com.tns.Runtime.callJSMethodImpl(Runtime.java:1083)
        at com.tns.Runtime.callJSMethod(Runtime.java:1070)
        at com.tns.Runtime.callJSMethod(Runtime.java:1050)
        at com.tns.Runtime.callJSMethod(Runtime.java:1042)
        at com.tns.FragmentClass.onCreateView(FragmentClass.java:54)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)


1 个答案:

答案 0 :(得分:0)

在使用 // i [2, 3, 3] // j / // v [2, 1, 3] 时,您不应通过元素的ID访问元素,也不应通过其引用手动更改item的属性。您应该始终更新数据并将适当的属性绑定到ListView /项目模板内的任何组件。

RadListView

根据需要更新Label,以便它自动更新列表项中的标签。

关于异常,我不确定您如何/在何处调用<label id="test" row="0" col="3" text="&#xf056;" class="fas" style="padding-top:25em;opacity:0.2;" verticalAligment="bottom" horizontalAlignment="right" color="{{ itemColor }}" /> 方法。但是您应该在itemColor中传递createViewModel属性,如果直接从页面加载事件中传递page,那么args应该为您提供对页面的引用。