import moment from 'moment'; import { ColumnProps } from './interface'; export const moneyFormat = (value: any) => { if (!value && value !== 0) return ''; try { let num = typeof value === 'string' ? parseFloat(value) : value; return num.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); } catch (error) { console.log(error); return '0.00'; } } export function formatValue(text: any, format: ColumnProps['format']) { if (typeof text === 'boolean') return text ? '是' : '否'; if ((!text && text !== 0) || !format) return text; if (format === 'time') return moment(text).format('YYYY/MM/DD HH:mm:ss'); if (format === 'date') return moment(text).format('YYYY/MM/DD'); if (format === 'money') return moneyFormat(text); return text; } export const ROW_HEIGHT = 39; export function getScrollY(height: number, len: number) { const bodyHeight = height - ROW_HEIGHT; // 获取 table body高度 const listHeight = len * ROW_HEIGHT; // 当前list 占用总高度 return listHeight > bodyHeight ? bodyHeight : undefined; } export function getScrollX(width: number, columns: ColumnProps[]) { const columnsTotalWidth = columns.reduce((next,cur) => next += (Number(cur.width) || 0),0); return columnsTotalWidth > width ? columnsTotalWidth : undefined } export function sortDataSource(column: ColumnProps) { return (rowA: T, rowB: T) => { const a = rowA[column.dataIndex as string] || null, b = rowB[column.dataIndex as string] || null; if(a === b) return 0; if(typeof a === 'number' || typeof b === 'number') return a > b ? 1 : -1; if(a === null) return 1; if(b === null) return -1; return (typeof a === 'string' || typeof b === 'string') ? (a.localeCompare(b, undefined, {numeric: true})) : (a > b ? 1 : -1); } }