2016-09-30 176 views
0

從ListView中刪除項目時,這些更改將不會反映在ListView中,並且不會在NativeScript中更新。我使用Firebase作爲後端,但數據在數據庫中正確更新。ListView在刪除後不會更新

任何人都請幫助我。我在下面提供我的代碼文件。拼接後,我得到了正確的結果,但列表視圖不會與該數據綁定。

list.model.ts

function delete(id) { 
return firebase.remove("/Groceries/" + id + ""); 
}; 

list.ts

var items = []; 
var itemsObject = new observableModule.fromObject({ 
groceryList: items 
}); 

export function deleteValues(args) { 
var item = args.view.bindingContext; 
var page = <pages.Page>args.object; 

deleteItems(item.key) 
    .then(
    () => { 
     items.splice(items.indexOf(item), 1); 
     page.bindingContext = itemsObject; 
    } 
    ).catch(
    () => { 
     alert("error"); 
    } 
    ) 
    }; 

list.xml

<Page loaded="loaded"> 
<GridLayout rows="auto, *" columns="2*, *"> 
<TextField id="grocery" text="{{ grocery }}" hint="Enter a grocery 
item" row="0" col="0" /> 
    <Button text="Add" tap="addGrocery" row="0" col="1" /> 
    <ListView id="groceryList" items="{{ groceryList }}" row="1" 
     colSpan="2"> 
     <ListView.itemTemplate> 
     <GridLayout columns="*, auto"> 
      <Label text="{{ name }}" horizontalAlignment="left" 
      verticalAlignment="center"/> 
      <Image src="~/images/delete.png" col="1" 
      tap="deleteValues" /> 
      </GridLayout> 
     </ListView.itemTemplate> 
    </ListView> 
</GridLayout> 

+1

要強制ListView更新,您可以使用'refresh'方法。 - https://docs.nativescript.org/api-reference/classes/_ui_list_view_.listview.html#refresh –

回答

1

您仍然可以定義爲itemList中ListView的作爲ObservableArray並使用th E中的內置splicefunction刪除目標項目:

因此,在你的代碼:

var items = new Observable.ObservableArray<any>(); 
items.splice(index, 1); 

基本上,這是一樣的,你做了什麼,但它應該如果你正在使用ObservableArray更新,而不是正常數組

+0

這有效。但只有第一項被刪除。而刪除第二個項目列表將不會更新。 – Khushi