簡體   English   中英

PrimeNG p-table header select 所有持久化,延遲加載和分頁

[英]PrimeNG p-table header select all persistence with lazy loading and pagination

當前配置(無法更新到最新):
"@angular/cli": "^7.3.9", "primeng": "7.0.5",

我有一個 PrimeNG p 表,其中包含帶有分頁的延遲加載數據。
PrimeNG GitHub 上也存在一個問題 - https://github.com/primefaces/primeng/issues/8139
Stackblitz 鏈接已附加在該問題中,因此沒有創建新鏈接。

設想:
第一頁,通過復選框選擇選擇了一些行。
在第二頁,Select header 中的所有復選框被選中,第二頁上的所有行都被自動選中。
現在,當導航到第一頁時,此處的選擇將被重置。 但是 header 中的 Select All 復選框仍處於選中狀態。

想知道是否有人有解決此問題的方法?

任何幫助表示贊賞。

編輯:在另一個類似的 GitHub 問題中找到的解決方案: https://github.com/primefaces/primeng/issues/6482

解決方案: https://github.com/primefaces/primeng/issues/6482#issuecomment-456644912

有人可以幫助在 Angular 7/8 應用程序中實現覆蓋。 無法理解如何獲取TableHeaderCheckbox引用並覆蓋原型。

好吧,問題的解決方案仍未添加到 PrimeNG 存儲庫中,因此即使是最新的 package 也沒有解決。

暫時,使用編輯下的問題中提到的解決方案

要回答我在Edit下提出的問題,請檢查以下內容:

// In some service file:

import { Table, TableHeaderCheckbox } from 'primeng/table';
import { ObjectUtils } from 'primeng/components/utils/objectutils';
import { uniq, each, intersection, map, remove } from 'lodash';

@Injectable()
export class BulkSelectAllPagesService {

overridePrimeNGTableMethods() {
    TableHeaderCheckbox.prototype.updateCheckedState = function () {
        const currentRows = map(this.dt.value, this.dt.dataKey);
        const selectedRows = map(this.dt.selection, this.dt.dataKey);
        this.rowsPerPageValue = this.dt.rows;
        const commonRows = intersection(currentRows, selectedRows);
        return commonRows.length === currentRows.length;
    };

    Table.prototype.toggleRowsWithCheckbox = function (event, check) {
        let _selection;
        if (!check) {
            _selection = this.value.slice();
            each(_selection, (row) => {
                const match = {}; match[this.dataKey] = row[this.dataKey];
                remove(this._selection, match);
            });
        } else {
            _selection = check ? this.filteredValue ? this.filteredValue.slice() : this.value.slice() : [];
            each(this._selection, (row) => {
                const match = {}; match[this.dataKey] = row[this.dataKey];
                remove(_selection, match);
            });
            this._selection = this._selection.concat(_selection);
        }

        this.preventSelectionSetterPropagation = true;
        this.updateSelectionKeys();
        this.selectionChange.emit(this._selection);
        this.tableService.onSelectionChange();
        this.onHeaderCheckboxToggle.emit({
            originalEvent: event,
            affectedRows: _selection,
            checked: check
        });
    };
}

// In app.component.ts

import { Component, OnInit } from '@angular/core';
import { BulkSelectAllPagesService } from 'PATH_TO_THE_FILE/bulk-select-all-pages.service';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {

    constructor(
        private bulkSelectAllPagesService: BulkSelectAllPagesService) {

    }

    ngOnInit() {
        this.bulkSelectAllPagesService.overridePrimeNGTableMethods();
    }
}

當然需要在 app.module.ts 中的providers[]中包含服務文件

將創建一個堆棧閃電戰並稍后添加。

處理行跨度分組數據的改進版本:

overridePrimeNGTableMethods() {
    TableHeaderCheckbox.prototype.updateCheckedState = function () {
        const currentRows = map(this.dt.value, this.dt.dataKey);
        const uniqueCurrentRows = uniq(currentRows);
        const selectedRows = map(this.dt.selection, this.dt.dataKey);
        this.rowsPerPageValue = this.dt.rows;
        const commonRows = intersection(currentRows, selectedRows);
        if (currentRows.length) {
            return commonRows.length === uniqueCurrentRows.length;
        } else {
            return false;
        }
    };

    Table.prototype.toggleRowWithCheckbox = function (event, rowData) {
        const findIndexesInSelection = (selection: any = [], data: any = {}, dataKey: any) => {
            const indexes = [];
            if (selection && selection.length) {
                selection.forEach((sel: any, i: number) => {
                    if (data[dataKey] === sel[dataKey]) {
                        indexes.push(i);
                    }
                });
            }
            return indexes;
        };

        this.selection = this.selection || [];
        const selected = this.isSelected(rowData);
        const dataKeyValue = this.dataKey ? String(ObjectUtils.resolveFieldData(rowData, this.dataKey)) : null;
        this.preventSelectionSetterPropagation = true;
        if (selected) {
            const selectionIndexes = findIndexesInSelection(this.selection, rowData, this.dataKey);
            const selectedItems = this.selection.filter((val: any) => {
                return val[this.dataKey] === rowData[this.dataKey];
            });
            this._selection = this.selection.filter((val: any, i: number) => {
                return selectionIndexes.indexOf(i) === -1;
            });
            this.selectionChange.emit(this.selection);
            selectedItems.forEach((selectedItem: any, index: number) => {
                this.onRowUnselect.emit({ originalEvent: event.originalEvent, index: event.rowIndex + index, data: selectedItem, type: 'checkbox' });
            });
            delete this.selectionKeys[rowData[this.dataKey]];
        } else {
            let rows = [rowData];
            if (dataKeyValue) {
                rows = this.value.filter(val => {
                    return (val[this.dataKey]).toString() === dataKeyValue;
                });
            }
            this._selection = this.selection ? this.selection.concat(rows) : rows;
            this.selectionChange.emit(this.selection);
            this.onRowSelect.emit({ originalEvent: event.originalEvent, index: event.rowIndex, data: rowData, type: 'checkbox' });
            if (dataKeyValue) {
                this.selectionKeys[dataKeyValue] = 1;
            }
        }
        this.tableService.onSelectionChange();
        if (this.isStateful()) {
            this.saveState();
        }
    };

    Table.prototype.toggleRowsWithCheckbox = function (event, check) {
        let _selection;
        if (!check) {
            _selection = this.value.slice();
            each(_selection, (row) => {
                const match = {}; match[this.dataKey] = row[this.dataKey];
                remove(this._selection, match);
            });
        } else {
            _selection = check ? this.filteredValue ? this.filteredValue.slice() : this.value.slice() : [];
            each(this._selection, (row) => {
                const match = {}; match[this.dataKey] = row[this.dataKey];
                remove(_selection, match);
            });
            this._selection = this._selection.concat(_selection);
        }

        this.preventSelectionSetterPropagation = true;
        this.updateSelectionKeys();
        this.selectionChange.emit(this._selection);
        this.tableService.onSelectionChange();
        this.onHeaderCheckboxToggle.emit({
            originalEvent: event,
            affectedRows: _selection,
            checked: check
        });
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM