Web Components Grid Selection Overview
With the Ignite UI for Web Components Select feature in Web Components Grid you can easily interact with and manipulate data using simple mouse interactions. There are three selection modes available:
- Seleção de linha
- Seleção de células
- Column selection
With the rowSelection
property, you can specify:
- Nenhum
- Solteiro
- Seleção múltipla
Web Components Grid Selection Example
The sample below demonstrates three types of cell selection behaviors in the IgcGridComponent
. Use the buttons below to enable each of the available selection modes.
Web Components Grid Selection Options
The Ignite UI for Web Components IgcGridComponent
component provides three different selection modes - Row selection, Cell selection and Column selection. By default only Multi-cell selection mode is enabled in the IgcGridComponent
. In order to change/enable selection mode you can use rowSelection
, cellSelection
or selectable
properties.
Web Components Grid Row Selection
permite rowSelection
que você especifique as seguintes opções:
None
- Row selection would be disabled for theIgcGridComponent
.Single
- Selection of only one row within theIgcGridComponent
would be available.Multiple
- A seleção de várias linhas estaria disponível usando os seletores de linha, com uma combinação de teclas como ctrl + clique, ou pressionando a tecla de espaço quando uma célula estiver em foco.
Vá para o tópico Seleção de linha para obter mais informações.
Web Components Grid Cell Selection
permite cellSelection
que você especifique as seguintes opções:
None
- Cell selection would be disabled for theIgcGridComponent
.Single
- Selection of only one cell within theIgcGridComponent
would be available.Multiple
- Currently, this is the default state of the selection in theIgcGridComponent
. Multi-cell selection is available by mouse dragging over the cells, after a left button mouse clicked continuously.
Vá para o tópico Seleção de célula para obter mais informações.
Web Components Grid Column Selection
The selectable
property enables you to specify the following options for each IgcColumnComponent
. The corresponding column selection will be enabled or disabled if this property is set to true or false, respectively.
Isso leva às três variações seguintes:
- Seleção única -mouse click sobre a célula da coluna.
- Seleção de várias colunas - segurando ctrl + mouse click sobre as células da coluna.
- Seleção de coluna de intervalo - segurando shift + mouse click seleciona tudo no meio.
Acesse o tópico Seleção de coluna para obter mais informações.
Web Components Grid Context Menu
Using the ContextMenu
event you can add a custom context menu to facilitate your work with IgcGridComponent
. With a right click on the grid's body, the event emits the cell on which it is triggered. The context menu will operate with the emitted cell.
Se houver uma seleção de várias células, colocaremos lógica, que verificará se a célula selecionada está na área da seleção de várias células. Se for, também emitiremos os valores das células selecionadas.
Basicamente, a função principal ficará assim:
public rightClick(event: any) {
const eventArgs = event.detail;
eventArgs.event.preventDefault();
this.multiCellArgs = {};
if (this.multiCellSelection) {
const node = eventArgs.cell.selectionNode;
const isCellWithinRange = this.grid.getSelectedRanges().some((range) => {
if (
node.column >= range.columnStart &&
node.column <= range.columnEnd &&
node.row >= range.rowStart &&
node.row <= range.rowEnd
) {
return true;
}
return false;
});
if (isCellWithinRange) {
this.multiCellArgs = { data: this.multiCellSelection.data };
}
}
this.contextmenuX = eventArgs.event.clientX;
this.contextmenuY = eventArgs.event.clientY;
this.clickedCell = eventArgs.cell;
this.toggleContextMenu();
}
O menu de contexto terá as seguintes funções:
- Copie o valor da célula selecionada.
- Copie o dataRow da célula selecionada.
- Se a célula selecionada estiver dentro de um intervalo de seleção de várias células, copie todos os dados selecionados.
public copySelectedRowData() {
const selectedData = this.grid.getRowData(this.clickedCell.id.rowID);
this.copyData(selectedData);
const selectedDataArea = document.getElementById('selectedArea');
selectedDataArea.innerText = JSON.stringify(selectedData);
this.toggleContextMenu();
}
public copySelectedCellData() {
const selectedData = this.clickedCell.value;
this.copyData(selectedData);
const selectedDataArea = document.getElementById('selectedArea');
selectedDataArea.innerText = JSON.stringify(selectedData);
this.toggleContextMenu();
}
public copySelectedData() {
const selectedData = this.grid.getSelectedData();
this.copyData(selectedData);
const selectedDataArea = document.getElementById('selectedArea');
selectedDataArea.innerText = JSON.stringify(selectedData);
this.toggleContextMenu();
}
private copyData(data: any[]) {
const tempElement = document.createElement('input');
document.body.appendChild(tempElement);
tempElement.setAttribute('id', 'temp_id');
(document.getElementById('temp_id') as HTMLInputElement).value = JSON.stringify(data);
tempElement.select();
document.execCommand('copy');
document.body.removeChild(tempElement);
}
The IgcGridComponent
will fetch the copied data and will paste it in a container element.
O modelo que vamos usar para combinar a grade com o menu de contexto:
<div class="container sample">
<div class="wrapper">
<igc-grid auto-generate="false" width="50%" height="100%" name="grid" id="grid">
<igc-column field="ProductID" header="Product ID">
</igc-column>
<igc-column field="ProductName" header="Product Name">
</igc-column>
<igc-column field="UnitsInStock" header="Units In Stock" data-type="number">
</igc-column>
<igc-column field="UnitPrice" header="Units Price" data-type="number">
</igc-column>
<igc-column field="Discontinued" data-type="boolean">
</igc-column>
<igc-column field="OrderDate" header="Order Date" data-type="date">
</igc-column>
</igc-grid>
<div id="selectedArea" class="selected-data-area">
</div>
</div>
</div>
<div id="menu" style="display: none;" class="contextmenu">
<span id="copySingleCell" class="item">
<igc-icon name="content_copy"></igc-icon>Copy Cell Data
</span>
<span id="copyRow" class="item">
<igc-icon name="content_copy"></igc-icon>Copy Row Data
</span>
<span id="copyMultiCells" class="item">
<igc-icon name="content_copy"></igc-icon>Copy Cells Data
</span>
</div>
</div>
Selecione várias células e pressione o botão direito do mouse. O menu de contexto aparecerá e após selecionar Copy cells data os dados selecionados aparecerão na caixa vazia à direita.
O resultado é:
Known Issues and Limitations
When the grid has no primaryKey
set and remote data scenarios are enabled (when paging, sorting, filtering, scrolling trigger requests to a remote server to retrieve the data to be displayed in the grid), a row will lose the following state after a data request completes:
- Seleção de linha
- Expandir/recolher linha
- Edição de linha
- Fixação de linha
API References
Additional Resources
- Seleção de linha
- Seleção de células
- Paginação
- Filtragem
- Classificação
- Resumos
- Movimentação de Colunas
- Virtualização e desempenho
Nossa comunidade é ativa e sempre acolhedora para novas ideias.