2017-10-18 17 views
8

我是ng2-smart-tables的新手。我試圖從GitHub頁面修改下面的例子,以便在從一個頁面移動到另一個頁面時,複選框不會消失。ng2智能表格複選框在所有頁面中不存在

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'basic-example-multi-select', 
    template: ` 
    <ng2-smart-table [settings]="settings" [source]="data"></ng2-smart-table> 
    `, 
}) 
export class BasicExampleMultiSelectComponent { 

    settings = { 
    selectMode: 'multi', 
    columns: { 
     id: { 
     title: 'ID', 
     }, 
     name: { 
     title: 'Full Name', 
     }, 
     username: { 
     title: 'User Name', 
     }, 
     email: { 
     title: 'Email', 
     }, 
    }, 
    }; 

    data = [ 
    { 
     id: 1, 
     name: 'Leanne Graham', 
     username: 'Bret', 
     email: '[email protected]', 
    }, 
    { 
     id: 2, 
     name: 'Ervin Howell', 
     username: 'Antonette', 
     email: '[email protected]', 
    }, 
    { 
     id: 3, 
     name: 'Clementine Bauch', 
     username: 'Samantha', 
     email: '[email protected]', 
    }, 
    { 
     id: 4, 
     name: 'Patricia Lebsack', 
     username: 'Karianne', 
     email: '[email protected]', 
    }, 
    { 
     id: 5, 
     name: 'Chelsey Dietrich', 
     username: 'Kamren', 
     email: '[email protected]', 
    }, 
    { 
     id: 6, 
     name: 'Mrs. Dennis Schulist', 
     username: 'Leopoldo_Corkery', 
     email: '[email protected]', 
    }, 
    { 
     id: 7, 
     name: 'Kurtis Weissnat', 
     username: 'Elwyn.Skiles', 
     email: '[email protected]', 
    }, 
    { 
     id: 8, 
     name: 'Nicholas Runolfsdottir V', 
     username: 'Maxime_Nienow', 
     email: '[email protected]', 
    }, 
    { 
     id: 9, 
     name: 'Glenna Reichert', 
     username: 'Delphine', 
     email: '[email protected]', 
    }, 
    { 
     id: 10, 
     name: 'Clementina DuBuque', 
     username: 'Moriah.Stanton', 
     email: '[email protected]', 
    }, 
    { 
     id: 11, 
     name: 'Nicholas DuBuque', 
     username: 'Nicholas.Stanton', 
     email: '[email protected]', 
    }, 
    ]; 
} 

這使用selectMode:'multi'選項來顯示帶有複選框的列。複選框會顯示,但每次我使用分頁鏈接轉到另一頁時,該選擇被清除。我試圖解決這個問題,因爲我的項目有類似這樣的問題。

我試圖找到有關如何在頁面間持續選擇的文檔,但由於只有有限的文檔可用,因此不成功。這似乎是一個常見的功能,應該有更多關於此的信息,但似乎並非如此。任何有關這個問題的幫助將不勝感激。

回答

1

我沒有使用多選與NG2-智能表自己,但documentation提到

doEmit:布爾 - 發射事件(刷新表)或沒有,默認爲真

我不確定這是否可行,但您可以嘗試將其設置爲false

從數據創建數據源,然後再修改分頁程序設置:

source: LocalDataSource; 

constructor() { 
    this.source = new LocalDataSource(this.data); 
    this.source.setPaging({ doEmit: false }); 
} 

如果這不起作用,你可以嘗試添加事件偵聽器收集檢查和核對行重新選擇它們刷新(或初始化)。將事件回調添加到表中...

<ng2-smart-table [settings]="settings" [source]="source" (rowSelect)="onRowSelect($event)" (userRowSelect)="onUserRowSelect($event)"></ng2-smart-table> 

...記錄事件並查看您是否從那裏獲得任何可用信息。

onRowSelect(event) { 
    console.log(event); 
} 

onUserRowSelect(event) { 
    console.log(event); 
} 

如果沒有這種幫助,打開GitHub上一個新的問題,並希望開發者知道一個簡單的方法來解決這個問題。 :-) 如果這也失敗了,請按照我所做的操作切換到angular/material2。他們的文檔很糟糕,但總體而言,我認爲它比大多數組件都好。

-1

如果要維護應用程序的實時數據,則必須以「持久方式」保存此數據並使用保存在ngOnInit中的數據。 在一個部件中,我使用ngOnDestroy和DataService的

@Component({ 
}) 

export class MyComponent implements OnInit,OnDestroy {} 
    variable1:number 
    variable2:number 
    contructor(private state:MyComponentData) 
    ngOnInit() { 
     let data=this.state.Data?data:null; 
     this.variable1=(data)?data.variable1; 
     this.variable2=(data)?data.variable2; 
    } 
    ngOnDestroy() 
    { 
     this.state.Data={ 
      variable1:this.variable1, 
      variable2:this.variable2 
     } 
    } 

該服務那麼容易

@Injectable() 
export class MyComponentData{ 
    Data:any; 
} 
相關問題