万水千山ABP - 弹出对话框禁用回车

模态对话框中禁用回车

 

ABP Zero 中,使用弹出对话框进行实体编辑,回车时会自动保存并关闭对话框。那么如何禁用这个回车功能 ?

 

查看实体列表视图 index.cshtml 所对应加载的脚本文件 index.js(如 Web\Areas\Mpa\Views\Editions\Index.js),有如下的定义

        var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + 'Mpa/Editions/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/Mpa/Views/Editions/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditEditionModal'
        });

这应该是定义生成或编辑模态对话框。(总是不记得那个参数 modalClass 的用处。其实这里的定义,要和 scriptUrl 定义的脚本文件里的函数保持一致。如上例中 _CreateOrEditModal.js 里的相关定义应是: app.modals.CreateOrEditEditionModal = function () { ... } ,这个函数定义了 init 和 save 方法,由 ModalManager 调用   )

通过在源码中搜索 ModalManager ,找到  Web\Common\Scripts\ModalManager.js 文件中关于 app.ModalManager 的定义

app.ModalManager = (function () {
......
})();

app.ModalManager 扩展定义了Bootstrap 的模态框插件,其中有一段代码:

                _$modal.find('.modal-body').keydown(function (e) {
                    if (e.which == 13) {
                        e.preventDefault();
                        _saveModal();
                    }
                });

很明显,这段代码捕获模态体中的按键事件,当按键是回车时,阻止默认绑定动作,并保存。

把这一段注释掉,或者根据需要做修改吧。

 

有关 bootstrap 模态框禁用回车的文章,提到需要在 modal-footer 的所有 button 上都加上 type="button" 属性。我们看一下 ABP Zero 是如何定义的

Web\Areas\Mpa\Views\Common\Modals\_ModalFooterWithSaveAndCancel.cshtml

<div class="modal-footer">
    <button type="button" class="btn default close-button" data-dismiss="modal">@L("Cancel")</button>
    <button type="button" class="btn blue save-button"><i class="fa fa-save"></i> <span>@L("Save")</span></button>
</div>

一点没错,button 上已经加过这个属性了。

 

posted @ 2017-05-26 13:45  forestk  阅读(1239)  评论(0编辑  收藏  举报