🎨 Add database table view checkbox column type https://github.com/siyuan-note/siyuan/issues/9667

This commit is contained in:
Daniel 2023-11-17 09:03:17 +08:00
parent 16727cd8af
commit 0359f96c64
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 90 additions and 2 deletions

View file

@ -72,6 +72,10 @@ const (
CalcOperatorRange CalcOperator = "Range"
CalcOperatorEarliest CalcOperator = "Earliest"
CalcOperatorLatest CalcOperator = "Latest"
CalcOperatorChecked CalcOperator = "Checked"
CalcOperatorUnchecked CalcOperator = "Unchecked"
CalcOperatorPercentChecked CalcOperator = "Percent checked"
CalcOperatorPercentUnchecked CalcOperator = "Percent unchecked"
)
func (value *Value) Compare(other *Value) int {
@ -171,6 +175,16 @@ func (value *Value) Compare(other *Value) int {
if nil != value.Template && nil != other.Template {
return strings.Compare(value.Template.Content, other.Template.Content)
}
case KeyTypeCheckbox:
if nil != value.Checkbox && nil != other.Checkbox {
if value.Checkbox.Checked && !other.Checkbox.Checked {
return 1
}
if !value.Checkbox.Checked && other.Checkbox.Checked {
return -1
}
return 0
}
}
return 0
}
@ -543,6 +557,15 @@ func (value *Value) CompareOperator(other *Value, operator FilterOperator) bool
return "" != strings.TrimSpace(value.Template.Content)
}
}
if nil != value.Checkbox {
switch operator {
case FilterOperatorIsTrue:
return value.Checkbox.Checked
case FilterOperatorIsFalse:
return !value.Checkbox.Checked
}
}
return true
}
@ -731,6 +754,8 @@ func (table *Table) CalcCols() {
table.calcColCreated(col, i)
case KeyTypeUpdated:
table.calcColUpdated(col, i)
case KeyTypeCheckbox:
table.calcColCheckbox(col, i)
}
}
}
@ -1840,3 +1865,46 @@ func (table *Table) calcColUpdated(col *TableColumn, colIndex int) {
}
}
}
func (table *Table) calcColCheckbox(col *TableColumn, colIndex int) {
switch col.Calc.Operator {
case CalcOperatorCountAll:
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
case CalcOperatorChecked:
countChecked := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && row.Cells[colIndex].Value.Checkbox.Checked {
countChecked++
}
}
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countChecked), NumberFormatNone)}
case CalcOperatorUnchecked:
countUnchecked := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && !row.Cells[colIndex].Value.Checkbox.Checked {
countUnchecked++
}
}
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUnchecked), NumberFormatNone)}
case CalcOperatorPercentChecked:
countChecked := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && row.Cells[colIndex].Value.Checkbox.Checked {
countChecked++
}
}
if 0 < len(table.Rows) {
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countChecked)/float64(len(table.Rows)), NumberFormatPercent)}
}
case CalcOperatorPercentUnchecked:
countUnchecked := 0
for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && !row.Cells[colIndex].Value.Checkbox.Checked {
countUnchecked++
}
}
if 0 < len(table.Rows) {
col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUnchecked)/float64(len(table.Rows)), NumberFormatPercent)}
}
}
}