From 349bd97b6d46bfdef9bb311baa5526ed22dc926d Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Sat, 29 Sep 2018 14:53:15 -0500 Subject: [PATCH] support Software column sorting closes #536 --- .../listsTable/columns/Languages.tsx | 4 +- .../listsTable/columns/Software.tsx | 50 ++++++++++++++----- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Languages.tsx b/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Languages.tsx index 769dfe6e4..21643272e 100644 --- a/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Languages.tsx +++ b/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Languages.tsx @@ -43,9 +43,9 @@ const sortMethod = (a: number[], b: number[], languages: ILanguage[]) => { if (a && a.length > 0) { if (b && b.length > 0) { const aLanguageNames = - languages.filter((l: ILanguage) => l.id === a[0]).map((l: ILanguage) => l.name).join(); + languages.filter((l: ILanguage) => a.indexOf(l.id) > -1).map((l: ILanguage) => l.name).join(); const bLanguageNames = - languages.filter((l: ILanguage) => l.id === b[0]).map((l: ILanguage) => l.name).join(); + languages.filter((l: ILanguage) => b.indexOf(l.id) > -1).map((l: ILanguage) => l.name).join(); return aLanguageNames.toLowerCase() > bLanguageNames.toLowerCase() ? 1 : -1; } else { return -1; diff --git a/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Software.tsx b/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Software.tsx index 2a7fe0209..9896fcf96 100644 --- a/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Software.tsx +++ b/src/FilterLists.Web/ClientApp/modules/home/components/listsTable/columns/Software.tsx @@ -3,17 +3,20 @@ import { Column, Filter } from "react-table"; import { IColumnVisibility, ISoftware } from "../../../interfaces"; import { SoftwareIcon } from "../../softwareIcon"; -export const Software = (columnVisibility: IColumnVisibility[], software: ISoftware[]) => ({ - Header: "Software", - accessor: "syntaxId", - filterable: true, - filterMethod: (f: Filter, r: any[]) => filterMethod(f, r, software), - Filter: ({ filter, onChange }: any) => Filter({ onChange, filter }, software), - sortable: false, - Cell: (c: any) => Cell(c.value, software), - width: 155, - show: columnVisibility.filter((c: IColumnVisibility) => c.column === "Software")[0].visible -} as Column); +export const Software = (columnVisibility: IColumnVisibility[], software: ISoftware[]) => { + const softwareSorted = software.sort((a, b) => a.name.localeCompare(b.name)); + return ({ + Header: "Software", + accessor: "syntaxId", + filterable: true, + filterMethod: (f: Filter, r: any[]) => filterMethod(f, r, softwareSorted), + Filter: ({ filter, onChange }: any) => Filter({ onChange, filter }, softwareSorted), + sortMethod: (a: number, b: number) => sortMethod(a, b, softwareSorted), + Cell: (c: any) => Cell(c.value, software), + width: 155, + show: columnVisibility.filter((c: IColumnVisibility) => c.column === "Software")[0].visible + } as Column); +}; const filterMethod = (f: Filter, r: any[], software: ISoftware[]): boolean => { const isAny = f.value === "any"; @@ -30,11 +33,32 @@ const Filter = (props: any, software: ISoftware[]) => value={props.filter ? props.filter.value : "any"}> {software.length > 0 - ? software.sort((a, b) => a.name.localeCompare(b.name)).map( - (s: ISoftware, i: number) => ) + ? software.map((s: ISoftware, i: number) => ) : null} ; +const sortMethod = (a: number, b: number, software: ISoftware[]) => { + if (a) { + if (b) { + const aSoftwareNames = + software.filter((s: ISoftware) => s.syntaxIds.indexOf(a) > -1).map((s: ISoftware) => s.name); + const bSoftwareNames = + software.filter((s: ISoftware) => s.syntaxIds.indexOf(b) > -1).map((s: ISoftware) => s.name); + if (aSoftwareNames.length === bSoftwareNames.length) { + return aSoftwareNames.join().toLowerCase() > bSoftwareNames.join().toLowerCase() ? 1 : -1; + } else if (aSoftwareNames.length > bSoftwareNames.length) { + return -1; + } else { + return 1; + } + } else { + return -1; + } + } else { + return 1; + } +}; + const Cell = (listSyntaxId: number, software: ISoftware[]) => listSyntaxId ? software.filter((s: ISoftware) => s.syntaxIds.indexOf(listSyntaxId) > -1)