From 30d95605df9acbd36a6d229d69e26b56c96fd650 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 21 Mar 2024 10:03:08 +0800 Subject: [PATCH] :art: Database select field supports sorting by option order Fix https://github.com/siyuan-note/siyuan/issues/10665 --- kernel/av/sort.go | 33 ++++++++++++++++++++++++--------- kernel/av/table.go | 4 ++-- kernel/model/attribute_view.go | 4 ++-- kernel/model/export.go | 4 ++-- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/kernel/av/sort.go b/kernel/av/sort.go index ade93d63f..681ea7fc7 100644 --- a/kernel/av/sort.go +++ b/kernel/av/sort.go @@ -25,7 +25,7 @@ import ( ) type Sortable interface { - SortRows() + SortRows(attrView *AttributeView) } type ViewSort struct { @@ -40,7 +40,7 @@ const ( SortOrderDesc SortOrder = "DESC" ) -func (value *Value) Compare(other *Value) int { +func (value *Value) Compare(other *Value, attrView *AttributeView) int { switch value.Type { case KeyTypeBlock: if nil != value.Block && nil != other.Block { @@ -112,14 +112,29 @@ func (value *Value) Compare(other *Value) int { return 0 } case KeyTypeSelect, KeyTypeMSelect: - if nil != value.MSelect && nil != other.MSelect { - var v1 string - for _, v := range value.MSelect { - v1 += v.Content + if 0 < len(value.MSelect) && 0 < len(other.MSelect) { + v1 := value.MSelect[0].Content + v2 := other.MSelect[0].Content + if v1 == v2 { + return 0 } - var v2 string - for _, v := range other.MSelect { - v2 += v.Content + + key, _ := attrView.GetKey(value.KeyID) + if nil != key { + optionSort := map[string]int{} + for i, op := range key.Options { + optionSort[op.Name] = i + } + + v1Sort := optionSort[v1] + v2Sort := optionSort[v2] + if v1Sort > v2Sort { + return 1 + } + if v1Sort < v2Sort { + return -1 + } + return 0 } return strings.Compare(v1, v2) } diff --git a/kernel/av/table.go b/kernel/av/table.go index 1a57055dc..0930b01eb 100644 --- a/kernel/av/table.go +++ b/kernel/av/table.go @@ -153,7 +153,7 @@ func (table *Table) GetID() string { return table.ID } -func (table *Table) SortRows() { +func (table *Table) SortRows(attrView *AttributeView) { if 1 > len(table.Sorts) { return } @@ -220,7 +220,7 @@ func (table *Table) SortRows() { return colIndexSort.Order != SortOrderAsc } - result := val1.Compare(val2) + result := val1.Compare(val2, attrView) if 0 == result { sorted = false continue diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 9108317e4..cd52a232d 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -724,7 +724,7 @@ func renderAttributeView(attrView *av.AttributeView, viewID, query string, page, } viewable.FilterRows(attrView) - viewable.SortRows() + viewable.SortRows(attrView) viewable.CalcCols() // 分页 @@ -2010,7 +2010,7 @@ func addAttributeViewBlock(avID, blockID, previousBlockID, addingBlockID string, if nil != view && 0 < len(view.Table.Filters) && !ignoreFillFilter { viewable, _ := renderAttributeViewTable(attrView, view, "") viewable.FilterRows(attrView) - viewable.SortRows() + viewable.SortRows(attrView) var nearRow *av.TableRow if 0 < len(viewable.Rows) { diff --git a/kernel/model/export.go b/kernel/model/export.go index b3cfb4dbc..eba5ee4ec 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -87,7 +87,7 @@ func ExportAv2CSV(avID, blockID string) (zipPath string, err error) { // 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474 table.FilterRows(attrView) - table.SortRows() + table.SortRows(attrView) exportFolder := filepath.Join(util.TempDir, "export", "csv", name) if err = os.MkdirAll(exportFolder, 0755); nil != err { @@ -2284,7 +2284,7 @@ func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros, keepFold bool, // 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474 table.FilterRows(attrView) - table.SortRows() + table.SortRows(attrView) var aligns []int for range table.Columns {