diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index e00a29e49..a2e1985c6 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -58,6 +58,7 @@ func filterRows(ret *av.AttributeView) { } } } + colIndexes = util.RemoveDuplicatedElem(colIndexes) var rows []*av.Row for _, row := range ret.Rows { @@ -92,6 +93,7 @@ func sortRows(ret *av.AttributeView) { } } } + colIndexes = util.RemoveDuplicatedElem(colIndexes) sort.Slice(ret.Rows, func(i, j int) bool { for _, index := range colIndexes { @@ -408,12 +410,12 @@ func sortAttributeViewColumn(columnID, previousColumnID, avID string) (err error break } } - attrView.Columns = util.InsertElement(attrView.Columns, previousIndex, col) + attrView.Columns = util.InsertElem(attrView.Columns, previousIndex, col) for _, row := range attrView.Rows { cel := row.Cells[index] row.Cells = append(row.Cells[:index], row.Cells[index+1:]...) - row.Cells = util.InsertElement(row.Cells, previousIndex, cel) + row.Cells = util.InsertElem(row.Cells, previousIndex, cel) } err = av.SaveAttributeView(attrView) @@ -446,7 +448,7 @@ func sortAttributeViewRow(rowID, previousRowID, avID string) (err error) { break } } - attrView.Rows = util.InsertElement(attrView.Rows, previousIndex, row) + attrView.Rows = util.InsertElem(attrView.Rows, previousIndex, row) err = av.SaveAttributeView(attrView) return diff --git a/kernel/util/misc.go b/kernel/util/misc.go index 310f7fc29..feb3f4fcf 100644 --- a/kernel/util/misc.go +++ b/kernel/util/misc.go @@ -24,9 +24,21 @@ import ( "github.com/88250/lute/html" ) -// InsertElement inserts a new element value at the specified index position. +// RemoveDuplicatedElem removes the duplicated elements from the slice. +func RemoveDuplicatedElem(slice []int) (ret []int) { + allKeys := make(map[int]bool) + for _, item := range slice { + if _, value := allKeys[item]; !value { + allKeys[item] = true + ret = append(ret, item) + } + } + return +} + +// InsertElem inserts a new element value at the specified index position. // 0 <= index <= len(a) -func InsertElement[T any](ret []T, index int, value T) []T { +func InsertElem[T any](ret []T, index int, value T) []T { if len(ret) == index { // nil or empty slice or after last element return append(ret, value) }