Skip to content

Commit

Permalink
🎨 Add database table view checkbox column type #9667
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 17, 2023
1 parent 16727cd commit 0359f96
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
14 changes: 14 additions & 0 deletions kernel/av/av.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const (
KeyTypeTemplate KeyType = "template"
KeyTypeCreated KeyType = "created"
KeyTypeUpdated KeyType = "updated"
KeyTypeCheckbox KeyType = "checkbox"
)

// Key 描述了属性视图属性列的基础结构。
Expand Down Expand Up @@ -157,6 +158,7 @@ type Value struct {
Template *ValueTemplate `json:"template,omitempty"`
Created *ValueCreated `json:"created,omitempty"`
Updated *ValueUpdated `json:"updated,omitempty"`
Checkbox *ValueCheckbox `json:"checkbox,omitempty"`
}

func (value *Value) String() string {
Expand Down Expand Up @@ -234,6 +236,14 @@ func (value *Value) String() string {
return ""
}
return value.Updated.FormattedContent
case KeyTypeCheckbox:
if nil == value.Checkbox {
return ""
}
if value.Checkbox.Checked {
return "√"
}
return ""
default:
return ""
}
Expand Down Expand Up @@ -529,6 +539,10 @@ func NewFormattedValueUpdated(content, content2 int64, format UpdatedFormat) (re
return
}

type ValueCheckbox struct {
Checked bool `json:"checked"`
}

// View 描述了视图的结构。
type View struct {
ID string `json:"id"` // 视图 ID
Expand Down
2 changes: 2 additions & 0 deletions kernel/av/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ const (
FilterOperatorEndsWith FilterOperator = "Ends with"
FilterOperatorIsBetween FilterOperator = "Is between"
FilterOperatorIsRelativeToToday FilterOperator = "Is relative to today"
FilterOperatorIsTrue FilterOperator = "Is true"
FilterOperatorIsFalse FilterOperator = "Is false"
)
68 changes: 68 additions & 0 deletions kernel/av/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -731,6 +754,8 @@ func (table *Table) CalcCols() {
table.calcColCreated(col, i)
case KeyTypeUpdated:
table.calcColUpdated(col, i)
case KeyTypeCheckbox:
table.calcColCheckbox(col, i)
}
}
}
Expand Down Expand Up @@ -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)}
}
}
}
4 changes: 2 additions & 2 deletions kernel/model/attribute_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ func addAttributeViewColumn(operation *Operation) (err error) {

keyType := av.KeyType(operation.Typ)
switch keyType {
case av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated:
case av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated, av.KeyTypeCheckbox:
var icon string
if nil != operation.Data {
icon = operation.Data.(string)
Expand Down Expand Up @@ -1218,7 +1218,7 @@ func updateAttributeViewColumn(operation *Operation) (err error) {

colType := av.KeyType(operation.Typ)
switch colType {
case av.KeyTypeBlock, av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated:
case av.KeyTypeBlock, av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail, av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated, av.KeyTypeCheckbox:
for _, keyValues := range attrView.KeyValues {
if keyValues.Key.ID == operation.ID {
keyValues.Key.Name = operation.Name
Expand Down
4 changes: 4 additions & 0 deletions kernel/treenode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,10 @@ func FillAttributeViewTableCellNilValue(tableCell *av.TableCell, rowID, colID st
if nil == tableCell.Value.Updated {
tableCell.Value.Updated = &av.ValueUpdated{}
}
case av.KeyTypeCheckbox:
if nil == tableCell.Value.Checkbox {
tableCell.Value.Checkbox = &av.ValueCheckbox{}
}
}
}

Expand Down

0 comments on commit 0359f96

Please sign in to comment.