Исправлено отображение данных в таблице "Заполнить режимы текущей скважины" и в таблице на странице "Режимы"

This commit is contained in:
ts_salikhov 2022-09-23 16:23:10 +04:00
parent 292d4e0ef0
commit 95950458c8
6 changed files with 46 additions and 49 deletions

View File

@ -1,38 +1,20 @@
import { InputNumber } from 'antd' import { InputNumber } from 'antd'
import { ReactNode } from 'react' import { ReactNode } from 'react'
import { makeNumericSorter, makeObjNumericSorter } from '../sorters' import { makeNumericSorter } from '../sorters'
import { columnPropsOther, makeGroupColumn, RenderMethod } from '.' import { columnPropsOther, makeGroupColumn, RenderMethod } from '.'
export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/ export const RegExpIsFloat = /^[-+]?\d+\.?\d*$/
export const makeNumericRender = <T extends unknown>(fixed?: number): RenderMethod<T> => (value) => { export const makeNumericRender = <T extends unknown>(fixed?: number, columnKey?: string): RenderMethod<T> => (value, obj) => {
let val = '-' let val = '-'
const isSelected = obj && columnKey ? obj[columnKey] : false
if ((value ?? null) !== null && Number.isFinite(+value)) { if ((value ?? null) !== null && Number.isFinite(+value)) {
val = (fixed ?? null) !== null val = (fixed ?? null) !== null
? (+value).toFixed(fixed) ? (+value).toFixed(fixed)
: (+value).toPrecision(5) : (+value).toPrecision(5)
} }
return (
<div className={'text-align-r-container'}>
<span>{val}</span>
</div>
)
}
type ObjRenderType = {
max: number
avg: number
min: number
isMax: boolean
isMin: boolean
}
export const makeObjRender = (key: keyof ObjRenderType, boolKey?: keyof ObjRenderType): RenderMethod<ObjRenderType> => (value: ObjRenderType) => {
const val = key ? value[key]: '-'
const isSelected = boolKey ? value[boolKey] : false
return ( return (
<div className={`text-align-r-container ${isSelected ? 'color-pale-green' : ''}`}> <div className={`text-align-r-container ${isSelected ? 'color-pale-green' : ''}`}>
<span>{val}</span> <span>{val}</span>
@ -40,7 +22,7 @@ export const makeObjRender = (key: keyof ObjRenderType, boolKey?: keyof ObjRende
) )
} }
export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string): columnPropsOther => ({ export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string, columnKey?: string): columnPropsOther => ({
editable: true, editable: true,
initialValue: 0, initialValue: 0,
width: 100, width: 100,
@ -50,20 +32,7 @@ export const makeNumericColumnOptions = (fixed?: number, sorterKey?: string): co
message: 'Введите число', message: 'Введите число',
pattern: RegExpIsFloat, pattern: RegExpIsFloat,
}], }],
render: makeNumericRender(fixed), render: makeNumericRender(fixed, columnKey),
})
export const makeObjColumnOptions = (key: keyof ObjRenderType, sortedObj?: string, boolKey?: keyof ObjRenderType): columnPropsOther => ({
editable: true,
initialValue: 0,
width: 100,
sorter: sortedObj && key ? makeObjNumericSorter(sortedObj, key) : undefined,
formItemRules: [{
required: true,
message: 'Введите число',
pattern: RegExpIsFloat,
}],
render: makeObjRender(key, boolKey),
}) })
export const makeNumericColumn = ( export const makeNumericColumn = (
@ -135,9 +104,9 @@ export const makeNumericAvgRange = (
renderDelegate: (_: any, row: object) => any, renderDelegate: (_: any, row: object) => any,
width: string width: string
) => makeGroupColumn(title, [ ) => makeGroupColumn(title, [
makeNumericColumn('мин', dataIndex, filters, filterDelegate, renderDelegate, width, makeObjColumnOptions('min', dataIndex, 'isMin')), makeNumericColumn('мин', dataIndex + 'Min', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Min', dataIndex + 'IsMin')),
makeNumericColumn('сред', dataIndex, filters, filterDelegate, renderDelegate, width, makeObjColumnOptions('avg', dataIndex)), makeNumericColumn('сред', dataIndex + 'Avg', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Avg')),
makeNumericColumn('макс', dataIndex, filters, filterDelegate, renderDelegate, width, makeObjColumnOptions('max', dataIndex, 'isMax')) makeNumericColumn('макс', dataIndex + 'Max', filters, filterDelegate, renderDelegate, width, makeNumericColumnOptions(fixed, dataIndex + 'Max', dataIndex + 'IsMax'))
]) ])
export default makeNumericColumn export default makeNumericColumn

View File

@ -7,9 +7,6 @@ import { DataType } from './Columns'
export const makeNumericSorter = <T extends unknown>(key: keyof DataType<T>) => export const makeNumericSorter = <T extends unknown>(key: keyof DataType<T>) =>
(a: DataType<T>, b: DataType<T>) => Number(a[key]) - Number(b[key]) (a: DataType<T>, b: DataType<T>) => Number(a[key]) - Number(b[key])
export const makeObjNumericSorter = <T extends unknown>(obj: keyof DataType<T>, key: keyof DataType<T>) =>
(a: DataType<DataType<T>>, b: DataType<DataType<T>>) => Number(a[obj][key]) - Number(b[obj][key])
export const makeStringSorter = <T extends unknown>(key: keyof DataType<T>) => (a?: DataType<T> | null, b?: DataType<T> | null) => { export const makeStringSorter = <T extends unknown>(key: keyof DataType<T>) => (a?: DataType<T> | null, b?: DataType<T> | null) => {
if (!a && !b) return 0 if (!a && !b) return 0
if (!a) return 1 if (!a) return 1

View File

@ -6,6 +6,7 @@ import { Table } from '@components/Table'
import LoaderPortal from '@components/LoaderPortal' import LoaderPortal from '@components/LoaderPortal'
import { invokeWebApiWrapperAsync } from '@components/factory' import { invokeWebApiWrapperAsync } from '@components/factory'
import { DrillParamsService } from '@api' import { DrillParamsService } from '@api'
import { getArrayConvertedObjectsIntoObjectsWithPrimitiveValues } from '@utils'
import { getColumns } from '@pages/WellOperations/WellDrillParams' import { getColumns } from '@pages/WellOperations/WellDrillParams'
@ -25,7 +26,7 @@ export const NewParamsTable = memo(({ selectedWellsKeys }) => {
async () => { async () => {
setIsParamsModalVisible(true) setIsParamsModalVisible(true)
const params = await DrillParamsService.getCompositeAll(well.id) const params = await DrillParamsService.getCompositeAll(well.id)
setParams(params) setParams(getArrayConvertedObjectsIntoObjectsWithPrimitiveValues(params))
}, },
setShowParamsLoader, setShowParamsLoader,
`Не удалось загрузить список режимов`, `Не удалось загрузить список режимов`,

View File

@ -2,7 +2,7 @@ import { useState, useEffect, useCallback, memo, useMemo } from 'react'
import { useWell } from '@asb/context' import { useWell } from '@asb/context'
import { import {
Table, EditableTable,
makeSelectColumn, makeSelectColumn,
makeNumericAvgRange, makeNumericAvgRange,
makeNumericSorter, makeNumericSorter,
@ -10,7 +10,7 @@ import {
import LoaderPortal from '@components/LoaderPortal' import LoaderPortal from '@components/LoaderPortal'
import { invokeWebApiWrapperAsync } from '@components/factory' import { invokeWebApiWrapperAsync } from '@components/factory'
import { DrillParamsService, WellOperationService } from '@api' import { DrillParamsService, WellOperationService } from '@api'
import { arrayOrDefault } from '@utils' import { arrayOrDefault, getArrayConvertedObjectsIntoObjectsWithPrimitiveValues } from '@utils'
export const getColumns = async (idWell) => { export const getColumns = async (idWell) => {
@ -44,9 +44,7 @@ export const WellDrillParams = memo(() => {
const updateParams = useCallback(async () => await invokeWebApiWrapperAsync( const updateParams = useCallback(async () => await invokeWebApiWrapperAsync(
async () => { async () => {
const params = arrayOrDefault(await DrillParamsService.getAll(well.id)) const params = arrayOrDefault(await DrillParamsService.getAll(well.id))
// Typescript против использования числа в качестве типа значения select setParams(getArrayConvertedObjectsIntoObjectsWithPrimitiveValues(params))
params.forEach((param) => param.idWellSectionType = `${param.idWellSectionType}`)
setParams(params)
}, },
setShowLoader, setShowLoader,
`Не удалось загрузить список режимов бурения`, `Не удалось загрузить список режимов бурения`,
@ -81,12 +79,15 @@ export const WellDrillParams = memo(() => {
return ( return (
<LoaderPortal show={showLoader}> <LoaderPortal show={showLoader}>
<Table <EditableTable
bordered bordered
size={'small'} size={'small'}
columns={columns} columns={columns}
dataSource={params} dataSource={params}
tableName={'well_drill_params'} tableName={'well_drill_params'}
onRowAdd={tableHandlers.add}
onRowEdit={tableHandlers.edit}
onRowDelete={tableHandlers.delete}
pagination={false} pagination={false}
/> />
</LoaderPortal> </LoaderPortal>

View File

@ -0,0 +1,26 @@
export type ObjType = {
[key:string]: Object | any
}
export function getArrayConvertedObjectsIntoObjectsWithPrimitiveValues(arr: Array<ObjType>) {
return arr.map(param => {
const newObj:ObjType = {}
for (const paramElement in param) {
if (typeof param[paramElement] === 'object') {
for (const item in param[paramElement]) {
newObj[paramElement + item[0].toUpperCase() + item.slice(1)] = param[paramElement][item]
}
} else {
// Typescript против использования числа в качестве типа значения select
if (paramElement === 'idWellSectionType') {
newObj[paramElement] = String(param[paramElement])
} else {
newObj[paramElement] = param[paramElement]
}
}
}
return newObj
})
}
export default getArrayConvertedObjectsIntoObjectsWithPrimitiveValues

View File

@ -27,3 +27,6 @@ export * from './svg'
export * from './table_settings' export * from './table_settings'
export type { TableColumnSettings, TableSettings, TableSettingsStore } from './table_settings' export type { TableColumnSettings, TableSettings, TableSettingsStore } from './table_settings'
export * from './getArrayConvertedObjectsIntoObjectsWithPrimitiveValues'
export type { ObjType } from './getArrayConvertedObjectsIntoObjectsWithPrimitiveValues'