Blame view

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