0

I read several answers on this topic but they don't seem to apply to my problem. My problem is quite complex. I have a form which uses ReportViewer.ASPX. The form is defined as following:

<form name="form" novalidate role="form"
          sm-dirty-check
          id="reportViewer"
          method="post"
          action="~/Infrastructure/ReportViewer/reportViewer.aspx"
          target="viewerIFrame"          
          ng-show="crud.showForm" class="ng-cloak">

       @* Form inputs *@
        <input type="hidden" name="labelType" value="Rental" />
        <input type="hidden" name="labelLayoutId" value="{{ crud.model.lbLayoutId }}" />
        <input type="hidden" name="itemsToPrint" value="{{ crud.jsItemsToPrint }}" />

The actual forms are defined in the tabs using ng-form (I only shared the top portion of my Edit form which is relevant to my question).

I also have these buttons at the bottom of the form:

    <button type="submit"
            ng-if="crud.model.lbLayoutId!==0"
            name="generateLabelButton"
            id="generateLabelButton"
            class="btn btn-primary pull-left"
            ng-click="crud.generateLabel()"
            ng-disabled="crud.isSaveButtonDisabled">
        @Labels.generateLabel
    </button>
    <div class="pull-left generateLabelButton">
        <data-desc:type ng-if="crud.model.lbLayoutId===0"
                        value="@Labels.generateLabel"
                        keep-pristine="true"
                        on-after-selection="crud.layoutSelected(selectedValue)"
                        title="{{ '@string.Format(Labels.selectX, Labels.labelLayout)'}}"
                        param="layouts"
                        message="@string.Format(Labels.selectX, Labels.labelLayout)"
                        selected="crud.model.lbLayoutId"
                        descrip-value="descrip"
                        id="layoutPickerButton"
                        name="layoutPickerButton"
                        button-type="button"
                        type="7"
                        filter-by="Label"
                        description="crud.model.lbLayout">
        </data-desc:type>
    </div>

So, if I have lblLayoutId defined, I have my regular submit button and I press it and get my form submitted and all is well.

If I don't have the lblLayoutId defined (it's 0), I need to use a directive which has a template for a button, when I press it, it opens a modal form to pick the layout, etc.

So, my problem is that after I picked the layout, I need to submit my form so the label can appear.

I tried making the directive to be of type submit (button-type property), this didn't work.

I also tried the following code in the method which is executed by the button when value is selected:

rentalEquipmentsCrudController.prototype.layoutSelected = function (selectedValue) {
    this.model.lbLayoutId = selectedValue;
    $("#generateLabelButton").click();        
}
rentalEquipmentsCrudController.prototype.generateLabel = function () {
    if (this.model.lbLayoutId === 0) return;
    this.jsItemsToPrint = "";
    this.itemsToPrint = this.getItemsToPrint();

    this.jsItemsToPrint = JSON.stringify(this.itemsToPrint);


    angular.element($("#viewerIFrame").contents()
      .find("#reportViewer_ReportViewer")).empty();
    let actionPath = angular.element($("#reportViewer")).attr("action");
    if (actionPath.slice(-3) !== "pdf") actionPath += "/Labels.pdf";

    angular.element($("#reportViewer")).attr("action", actionPath);
    this.showViewer = true;

};

The layoutSelected method is executed from my directive and the next code is executed by my regular button.

So, I'm at lost as how to make it work.

0

2 Answers 2

2

The role of forms in client-side AngularJS applications is different than in classical roundtrip apps, it is desirable for the browser not to translate the form submission into a full page reload. Instead post JSON data and receive JSON data responses. Go to the server for data, but not html/js/css etc.

Read AngularJS <form> Directive API Reference - Submitting a form and preventing the default action.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I was thinking this weekend that I would have to re-design as how the data for report (label) are returned. That ASPX code was actually not mine and I was trying to make it work the same way it was working in other places, but I came to realization that I do need to change this approach.
1

You don't want to combine ng-click with a button of type="submit", this will still cause the form to submit (non-programmatically). Instead, use type="button". Alternatively, you can keep type="submit" but add the ng-submit="crud.generateLabel()" to the form element

<form>
  ...
  <button type="button" ng-click="crud.generateLabel()">...</button>
</form>

Alternatively:

<form ng-submit="crud.generateLabel()">
  ...
  <button type="submit">...</button>
</form>

4 Comments

I see, for my normal button you suggest to not even have a code, but have it in the ng-submit - let me try that. I'm still not sure if it will work for my directive that needs to execute the picker's code and then my form needs to get submitted.
So, your suggestion indeed worked for my submit button. Unfortunately, I'm still not sure what do I need to do with my directive. Do I need to try to use it as button-type submit too? If yes, what needs to be done to avoid form submission until that directive did call its modal form and returned a result (the directive has ng-click = "click()" and click is a method on the scope
So the question is - is there a way to prevent form submission to fire on some condition?
I think my real question is - is there a better way to call this method="post" action="~/Infrastructure/ReportViewer/reportViewer.aspx" target="viewerIFrame" [E.g. instead of having this old style code may be just do it using http.post instead but yet have it somehow re-directed to frame?]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.