Added "allow-selection" property to TableView

This commit is contained in:
Alexei Anoshenko 2022-01-14 18:06:10 -05:00
parent 1a4040bd00
commit 32b6182dbf
4 changed files with 125 additions and 4 deletions

View File

@ -1402,7 +1402,7 @@ function tableViewBlurEvent(element, event) {
function setTableCellCursor(element, row, column) {
const cellID = element.id + "-" + row + "-" + column;
var cell = document.getElementById(cellID);
if (!cell) {
if (!cell || cell.getAttribute("data-disabled")) {
return false;
}
@ -1573,7 +1573,7 @@ function tableViewCellKeyDownEvent(element, event) {
function setTableRowCursor(element, row) {
const tableRowID = element.id + "-" + row;
var tableRow = document.getElementById(tableRowID);
if (!tableRow) {
if (!tableRow || tableRow.getAttribute("data-disabled")) {
return false;
}

View File

@ -34,6 +34,8 @@ GridLayout {
Checkbox { row = 7, column = 0:1, id = tableColumnStyle, content = "Column style" },
TextView { row = 8, text = "Selection mode" },
DropDownList { row = 8, column = 1, id = tableSelectionMode, current = 0, items = ["none", "cell", "row"]},
Checkbox { row = 9, column = 0:1, id = tableDisableHead, content = "Disable head selection" },
Checkbox { row = 10, column = 0:1, id = tableDisableFoot, content = "Disable foot selection" },
]
}
]
@ -42,6 +44,31 @@ GridLayout {
}
`
type demoTableAllowSelection struct {
index []int
}
func (allow *demoTableAllowSelection) AllowCellSelection(row, column int) bool {
return allow.AllowRowSelection(row)
}
func (allow *demoTableAllowSelection) AllowRowSelection(row int) bool {
if allow.index != nil {
for _, index := range allow.index {
if index == row {
return false
}
}
}
return true
}
func newDemoTableAllowSelection(index []int) *demoTableAllowSelection {
result := new(demoTableAllowSelection)
result.index = index
return result
}
func createTableViewDemo(session rui.Session) rui.View {
view := rui.CreateViewFromText(session, tableViewDemoText)
if view == nil {
@ -97,6 +124,45 @@ func createTableViewDemo(session rui.Session) rui.View {
rui.Set(view, "tableSelectionMode", rui.DropDownEvent, func(list rui.DropDownList, number int) {
rui.Set(view, "demoTableView1", rui.SelectionMode, number)
switch rui.GetCurrent(view, "tableSelectionMode") {
case rui.CellSelection:
// TODO
case rui.RowSelection:
// TODO
}
})
rui.Set(view, "tableDisableHead", rui.CheckboxChangedEvent, func(checked bool) {
if checked {
if rui.IsCheckboxChecked(view, "tableDisableFoot") {
rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1, 11}))
} else {
rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1}))
}
} else {
if rui.IsCheckboxChecked(view, "tableDisableFoot") {
rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{11}))
} else {
rui.Set(view, "demoTableView1", rui.AllowSelection, nil)
}
}
})
rui.Set(view, "tableDisableFoot", rui.CheckboxChangedEvent, func(checked bool) {
if checked {
if rui.IsCheckboxChecked(view, "tableDisableHead") {
rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1, 11}))
} else {
rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{11}))
}
} else {
if rui.IsCheckboxChecked(view, "tableDisableHead") {
rui.Set(view, "demoTableView1", rui.AllowSelection, newDemoTableAllowSelection([]int{0, 1}))
} else {
rui.Set(view, "demoTableView1", rui.AllowSelection, nil)
}
}
})
rui.Set(view, "tableCellGap", rui.DropDownEvent, func(list rui.DropDownList, number int) {

View File

@ -18,6 +18,14 @@ type TableCellStyle interface {
CellStyle(row, column int) Params
}
type TableAllowCellSelection interface {
AllowCellSelection(row, column int) bool
}
type TableAllowRowSelection interface {
AllowRowSelection(row int) bool
}
type SimpleTableAdapter interface {
TableAdapter
TableCellStyle

View File

@ -196,6 +196,12 @@ const (
// The main listener format: func(TableView, int), where the second argument is the row number.
TableRowSelectedEvent = "table-row-selected"
// AllowSelection is the constant for the "allow-selection" property tag.
// The "allow-selection" property sets the adapter which specifies styles of each table row.
// This property can be assigned or by an implementation of TableAllowCellSelection
// or TableAllowRowSelection interface.
AllowSelection = "allow-selection"
// NoneSelection is the value of "selection-mode" property: the selection is forbidden.
NoneSelection = 0
// CellSelection is the value of "selection-mode" property: the selection of a single cell only is enabled.
@ -293,7 +299,7 @@ func (table *tableViewData) remove(tag string) {
table.propertyChanged(tag)
case Gap, CellBorder, CellPadding, RowStyle, ColumnStyle, CellStyle,
HeadHeight, HeadStyle, FootHeight, FootStyle:
HeadHeight, HeadStyle, FootHeight, FootStyle, AllowSelection:
if _, ok := table.properties[tag]; ok {
delete(table.properties, tag)
table.propertyChanged(tag)
@ -497,6 +503,19 @@ func (table *tableViewData) set(tag string, value interface{}) bool {
return false
}
case AllowSelection:
switch value.(type) {
case TableAllowCellSelection:
table.properties[tag] = value
case TableAllowRowSelection:
table.properties[tag] = value
default:
notCompatibleType(tag, value)
return false
}
case Current:
switch value := value.(type) {
case int:
@ -559,7 +578,8 @@ func (table *tableViewData) propertyChanged(tag string) {
case Content, RowStyle, ColumnStyle, CellStyle, CellPadding, CellBorder,
HeadHeight, HeadStyle, FootHeight, FootStyle,
CellPaddingTop, CellPaddingRight, CellPaddingBottom, CellPaddingLeft,
TableCellClickedEvent, TableCellSelectedEvent, TableRowClickedEvent, TableRowSelectedEvent:
TableCellClickedEvent, TableCellSelectedEvent, TableRowClickedEvent,
TableRowSelectedEvent, AllowSelection:
table.ReloadTableData()
case Gap:
@ -861,6 +881,26 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) {
ignorCells := []struct{ row, column int }{}
selectionMode := GetSelectionMode(table, "")
var allowCellSelection TableAllowCellSelection = nil
if allow, ok := adapter.(TableAllowCellSelection); ok {
allowCellSelection = allow
}
if value := table.getRaw(AllowSelection); value != nil {
if style, ok := value.(TableAllowCellSelection); ok {
allowCellSelection = style
}
}
var allowRowSelection TableAllowRowSelection = nil
if allow, ok := adapter.(TableAllowRowSelection); ok {
allowRowSelection = allow
}
if value := table.getRaw(AllowSelection); value != nil {
if style, ok := value.(TableAllowRowSelection); ok {
allowRowSelection = style
}
}
tableCSS := func(startRow, endRow int, cellTag string, cellBorder BorderProperty, cellPadding BoundsProperty) {
for row := startRow; row < endRow; row++ {
@ -891,6 +931,10 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) {
}
buffer.WriteString(` onclick="tableRowClickEvent(this, event)"`)
if allowRowSelection != nil && !allowRowSelection.AllowRowSelection(row) {
buffer.WriteString(` data-disabled="1"`)
}
}
if cssBuilder.buffer.Len() > 0 {
@ -982,6 +1026,9 @@ func (table *tableViewData) htmlSubviews(self View, buffer *strings.Builder) {
if selectionMode == CellSelection {
buffer.WriteString(` onclick="tableCellClickEvent(this, event)"`)
if allowCellSelection != nil && !allowCellSelection.AllowCellSelection(row, column) {
buffer.WriteString(` data-disabled="1"`)
}
}
if columnSpan > 1 {