mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-28 03:06:10 +01:00
♻️ Refactor av data structure
This commit is contained in:
parent
2f99364071
commit
bd95fdc5d7
8 changed files with 724 additions and 582 deletions
|
|
@ -16,9 +16,11 @@
|
|||
|
||||
package av
|
||||
|
||||
import "sort"
|
||||
type Sortable interface {
|
||||
SortRows()
|
||||
}
|
||||
|
||||
type AttributeViewSort struct {
|
||||
type ViewSort struct {
|
||||
Column string `json:"column"` // 列 ID
|
||||
Order SortOrder `json:"order"` // 排序顺序
|
||||
}
|
||||
|
|
@ -29,44 +31,3 @@ const (
|
|||
SortOrderAsc SortOrder = "ASC"
|
||||
SortOrderDesc SortOrder = "DESC"
|
||||
)
|
||||
|
||||
func (av *AttributeView) SortRows() {
|
||||
if 1 > len(av.Sorts) {
|
||||
return
|
||||
}
|
||||
|
||||
type ColIndexSort struct {
|
||||
Index int
|
||||
Order SortOrder
|
||||
}
|
||||
|
||||
var colIndexSorts []*ColIndexSort
|
||||
for _, s := range av.Sorts {
|
||||
for i, c := range av.Columns {
|
||||
if c.ID == s.Column {
|
||||
colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(av.Rows, func(i, j int) bool {
|
||||
for _, colIndexSort := range colIndexSorts {
|
||||
c := av.Columns[colIndexSort.Index]
|
||||
if c.Type == ColumnTypeBlock {
|
||||
continue
|
||||
}
|
||||
|
||||
result := av.Rows[i].Cells[colIndexSort.Index].Value.Compare(av.Rows[j].Cells[colIndexSort.Index].Value)
|
||||
if 0 == result {
|
||||
continue
|
||||
}
|
||||
|
||||
if colIndexSort.Order == SortOrderAsc {
|
||||
return 0 > result
|
||||
}
|
||||
return 0 < result
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue