mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-05-18 17:31:35 +00:00
Add SI-prefix-aware sorting column for the parts table
Adds an optional "Name (SI)" column that parses numeric values with SI prefixes (p, n, u/µ, m, k/K, M, G, T) from part names and sorts by the resulting physical value. This is useful for electronic components where alphabetical sorting produces wrong results — e.g. 100nF, 10pF, 1uF should sort as 10pF < 100nF < 1uF. Implementation: - New SiValueSort DQL function with platform-specific SQL generation for PostgreSQL (POSIX regex), MySQL/MariaDB (REGEXP_SUBSTR), and SQLite (PHP callback registered via the existing middleware). - The regex is start-anchored: only names beginning with a number are matched. Part numbers like "MCP2515" or "Crystal 20MHz" are ignored. - When SI sort is active, NATSORT is appended as a secondary sort so that non-matching parts fall back to natural string ordering instead of appearing in arbitrary order. - The column is opt-in (not in default columns) and displays the parsed float value, or an empty cell for non-matching names.
This commit is contained in:
parent
5b86d6f652
commit
63486782c4
7 changed files with 414 additions and 0 deletions
|
|
@ -38,6 +38,7 @@ use App\DataTables\Filters\PartFilter;
|
|||
use App\DataTables\Filters\PartSearchFilter;
|
||||
use App\DataTables\Helpers\ColumnSortHelper;
|
||||
use App\DataTables\Helpers\PartDataTableHelper;
|
||||
use App\Doctrine\Functions\SiValueSort;
|
||||
use App\Doctrine\Helpers\FieldHelper;
|
||||
use App\Entity\Parts\ManufacturingStatus;
|
||||
use App\Entity\Parts\Part;
|
||||
|
|
@ -118,6 +119,17 @@ final class PartsDataTable implements DataTableTypeInterface
|
|||
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderName($context),
|
||||
'orderField' => 'NATSORT(part.name)'
|
||||
])
|
||||
->add('si_name', TextColumn::class, [
|
||||
'label' => $this->translator->trans('part.table.si_name'),
|
||||
'render' => function ($value, Part $context): string {
|
||||
$siValue = SiValueSort::sqliteSiValue($context->getName());
|
||||
if ($siValue !== null) {
|
||||
return htmlspecialchars(sprintf('%g', $siValue));
|
||||
}
|
||||
return '';
|
||||
},
|
||||
'orderField' => 'SI_VALUE_SORT(part.name)',
|
||||
])
|
||||
->add('id', TextColumn::class, [
|
||||
'label' => $this->translator->trans('part.table.id'),
|
||||
])
|
||||
|
|
@ -484,6 +496,19 @@ final class PartsDataTable implements DataTableTypeInterface
|
|||
//$builder->addGroupBy('_bulkImportJob');
|
||||
}
|
||||
|
||||
//When sorting by SI value, add NATSORT as a secondary sort so that parts without
|
||||
//an SI-prefixed value fall back to natural string ordering seamlessly.
|
||||
$orderByParts = $builder->getDQLPart('orderBy');
|
||||
foreach ($orderByParts as $orderBy) {
|
||||
foreach ($orderBy->getParts() as $part) {
|
||||
if (str_contains($part, 'SI_VALUE_SORT')) {
|
||||
$direction = str_contains($part, 'DESC') ? 'DESC' : 'ASC';
|
||||
$builder->addOrderBy('NATSORT(part.name)', $direction);
|
||||
break 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue