Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table visualization with column named "children" renders +/- buttons #4394

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions client/app/visualizations/table/Renderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import Table from 'antd/lib/table';
import Input from 'antd/lib/input';
import { RendererPropTypes } from '@/visualizations';

import { prepareColumns, filterRows, sortRows } from './utils';
import { prepareColumns, initRows, filterRows, sortRows } from './utils';

import './renderer.less';

export default function Renderer({ options, data, context }) {
const [rowKeyPrefix, setRowKeyPrefix] = useState(`row:1:${options.itemsPerPage}:`);
const [searchTerm, setSearchTerm] = useState('');
const [orderBy, setOrderBy] = useState([]);

Expand Down Expand Up @@ -37,7 +36,7 @@ export default function Renderer({ options, data, context }) {
);

const preparedRows = useMemo(
() => sortRows(filterRows(data.rows, searchTerm, searchColumns), orderBy),
() => sortRows(filterRows(initRows(data.rows), searchTerm, searchColumns), orderBy),
[data.rows, searchTerm, searchColumns, orderBy],
);

Expand Down Expand Up @@ -65,13 +64,11 @@ export default function Renderer({ options, data, context }) {
data-test="TableVisualization"
columns={tableColumns}
dataSource={preparedRows}
rowKey={(record, index) => rowKeyPrefix + index}
pagination={{
size: context === 'widget' ? 'small' : '',
position: 'bottom',
pageSize: options.itemsPerPage,
hideOnSinglePage: true,
onChange: (page, pageSize) => setRowKeyPrefix(`row:${page}:${pageSize}:`),
}}
/>
</div>
Expand Down
20 changes: 10 additions & 10 deletions client/app/visualizations/table/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,8 @@ export function prepareColumns(columns, searchInput, orderBy, onOrderByChange) {
const sortColumnIndex = isMultiColumnSort && orderByInfo[column.name] ? orderByInfo[column.name].index : null;

const result = {
key: column.name, // set this because we don't use `dataIndex`
// Column name may contain any characters (or be empty at all), therefore
// we cannot use `dataIndex` because it has special syntax and will not work
// for all possible column names. Instead, we'll generate row key dynamically
// based on row index
dataIndex: null,
key: column.name,
dataIndex: `record[${JSON.stringify(column.name)}]`,
align: column.alignContent,
title: (
<React.Fragment>
Expand Down Expand Up @@ -96,7 +92,7 @@ export function prepareColumns(columns, searchInput, orderBy, onOrderByChange) {
const initColumn = ColumnTypes[column.displayAs];
const Component = initColumn(column);
result.render = (unused, row) => ({
children: <Component row={row} />,
children: <Component row={row.record} />,
props: { className: `display-as-${column.displayAs}` },
});

Expand Down Expand Up @@ -135,6 +131,10 @@ export function prepareColumns(columns, searchInput, orderBy, onOrderByChange) {
return tableColumns;
}

export function initRows(rows) {
return map(rows, (record, index) => ({ key: `record${index}`, record }));
}

export function filterRows(rows, searchTerm, searchColumns) {
if ((searchTerm !== '') && (searchColumns.length > 0)) {
searchTerm = searchTerm.toUpperCase();
Expand All @@ -147,7 +147,7 @@ export function filterRows(rows, searchTerm, searchColumns) {
};
});

return filter(rows, row => some(matchFields, match => match(row)));
return filter(rows, row => some(matchFields, match => match(row.record)));
}
return rows;
}
Expand All @@ -164,8 +164,8 @@ export function sortRows(rows, orderBy) {
let va;
let vb;
for (let i = 0; i < orderBy.length; i += 1) {
va = a[orderBy[i].name];
vb = b[orderBy[i].name];
va = a.record[orderBy[i].name];
vb = b.record[orderBy[i].name];
if (isNil(va) || va < vb) {
// if a < b - we should return -1, but take in account direction
return -1 * directions[orderBy[i].direction];
Expand Down