ヘルプを見ると、 「既存のインデックスおよび選択したものを保存する」とありますね。
私の望む振る舞いをしてくれました。
ありがとうございます。
Code:
{curl 8.0 applet}
{curl-file-attributes character-encoding = "utf8"}
{applet manifest = "manifest.mcurl",
{compiler-directives careful? = true}
}
{let people:RecordSet =
{RecordSet
{RecordFields
{RecordField "index", caption = "No.", domain = int},
{RecordField "First", domain = String},
{RecordField "Last", domain = String},
{RecordField "Age", domain = int}
}
}
}
{for i:int = 0 to 10 do
{people.append {RecordData index = i + 1, Age = i + 1}}
}
{define-class public SortrderGrid {inherits RecordGrid}
{constructor public {default
record-source:#RecordSet = null,
...}
{construct-super {splice ...}}
set self.record-source = record-source
}
||グリッドのヘッダークリックでソートしたときには
||RecordSet からはこのイベントは発生しないため、
||継承したグリッドの sort セッターで無理やり発生させている
{setter public {sort sort:#RecordSort}:void
{if-non-null rs = self.record-source then
{rs.handle-event {RecordsReordered}}
}
set super.sort = sort
}
{setter public {record-source rs:#RecordSet}:void
set super.record-source = rs
{if-non-null rs = self.record-source then
{rs.add-event-handler
{on e:RecordsChanged at rs:RecordSet do
{type-switch e
case ra:RecordAdded do
def insert-index = self.current-index + 1
{with rs.batch-events? = true do
{for idx:int = rs.size above 0 do
{if insert-index <= idx then
{if-non-null select-record =
{rs.select-one
filter = {RecordFilter
{proc {r:Record}:bool
{if r["index"] asa int == idx then
{return true}
else
{return false}
}
}
}
} then
set select-record["index"] = idx + 1
}
}
}
set ra.record["index"] = insert-index
{if-non-null sort = self.sort then
set super.sort = {RecordSort.concat sort, {RecordSort.from-string "index"}}
}
}
{return}
case ra:RecordModified do
case rr:RecordRemoved do
case ro:RecordsReordered do
case rbc:RecordsBulkChanges do
else
{return}
}
{after 0s do
{with
self.records.batch-events? = true ,
rs.batch-events? = true do
{for r key idx:int in self.records do
set r["index"] = idx + 1
}
}
}
{return}
}
}
{rs.handle-event {RecordsReordered }}
}
}
}
{def grid =
{SortrderGrid
||レコードの選択行が移動してしまう問題の対応
key-spec = RecordSetDisplay.preserve-indices,
record-source = people,
height = 5in,
width = 5in
}
}
{CommandButton
label = "挿入",
{on Action do
{if-non-null rs = grid.record-source then
{rs.append {RecordData}}
}
}
}
{CommandButton
label = "削除",
{on Action do
{if-non-null r = grid.current-record then
{r.delete}
}
}
}
{value grid}