Thread Rating:
  • 401 Vote(s) - 2.85 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Worksheetクラスの選択した(複数)行のデータを別のWorksheetクラスに移動する
02-15-2012, 06:40 PM,
#1
Worksheetクラスの選択した(複数)行のデータを別のWorksheetクラスに移動する
次のコードにて、Worksheetクラスの選択した(複数)行のレコードを、
別のWorksheetクラスに移動させたい場合はどのように行えばよいのでしょうか?
>ボタンおよび<ボタンで双方に移動できる処理をCommandButtonのActionイベントで行えばよいかと
思うのですが、記述方法がわかりません。

Code:
{import * from COM.CURL.EXT.WORKSHEET}

{def id-field = "id"}
{def id-name = "name"}
{def id-value = "value"}
{def item-rs1 =
    {RecordSet
        {RecordFields
            {RecordField id-field, domain = int},
            {RecordField id-name, domain = String},
            {RecordField id-value, domain = DataSource.double-domain}
        },
        {RecordData id = 1, name = "A", value = "1.0"},
        {RecordData id = 2, name = "B", value = "2.0"}
    }
}
{def item-rs2 =
    {RecordSet
        {RecordFields
            {RecordField id-field, domain = int},
            {RecordField id-name, domain = String},
            {RecordField id-value, domain = DataSource.double-domain}
        }
    }
}
{def item-ds1 = {RecordSetDataSource item-rs1}}
{def item-ds2 = {RecordSetDataSource item-rs2}}
{def ws1 =
    {Worksheet 4, 3,
        {widths 20pt,50pt,50pt},
        row = 0, col = 0,
        {EmbeddedRecordForm item-ds1, id-field = id-field,
            row = 0, col = 0,
            {FieldSpec id-field},
            {FieldSpec editable? = true, id-name},
            {FieldSpec editable? = true, id-value}
        }
    }
}
{def ws2 =
    {Worksheet 4, 3,
        {widths 20pt,50pt,50pt},
        row = 0, col = 0,
        {EmbeddedRecordForm item-ds2, id-field = id-field,
            row = 0, col = 0,
            {FieldSpec id-field},
            {FieldSpec editable? = true, id-name},
            {FieldSpec editable? = true, id-value}
        }
    }
}
{HBox
    valign="center",
    ws1,
    {VBox
        width = 50pt,
        height = 80pt,
        halign="center",
        {CommandButton
            label = ">",
            width = 20pt,
            height = 20pt,
            {on Action at cb:CommandButton do
                || ws2移動処理
            }
        },
        {CommandButton
            label = "<",
            width = 20pt,
            height = 20pt,
            {on Action at cb:CommandButton do
                || ws1移動処理
            }
        }
    },
    ws2
}
05-09-2013, 01:44 PM,
#2
RE: Worksheetクラスの選択した(複数)行のデータを別のWorksheetクラスに移動する
Worksheetに埋め込まれているのがRecordFormでしたので、双方のWorksheet から
RecordSetを取り出して、選択行のレコードを移動(追加/削除)する方法を考えてみましたが
こんな感じでどうでしょうか?

選択されたレコードを特定するのにワークシートに表示されている"id"列から取得した値を
使っているのがいまいちな気もしますが・・・

(※コメントが付いている付近が追加した部分です)
Code:
{curl 8.0 applet}
{curl-file-attributes character-encoding = "utf8"}
{applet manifest = "manifest.mcurl",
    {compiler-directives careful? = true}
}

{import * from COM.CURL.EXT.WORKSHEET}
{import * from CURL.LANGUAGE.REFLECTION}

{def id-field = "id"}
{def id-name = "name"}
{def id-value = "value"}
{def item-rs1 =
    {RecordSet
        {RecordFields
            {RecordField id-field, domain = int},
            {RecordField id-name, domain = String},
            {RecordField id-value, domain = DataSource.double-domain}
        },
        {RecordData id = 1, name = "A", value = "1.0"},
        {RecordData id = 2, name = "B", value = "2.0"}
    }
}
{def item-rs2 =
    {RecordSet
        {RecordFields
            {RecordField id-field, domain = int},
            {RecordField id-name, domain = String},
            {RecordField id-value, domain = DataSource.double-domain}
        }
    }
}
{def item-ds1 = {RecordSetDataSource item-rs1}}
{def item-ds2 = {RecordSetDataSource item-rs2}}
{def ws1 =
    {Worksheet 4, 3,
        {widths 20pt,50pt,50pt},
        row = 0, col = 0,
        {EmbeddedRecordForm
            item-ds1,
            id-field = id-field,
            row = 0, col = 0,
            {FieldSpec id-field},
            {FieldSpec editable? = true, id-name},
            {FieldSpec editable? = true, id-value}
        }
    }
}
{def ws2 =
    {Worksheet 4, 3,
        {widths 20pt,50pt,50pt},
        row = 0, col = 0,
        {EmbeddedRecordForm
            item-ds2,
            id-field = id-field,
            row = 0, col = 0,
            {FieldSpec id-field},
            {FieldSpec editable? = true, id-name},
            {FieldSpec editable? = true, id-value}
        }
    }
}

|| レコードセットから"id"フィールドの値をキーにしてレコードを取得するプロシージャ
{define-proc {find-record-by-id rs:RecordSet, id:int}:#Record
    {return
        {rs.select-one
            filter = {RecordFilter
                         {proc {rec:Record}:bool
                             {if {rec.get id-field} == id then
                                 {return true}
                              else
                                 {return false}
                             }
                         }
                     }
        }
    }
}

|| 選択行を他のワークシートへ移動するプロシージャ
{define-proc {move-selected-rows ws-src:Worksheet, ws-dst:Worksheet}:void
    || 移動元ワークシートの選択範囲を取得
    def sel:WorksheetSelection = ws-src.selection
    || 行選択されていない場合はなにもせずに終了
    {if not sel.all-cols? then
        {return}
    }
    || ワークシートから埋め込まれているEmbeddedRecordFormを取得
    || (※どのセルも同一のEmbeddedRecordFormが埋め込まれている前提で左上端のセルから取得)
    def form-src:#EmbeddedRecordForm = {ws-src.model.get-embedded-form 0, 0}
    def form-dst:#EmbeddedRecordForm = {ws-dst.model.get-embedded-form 0, 0}
    || EmbeddedRecordFormからレコードセットを取得
    def rs-src:RecordSet = form-src.record-source
    def rs-dst:RecordSet = form-dst.record-source
    ||ID列はフォームの先頭にある前提
    def id-colum-pos = 0
    || 選択行に該当するレコードを移動
    {for row:int = sel.begin-row to sel.end-row do
        || 選択行の"id"列の値を取得
        def id:any = {ws-src.get-value row, id-colum-pos}
        {if-non-null id then
            || 選択行のIDより移動元のレコードを取得
            def src-rec:#Record = {find-record-by-id rs-src, (id asa int)}
            {if-non-null src-rec then
                || 移動先レコードセットの新規レコードを作成
                def new-rec:Record = {rs-dst.new-record}
                || 移動元のレコードの全フィールドを新規レコードへコピー
                {for rf:RecordField in {src-rec.record-set.fields.to-Iterator} do
                    def field-name = rf.name
                    def field-val = {src-rec.get field-name}
                    {new-rec.set field-name, field-val}
                }
                || 移動先レコードセットへ新規レコードを追加
                {rs-dst.append new-rec}

                || 移動元ワークシートのレコードセットからレコードを削除
                {src-rec.delete}
            }
        }
    }
    || 移動元、移動先のレコードセットの変更点をコミット
    {rs-dst.commit}
    {rs-src.commit}
    || 移動元のワークシートをなにも選択されていない状態に設定
    {ws-src.select-nothing}
}


{HBox
    valign="center",
    ws1,
    {VBox
        width = 50pt,
        height = 80pt,
        halign="center",
        {CommandButton
            label = ">",
            width = 20pt,
            height = 20pt,
            {on Action at cb:CommandButton do
                || ws2移動処理
                {move-selected-rows ws1, ws2}
            }
        },
        {CommandButton
            label = "<",
            width = 20pt,
            height = 20pt,
            {on Action at cb:CommandButton do
                || ws1移動処理
                {move-selected-rows ws2, ws1}
            }
        }
    },
    ws2
}

Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('411')