Thread Rating:
  • 362 Vote(s) - 2.88 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Record の選択について
10-21-2011, 03:51 PM,
#1
Record の選択について
RecordGrid のレコードをクリックすると、そのレコードを選択し、
画面内のフィールドなどに値を反映させたいと思います。


cells-take-focus? = false、 select-current-record? = false に設定して
RecordGrid のSelectionChanged を使うと簡単に実装できるのですが、
RecordSetの中身を入れ替える(サーバーから再検索した結果に入れ替えるなど)場合、
レコード追加後にレコード選択のイベントが走るため、
一番上のレコードの値が反映先に設定されてしまい、
ユーザからすると、意識的にレコードを選択していないのに、値が設定されてしまうことになります。


レコードの選択はあくまでもユーザの操作でさせたい場合、どんな記述の仕方があるでしょうか。

とりあえず下記のような書き方で実装できますが、
RecordGridで簡潔せず、よろしくない気がしています。

Code:
{let rs:RecordSet =
    {RecordSet
        {RecordFields
            {RecordField "First", domain = String},
            {RecordField "Last", domain = String},
            {RecordField "Age", domain = int}
        }
    }
}
{let rg:RecordGrid =
    {RecordGrid
        ui-object =
            {StandardRecordGridUI
                ||レコードの選択を有効化する処理
                {on FocusIn at ui:RecordGridUI do
                    set ui.grid.record-selection-enabled? = true
                    set ui.grid.select-current-record? = true
                    {if ui.grid.current-index >= 0 then
                        {ui.grid.select-record ui.grid.current-index}
                    }
                },
                {on e:PointerEnvelopeEvent at ui:RecordGridUI do
                    {type-switch e.contents
                     case pp:PointerPress do
                        set ui.grid.record-selection-enabled? = true
                        set ui.grid.select-current-record? = true
                        {if ui.grid.current-index >= 0 then
                            {ui.grid.select-record ui.grid.current-index}
                        }
                     case pr:PointerRelease do
                        {pr.consume}
                        {if ui.grid.current-index >= 0 then
                            {ui.grid.select-record ui.grid.current-index}
                        }
                     case pn:PointerNonevent do
                        {pn.consume}
                        {if ui.grid.current-index >= 0 then
                            {ui.grid.select-record ui.grid.current-index}
                        }
                    }
                }
            }
        ,
        cells-take-focus? = false,
        select-current-record? = false,
        editable? = false,
        record-source = rs
    }
}
{let cb:CommandButton =
    {CommandButton
    label= "検索",
        {on Action do
            
            ||レコードの選択を無効にする
            set rg.cells-take-focus? = false
            set rg.select-current-record? = false
            
            {rs.delete-all}
            {rs.append
                {RecordData First = "John", Last = "Smith", Age = 25}}
            {rs.append
                {RecordData First = "Jane", Last = "Smith", Age = 29}}
            {rs.append
                {RecordData First = "Jane", Last = "Jones", Age = 28}}
        }
    }
}
{let tf:TextField = {TextField }}
{do
    {rg.add-event-handler
        {on sc:SelectionChanged at rg:RecordGrid do
            {if-non-null r = rg.current-record then
                set tf.value  =
                    {r.get "First"} asa String
            }
        }
    }
}
{VBox rg, tf, cb}
10-24-2011, 02:33 PM,
#2
RE: Record の選択について
key-spec設定されていない様です。
11-01-2011, 02:58 PM,
#3
RE: Record の選択について
key-press はちょっと要望の機能とは違いました。
再検索時に、同じレコードが返ってくる場合があって、
検索前の選択状態を復元させたい場合には使えそうですね。


とりあえず、after句を使ったコードも書いてみました。
ただ、イベントの順番とかを考えると、after句を使うのはなるべく抑えたい気も・・・。

Code:
{let rs:RecordSet =
    {RecordSet
        {RecordFields
            {RecordField "id", domain = int},
            {RecordField "First", domain = String},
            {RecordField "Last", domain = String},
            {RecordField "Age", domain = int}
        }
    }
}
{let rg:RecordGrid =
    {RecordGrid
        cells-take-focus? = false,
        select-current-record? = true,
        editable? = false,
        record-source = rs
    }
}
{let cb:CommandButton =
    {CommandButton
        label= "検索",
        {on Action do
            set rg.select-current-record? = false
            {rs.delete-all}
            {rs.append
                {RecordData id = 1, First = "John", Last = "Smith", Age = 25}}
            {rs.append
                {RecordData  id = 2, First = "Jane", Last = "Smith", Age = 29}}
            {rs.append
                {RecordData  id = 3, First = "Jane", Last = "Jones", Age = 28}}
            
            {after 0s do
                set rg.select-current-record? = true
                {rg.select-nothing}
            }
        }
    }
}
{let tf:TextField = {TextField }}
{do
    {rg.add-event-handler
        {on sc:SelectionChanged at rg:RecordGrid do
                {if-non-null r = rg.current-record then
                    set tf.value  =
                        {r.get "First"} asa String
                }
        }
    }
}
{VBox rg, tf, cb}


Possibly Related Threads...
Thread Author Replies Views Last Post
  Modify Record benzamil 2 3,310 04-03-2017, 01:58 PM
Last Post: benzamil
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('318')