【问题标题】:Angularjs – Warn on unsaved changes in Master Detail ScenarioAngularjs - 警告主细节场景中未保存的更改
【发布时间】:2017-01-01 02:41:38
【问题描述】:

我需要一个解决方案来警告主从方案中未保存的更改。我能够实施 Sam 在 2015 年 9 月创建的解决方案 (http://www.samjhill.com/blog/?p=525#comment-28558)。这是对链接 Detect Unsaved changes and alert user using angularjs 上的 stackoverflow 问题和答案的回应。

Sam 提供的解决方案适用于 html 文件中的单个表单,但我遇到的问题是,我在标题部分和详细信息部分有多个表单,两者都可以更改。在详细信息部分中,我使用的是 xeditable 表。我尝试将“退出时确认”属性添加到两种表单中。在顶部,确认框按预期出现,但即使在我提交更新后仍会继续出现。在 xeditable 部分,确认框永远不会出现。我在下面包含了我的 html。您能否提供我可以提出的任何建议,以便在页面上的任何表单发生更改时出现警告?

.container.col-md-12.col-sm-12.col-xs-12-center
    .well
        form.form-horizontal(name="createQuestionAnswer" confirm-on-exit)
            fieldset
                legend Question Definition
                .form-group
                    label.col-md-2.control-label(for="statement") Question
                    .col-md-10
                        input.form-control(name="statement", type="text", placeholder="Statement", ng-model="statement", ng-change = "changeStatement()",required)
                .form-group
                    label.col-md-2.control-label(for="sequenceNo") SequenceNo
                    .col-md-4
                        input.form-control(name="sequenceNo", type="text", placeholder="SequenceNo", ng-model="sequenceNo", ng-change = "changeSequenceNo()",required)
                    label.col-md-2.control-label(for="componentType") Type
                    .col-md-4
                        input.form-control(name="componentType", type="text", placeholder="ComponentType", ng-model="componentType", ng-change = "changeComponentType()",required)
                .form-group
                    label.col-md-2.control-label(for="weight") Weight
                    .col-md-2
                        input.form-control(name="weight", type="text", placeholder="Weight", ng-model="weight", ng-change = "changeWeight()")
                    label.col-md-2.control-label(for="imageURL") Image
                    .col-md-6
                        input.form-control(name="imageURL", type="text", placeholder="Image", ng-model="imageURL", ng-change = "changeImageURL()", required)
    div(ng-app='app')
            table.table.table-bordered.table-hover.table-condensed(style='width: 100%')
                tr(style='font-weight: bold')
                    td(style='width:70%') Answer
                    td(style='width:10%') Correct
                    td(style='width:10%') Rank
                    td(style='width:10%') Position
                tr(ng-repeat='answer in answers')
                    td
                        // editable answer (text with validation)
                        span(editable-text='answer.answer',  e-name='answer', e-form='rowform', onbeforesave='checkAnswer($data)',onaftersave='saveAnswers($data)', e-required='') {{ answer.answer || 'empty'}}
                    td
                        // editable  (select-local)
                        span(editable-text='answer.correct', e-name='correct', e-form='rowform') {{ answer.correct }}
                    td
                        // editable
                        span(editable-text='answer.rank', e-name='rank', e-form='rowform' ) {{ answer.rank }}
                    td
                        // editable
                        span(editable-text='answer.position', e-name='position', e-form='rowform') {{ answer.position }}
                    td(style='white-space: nowrap')
                        // form
                        form.form-buttons.form-inline(editable-form='', name='rowform' onbeforesave='changeAnswers()', ng-show='rowform.$visible', shown='inserted == answer')
                            button.btn.btn-primary(type='submit', ng-disabled='rowform.$waiting') save
                            button.btn.btn-default(type='button', ng-disabled='rowform.$waiting', ng-click='rowform.$cancel()') cancel
                        .buttons(ng-show='!rowform.$visible')
                            button.btn.btn-primary(ng-click='rowform.$show()' ) edit
                            button.btn.btn-danger(ng-click='removeAnswer($index)') del
            button.btn.btn-default(ng-click='addAnswer()') Add row
form.form-horizontal(name="buttonQuestionAnswer")
    fieldset
        .form-group
            .col-md-10.col-md-offset-2
                .pull-right
                    button.btn.btn-primary(ng-controller="flQuestionCrudCtrl", ng-show="questionMode=='Create'",  ng-click="createQuestion()") Save New
                    |  
                    button.btn.btn-primary(ng-controller="flQuestionCrudCtrl", ng-show="questionMode=='Update'", ng-click="updateQuestion()") Update
                    |  
                    button.btn.btn-primary(ng-controller="flQuestionCrudCtrl", ng-show="questionMode=='Update'", ng-click="deleteQuestion()") Delete
                    |  
                    a.btn.btn-default(ui-sref="home") Cancel

【问题讨论】:

  • ngForm: "表单指令的可嵌套别名。HTML 不允许嵌套表单元素。嵌套表单很有用,例如,如果需要确定控件子组的有效性。 " docs.angularjs.org/api/ng/directive/ngForm
  • 将 rowform 更改为 ngForm 会弄乱 xeditable 表,因此我不得不将其更改回来。将 buttonQuestionAnswer 表单更改为 ngForm 没有影响,无论好坏。关于我可能会做出哪些其他更改的任何建议?

标签: javascript angularjs html savechanges


【解决方案1】:

由于我不知道 HTML 存在嵌套表单元素的问题,这是一个很好的提示。我删除了按钮表单并将创建/更新/删除按钮作为标题表单的一部分。然后我将表单名称传递给函数来创建、更新和删除记录。在函数中我添加了代码: myForm.$setPristine(true); myForm.$setSubmitted(true); myForm.$setDirty(false); 在可编辑表单中,我还将主表单的名称传递给 onbeforesave='changeAnswers(headerForm)'。在 changeAnswers 函数中,我添加了代码: myForm.$setDirty(true);

到目前为止工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 2022-11-16
    • 2018-11-28
    • 2015-11-24
    • 2023-01-02
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多