Blame view

build/Form/Form.js 2.32 KB
3a3ecabe   Imshann   init
1
2
  import template from "./Form.html";
  import style from "antd/lib/form/style/index.css";
81f8a467   Imshann   调整组件前缀
3
  angular.module("esNgAntd").directive("antdForm", function (esNgAntd) {
3a3ecabe   Imshann   init
4
      return {
81f8a467   Imshann   调整组件前缀
5
          controllerAs: "antdForm",
3a3ecabe   Imshann   init
6
7
8
9
10
11
12
13
          restrict: "E",
          transclude: true,
          replace: true,
          scope: {
              name: "@",
              labelCol: "=",
              wrapperCol: "=",
              onFinish: "&",
dd962f77   Imshann   优化
14
              form: "=",
3a3ecabe   Imshann   init
15
16
          },
          template: template,
061629e7   Imshann   add
17
          controller: function ($scope, $element, $attrs) {
3a3ecabe   Imshann   init
18
19
20
21
              this.getContext = function () {
                  return $scope;
              };
  
dd962f77   Imshann   优化
22
23
24
25
26
27
              $scope.state = {
                  formItems: [],
              };
  
              $scope.resetFields = function () {
                  $scope.state.formItems.forEach(function (item) {
a468667f   Imshann   优化
28
29
30
31
32
                      if (typeof item.setValue === "function") {
                          item.setValue(item.defaultValue || null);
                      } else {
                          item.value = null;
                      }
dd962f77   Imshann   优化
33
34
                  });
              };
3a3ecabe   Imshann   init
35
  
061629e7   Imshann   add
36
37
38
39
              $scope.submit = function () {
                  $scope.handleSubmit();
              };
  
3a3ecabe   Imshann   init
40
41
              $scope.handleSubmit = function () {
                  let values = {};
dd962f77   Imshann   优化
42
                  $scope.state.formItems.forEach(function (item) {
81f8a467   Imshann   调整组件前缀
43
                      let name = item.antdFormItem && item.antdFormItem.name;
fe17acf4   Imshann   优化组件
44
45
46
47
48
49
50
51
                      let value = null;
  
                      if (item.state.type === "checkbox") {
                          value = item.state.checked;
                      } else {
                          value = item.value || item.state.value;
                      }
  
dd962f77   Imshann   优化
52
                      values[name] = value;
061629e7   Imshann   add
53
                  });
3a3ecabe   Imshann   init
54
55
56
57
58
59
60
                  $scope.onFinish({
                      values: values,
                  });
              };
          },
          link: function ($scope, $element, $attrs, $controllers, $transclude) {
              esNgAntd.createStyle("ant-form", style);
3a3ecabe   Imshann   init
61
  
4b23b387   Imshann   feat(pagination):...
62
63
64
65
              if ($scope.form !== undefined) {
                  $scope.form = $scope;
              }
  
3a3ecabe   Imshann   init
66
67
68
69
70
71
72
73
74
75
76
              if ($scope.name) {
                  let inputs = $element[0].querySelectorAll("input");
  
                  for (let i = 0; i < inputs.length; i++) {
                      const element = inputs[i];
                      element.id = $scope.name + "_" + element.id;
                  }
              }
          },
      };
  });