6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wicketでボタンを押したときにJavaScriptによる処理を挟む方法

Last updated at Posted at 2014-02-21

例えばAjaxButtonに対して、ユーザがボタンを押したときにconfirmで確認ダイアログを出したときだけサブミットを実行したい、というときは、次のようにすれば実現出来ます。
ポイントはupdateAjaxAttributesメソッドのオーバーライドです。

SomePage.java
final IndicatingAjaxButton button = new IndicatingAjaxButton("deleter") {

    @Override
    protected void updateAjaxAttributes(final AjaxRequestAttributes pAttributes) {
        super.updateAjaxAttributes(pAttributes);
        final AjaxCallListener ajaxCallListener = new AjaxCallListener();
        ajaxCallListener.onPrecondition("return confirm('この操作は取り消せません!本当に削除してよろしいですか?');");
        pAttributes.getAjaxCallListeners().add(ajaxCallListener);
    }

    @Override
    protected void onSubmit(final AjaxRequestTarget pTarget, final Form<?> pForm) {
        // サブミットしたときの処理
        ...
    }
};

確認ダイアログを出すくらいの単純な処理なら、上記のように直接JavaScriptの実行コードを書いてもいいですが、もっと複雑な処理を実行する必要がある場合、別途JavaScriptの関数を作っておいて、そちらを呼び出すJavaScriptコードを書けばよいでしょう。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?