Thread Rating:
  • 312 Vote(s) - 2.81 Average
  • 1
  • 2
  • 3
  • 4
  • 5
セルが表示されているレコードが正しく取得できない
11-25-2013, 06:25 PM,
#1
セルが表示されているレコードが正しく取得できない
あるセルに入力された金額の消費税額を、同じレコードの消費税費カラムのセルに表示したいです。

下記のコードでは、常に、「最初にフォーカスを入れたセルのレコード」の消費税フィールドに
値が設定されてしまいます。

なぜでしょうか。

Code:
{define-class public CustomCell {inherits StandardStringCell }
  {constructor public {default ...}
    {construct-super {splice ...}}
  }
  {method public {create-editor}:TextField
    def tf = {super.create-editor}
    {tf.add-event-handler
        {on ValueFinished do
            def yen = {String tf.value}
            {if-non-null r = self.record , col = self.column then
                set r["tax"] = {yen.to-int} * 0.05 & "円"
            }
        }
    }
    {return tf}
  }
}
{define-class public CustomColumn {inherits RecordGridColumn}
  {constructor public {default ...}
    {construct-super {splice ...}}
    set self.cell-spec = CustomCell
  }
}
{let rs:RecordSet =
    {RecordSet
        {RecordFields
            {RecordField "yen", domain = String, nullable? = true, caption = "価格"},
            {RecordField "tax", domain = String, nullable? = true, caption = "消費税"}
        },
        {RecordData}, {RecordData}, {RecordData}, {RecordData}
    }
}
{value
    {RecordGrid
        record-source = rs,
        {CustomColumn "yen"}
    }
}
11-25-2013, 06:34 PM, (This post was last modified: 11-25-2013, 06:41 PM by umemura.)
#2
RE: セルが表示されているレコードが正しく取得できない
create-editor メソッドでは、そのカラムで利用するテキストフィールドを生成しますが、
1セルにつき1テキストフィールドではなく、
1カラムにつき、1テキストフィールドが生成されているようで、
そのため、 self.record の値が、
「最初にテキストフィールドが生成されたセルのレコード」なってしまっているようでした。

オープンコントロールのソースを参考にして、get-grid-cell を利用することで、
正しく、編集したセルのレコードを取得できるようになりました。

Code:
{define-class public CustomCell {inherits StandardStringCell }
{constructor public {default ...}
{construct-super {splice ...}}
}
{method public {create-editor}:TextField
def tf = {super.create-editor}
{tf.add-event-handler
{on ValueFinished do
def yen = {String tf.value}
{if-non-null cell = {get-grid-cell tf } then
{if-non-null r = cell.record, col = self.column then
set r["tax"] = {yen.to-int} * 0.05 & "円"
}
}
}
}
{return tf}
}
}


11-25-2013, 06:40 PM, (This post was last modified: 11-25-2013, 06:41 PM by umemura.)
#3
RE: セルが表示されているレコードが正しく取得できない

先のソースでは、セルに値を入力後、returnキー等を押さずにフォーカスアウトすると、
セルの中の値が正しく反映されずに、消えてしまう、という問題がありました。

デバッグで create-editor で生成したTextField のvalueを確認したところ、
同じくcreate-editor内で追加した ValueFinished のイベントハンドラの前後で、
テキストフィールドの値が異なっていました。

オープンコントロールのソースを見る限り、
SRGTextField のコンストラクタ内でもValueFinished のイベントが追加されており、
その中で、変更前の値が、セルのattempt-update に投げられているようでした。

セルの値が変更されたら何かを行う、という処理は、
create-editor で生成したコントロールの値変更イベントで設定するのではなく、
StandardStringCell.attempt-update をオーバーライドしたほうが良いのかもしれません。

下記のソースであれば、セルの値を変更した直後にフォーカスアウトしても、
値が残るようなことはありませんでした。


Code:
{define-class public CustomCell {inherits StandardStringCell }
{constructor public {default ...}
{construct-super {splice ...}}
}
{method public {attempt-update val:any}:#void
{super.attempt-update val}
{if-non-null r = self.record , col = self.column then
def yen = {String r[col.field-name]}
set r["tax"] = {yen.to-int} * 0.05 & "円"
}
}
}
12-05-2013, 03:49 PM, (This post was last modified: 11-27-2015, 02:51 PM by shimonshoo.)
#4
RE: セルが表示されているレコードが正しく取得できない
I have been meaning to write something like this on my website and you have given me an idea. Your post will be rather good.

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('1011')