Code:
||コマンドボタンセル
{define-class public CustomCommandButtonCell {inherits StandardRecordGridCell}
field public button:CommandButton =
{CommandButton
width = 40pt,
active-traversal-container = null,
vorigin = 0.0,
horigin = 0.0,
label-inset = 2px,
test-visible? = false,
takes-focus? = false
}
{method public {on-key-press ev:KeyPress}:void
{super.on-key-press ev}
{switch ev.value
case KeyPressValue.enter, KeyPressValue.space do
{self.button.take-action}
}
}
field _do-action-proc:#{proc-type{r:#Record, rs:#RecordSet, cell:#RecordGridCell}:void} = null
{constructor public {default
label:String = "command-button",
do-action-proc:#{proc-type{r:#Record, rs:#RecordSet, cell:#RecordGridCell}:void} = null
}
{construct-super.StandardRecordGridCell}
{self.button.add-event-handler
{on Action do
{self.do-action}
}
}
set self._do-action-proc = do-action-proc
set self.button.label = label
set self.button.width = {make-elastic}
{self.add-internal replace? = true, self.button}
}
||ボタン押下時の処理
{method public {do-action}:void
def record-set =
{if-non-null self.record, rs = self.record.record-set then
rs
else
null
}
{self._do-action-proc self.record, record-set, self}
}
}
||コマンドボタンカラム
{define-class public CustomCommandButtonColumn {inherits RecordGridColumn}
{constructor public {default
column-resizable?:bool = false,
label:String = "command-button",
do-action-proc:#{proc-type{r:#Record, rs:#RecordSet, cell:#RecordGridCell}:void} = null,
...}
{construct-super
column-resizable? = column-resizable?,
{splice ...}}
set self.cell-spec =
{proc {col:RecordGridColumn}:RecordGridCell
def cell = {CustomCommandButtonCell
label = label,
do-action-proc = do-action-proc
}
{return cell}
}
}
}
||レコードの挿入と、インデックスの振りなおしを行う
{define-proc public {insert-record
record-set:RecordSet,
record:Record,
index:int,
index-field:String
}:void
{with record-set.batch-events? = true do
{for r in record-set do
def tmp-index = r[index-field] asa int
{if tmp-index >= index then
set r[index-field] = tmp-index + 1
}
}
set record[index-field] = index
{record-set.append record}
}
}
{def rs =
{RecordSet
{RecordFields
||ボタンのカラムのフィールド
{RecordField "RECORD-INDEX", domain = int, caption = "No."},
{RecordField "First", domain = String, caption = "苗字"},
{RecordField "Last", domain = String, caption = "名前"}
}
}
}
{def grid = {RecordGrid }}
{do
set grid.columns =
{{Array-of RecordGridColumn}
{RecordGridColumn "RECORD-INDEX", width = 10mm, halign = "right"},
||ボタンカラム
{CustomCommandButtonColumn
"btn",
label = "分割",
width = 15mm,
do-action-proc =
{proc {r:#Record, rs:#RecordSet, cell:#RecordGridCell}:void
||r:#Record ・・・ ボタン押下を行ったレコード
||rs:#RecordSet ・・・ 上記レコードを含むレコードセット
||cell:#RecordGridCell ・・・ ボタンが格納されているセル
||
{if-non-null r, rs then
{with rs.batch-events? = true do
||「分割」ボタンが欧化されたら、
||レコードを挿入する
def new-r = {rs.new-record}
set new-r["First"] = r["First"]
||引数で指定したレコードセットに、レコードを挿入する
||※挿入位置
{insert-record
rs, ||挿入対象のレコードセット
new-r, ||挿入するレコード
||挿入位置
||(ここでは「自分の行番号プラス1=自分の行の下」を指定)
r["RECORD-INDEX"] asa int + 1,
||挿入位置特定するための対象フィールド名
||グラフィックで、 RecordGrid.sort に指定したフィールド名と合わせてください。
"RECORD-INDEX"
}
}
}
}
},
{RecordGridColumn "First"},
{RecordGridColumn "Last"}
}
set grid.record-source = rs
set grid.sort = "RECORD-INDEX"
}
||初期表示レコードの追加
{with rs.batch-events? = true do
{for i:int = 1 to 10 do
def new-r = {rs.new-record}
||レコードの並び順を保持するために、インデックスを指定します
set new-r["RECORD-INDEX"] = i
set new-r["First"] = "ABC"
set new-r["Last"] = "DEF"
{rs.append new-r}
}
{rs.commit}
}
{value grid}