🎨 Add multi-select type column to Attribute View https://github.com/siyuan-note/siyuan/issues/8695

This commit is contained in:
Daniel 2023-07-10 23:35:24 +08:00
parent 0c6c80ffbd
commit 9578e55446
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 154 additions and 131 deletions

View file

@ -22,7 +22,6 @@ import (
"database/sql"
"os"
"path/filepath"
"sort"
"strings"
"github.com/88250/gulu"
@ -190,117 +189,3 @@ func RebuildAttributeViewTable(tx *sql.Tx, av *AttributeView) (err error) {
}
return
}
type AttributeViewSort struct {
Column string `json:"column"` // 列 ID
Order SortOrder `json:"order"` // 排序顺序
}
type SortOrder string
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
})
}
type AttributeViewFilter struct {
Column string `json:"column"`
Operator FilterOperator `json:"operator"`
Value *Value `json:"value"`
}
type FilterOperator string
const (
FilterOperatorIsEqual FilterOperator = "="
FilterOperatorIsNotEqual FilterOperator = "!="
FilterOperatorIsGreater FilterOperator = ">"
FilterOperatorIsGreaterOrEqual FilterOperator = ">="
FilterOperatorIsLess FilterOperator = "<"
FilterOperatorIsLessOrEqual FilterOperator = "<="
FilterOperatorContains FilterOperator = "Contains"
FilterOperatorDoesNotContain FilterOperator = "Does not contains"
FilterOperatorIsEmpty FilterOperator = "Is empty"
FilterOperatorIsNotEmpty FilterOperator = "Is not empty"
FilterOperatorStartsWith FilterOperator = "Starts with"
FilterOperatorEndsWith FilterOperator = "Ends with"
FilterOperatorIsBetween FilterOperator = "Is between"
FilterOperatorIsRelativeToToday FilterOperator = "Is relative to today"
)
func (av *AttributeView) FilterRows() {
if 1 > len(av.Filters) {
return
}
var colIndexes []int
for _, f := range av.Filters {
for i, c := range av.Columns {
if c.ID == f.Column {
colIndexes = append(colIndexes, i)
break
}
}
}
rows := []*Row{}
for _, row := range av.Rows {
pass := true
for j, index := range colIndexes {
c := av.Columns[index]
if c.Type == ColumnTypeBlock {
continue
}
if !row.Cells[index].Value.CompareOperator(av.Filters[j].Value, av.Filters[j].Operator) {
pass = false
break
}
}
if pass {
rows = append(rows, row)
}
}
av.Rows = rows
}

View file

@ -35,7 +35,6 @@ type Value struct {
Text *ValueText `json:"text,omitempty"`
Number *ValueNumber `json:"number,omitempty"`
Date *ValueDate `json:"date,omitempty"`
Select *ValueSelect `json:"select,omitempty"`
MSelect []*ValueSelect `json:"mSelect,omitempty"`
}
@ -78,9 +77,7 @@ func (value *Value) Compare(other *Value) int {
return 0
}
}
if nil != value.Select && nil != other.Select {
return strings.Compare(value.Select.Content, other.Select.Content)
}
if nil != value.MSelect && nil != other.MSelect {
var v1 string
for _, v := range value.MSelect {
@ -172,21 +169,12 @@ func (value *Value) CompareOperator(other *Value, operator FilterOperator) bool
}
}
if nil != value.Select && nil != other.Select {
if nil != value.MSelect && nil != other.MSelect && 0 < len(value.MSelect) && 0 < len(other.MSelect) {
switch operator {
case FilterOperatorIsEqual:
return value.Select.Content == other.Select.Content
return value.MSelect[0].Content == other.MSelect[0].Content
case FilterOperatorIsNotEqual:
return value.Select.Content != other.Select.Content
case FilterOperatorIsEmpty:
return "" == strings.TrimSpace(value.Select.Content)
case FilterOperatorIsNotEmpty:
return "" != strings.TrimSpace(value.Select.Content)
}
}
if nil != value.MSelect && nil != other.MSelect {
switch operator {
return value.MSelect[0].Content != other.MSelect[0].Content
case FilterOperatorContains:
for _, v := range value.MSelect {
if v.Content == other.MSelect[0].Content {

78
kernel/av/filter.go Normal file
View file

@ -0,0 +1,78 @@
// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package av
type AttributeViewFilter struct {
Column string `json:"column"`
Operator FilterOperator `json:"operator"`
Value *Value `json:"value"`
}
type FilterOperator string
const (
FilterOperatorIsEqual FilterOperator = "="
FilterOperatorIsNotEqual FilterOperator = "!="
FilterOperatorIsGreater FilterOperator = ">"
FilterOperatorIsGreaterOrEqual FilterOperator = ">="
FilterOperatorIsLess FilterOperator = "<"
FilterOperatorIsLessOrEqual FilterOperator = "<="
FilterOperatorContains FilterOperator = "Contains"
FilterOperatorDoesNotContain FilterOperator = "Does not contains"
FilterOperatorIsEmpty FilterOperator = "Is empty"
FilterOperatorIsNotEmpty FilterOperator = "Is not empty"
FilterOperatorStartsWith FilterOperator = "Starts with"
FilterOperatorEndsWith FilterOperator = "Ends with"
FilterOperatorIsBetween FilterOperator = "Is between"
FilterOperatorIsRelativeToToday FilterOperator = "Is relative to today"
)
func (av *AttributeView) FilterRows() {
if 1 > len(av.Filters) {
return
}
var colIndexes []int
for _, f := range av.Filters {
for i, c := range av.Columns {
if c.ID == f.Column {
colIndexes = append(colIndexes, i)
break
}
}
}
rows := []*Row{}
for _, row := range av.Rows {
pass := true
for j, index := range colIndexes {
c := av.Columns[index]
if c.Type == ColumnTypeBlock {
continue
}
if !row.Cells[index].Value.CompareOperator(av.Filters[j].Value, av.Filters[j].Operator) {
pass = false
break
}
}
if pass {
rows = append(rows, row)
}
}
av.Rows = rows
}

72
kernel/av/sort.go Normal file
View file

@ -0,0 +1,72 @@
// SiYuan - Refactor your thinking
// Copyright (c) 2020-present, b3log.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package av
import "sort"
type AttributeViewSort struct {
Column string `json:"column"` // 列 ID
Order SortOrder `json:"order"` // 排序顺序
}
type SortOrder string
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
})
}