使用angularjs-datepicker将日期添加到表中

时间:2018-07-14 06:32:47

标签: jquery html angularjs angular-daterangepicker

我正在开发AngularJS网站,并使用angular-daterangepicker处理日期。
angular-daterangepicker

在我的页面上,datepicker正常工作,并且以今天的开始日期进行初始化,并从今天起的5天后终止日期(并且在页面加载时执行此操作)。

然后我要从开始日期到结束日期进行迭代,将每个日期添加到关联数组中,然后尝试将该数组用于具有ng-repeat的表中,以便每个日期都是新行-但是我的问题是该表中没有显示日期,并且在调试中我可以看到正在使用所有日期创建关联数组。

理想情况下,如果显示日期(并且日期格式正确),这就是我的表格的外观-但我只是无法使表格显示任何日期。 enter image description here

*其他问题-由于即时通讯使用ng-repeat在表格中显示日期,因此“ Shift 1”下的所有“选择”框最终都绑定到同一对象-我该如何别这样吗就像用户选择了所有内容(即:班次1,班次2,休眠)一样,所有这些数据都将提交到数据库中。

这是我的HTML:

<div data-ng-controller="admRosController" data-ng-init="listStaff()">

    <div class="page-wrapper">
        <div class="banner notdark-translucent-bg" style="background-image:url('images/banner/admin.jpeg'); background-position: 50% 50%;">
            <div class="breadcrumb-container">
                <div class="container">
                     <ol class="breadcrumb">
                        <li class="breadcrumb-item"><i class="fa fa-home pr-2"></i><a class="link-dark" href="home">Home</a></li>
                        <li class="breadcrumb-item active">Staff Roster</li>
                    </ol>
                </div>
            </div>
    </div>

    <section class="main-container">
      <div class="container">
        <div class="row">
          <div class="main col-lg-8">
             <h1 class="page-title">Staff Roster</h1>
             <div class="separator-2"></div>
          </div>
        </div>

        <div class="row">
          <div class="main col-12">

          <form data-ng-submit="createRoster()" name="rosterForm" method="POST">
            <div class="form-row">
              <div class="form-group col-md-4">
                <label for="daterange1" class="control-label">Select Roster Dates:</label>
                <div class="input-group">
                  <div class="input-group-prepend">
                    <div class="input-group-text"><i class="fa fa-calendar-check-o"></i></div>
                  </div>
                  <input date-range-picker type="text" class="form-control form-control-sm date-picker" id="daterange1" name="daterange1" data-ng-model="rosData.date" options="{locale: {format: 'MMMM D, YYYY'}}">
                </div>
              </div>
            </div>
            <div class="table-responsive">
                <table class="table table-striped table-hover" data-ng-model="roster">
                    <thead class="thead-dark">
                        <tr>
                            <th class="text-center">Date</th>
                            <th class="text-center">Shift 1</th>
                            <th class="text-center">Shift 2</th>
                            <th class="text-center">Sleep Over</th>
                        </tr>
                    </thead>
                <tbody>
                    <tr class="text-center" data-ng-repeat="x in rosCal">
                        <td>{{rosCal[x].date}}</td>
                        <td><select class="form-control form-control-sm" data-ng-model="rosData.monshift1" data-ng-options="x.id as x.name for x in obj"></select></td>
                        <td><select class="form-control form-control-sm" data-ng-model="rosData.monshift2" data-ng-options="x.id as x.name for x in obj"></select></td>
                        <td><select class="form-control form-control-sm" data-ng-model="rosData.monsleep" data-ng-options="x.id as x.name for x in obj"></select></td>
                    </tr>
                </tbody>
            </table>

        </div>
            <div class="form-group has-feedback">
              <label for="message">Roster Notes</label>
              <textarea class="form-control" rows="3" id="notes" name="notes" data-ng-model="rosData.notes" placeholder=""></textarea>
              <i class="fa fa-pencil form-control-feedback"></i>
            </div>
            <p><strong>Shift 1</strong> 8:30am - 4:30pm (8 hrs)<br>
              <strong>Shift 2</strong> 9:00am - 3:00pm (6 hrs)</p>
            <br>
            <button type="submit" class="btn btn-primary">Save Roster</button>
            <button type="reset" value="Reset" class="btn btn-secondary">Clear</button>
          </form>
        </div>
      </div>
    </div>
  </section>
</div>
</div>

这是我的Angular Controller:

var app = angular.module('myApp', ['ngRoute', 'ngStorage', 'daterangepicker'])
app.controller ("admRosController", function ($scope, $http, $location) {
        $scope.rosCal = [];
        $scope.rosData = {};
        $scope.rosData.date = {
        startDate: moment(),
        endDate: moment().add(5, "days")
        };

        $scope.listStaff = function() {
            $http.get('php/rosstaff.php')
                .then(
                    function (response) {
                        $scope.obj = response.data;
                    },
                    function (response) {
                        // error handling routine
                    }
                );
                var i = 0;
                for (thisdate = $scope.rosData.date.startDate; thisdate < $scope.rosData.date.endDate; thisdate = moment().add(i, "days"))
                {
                    $scope.rosCal.push({date: thisdate});
                    alert($scope.rosCal[i].date);
                    i++;
                }

            };

        $scope.createRoster = function() {
            //var i = 0;
            //for (thisdate = $scope.rosData.date.startDate; thisdate < $scope.rosData.date.endDate; thisdate = moment().add(i, "days"))
            //{
                //$scope.rosCal.push({date: thisdate});
                //alert($scope.rosCal[i].date);
                //i++;
            //}
        };
    });

1 个答案:

答案 0 :(得分:0)

我解决了大多数自己的问题(但是我还没有解决我的“其他问题”); 主要问题是我没有将数组转换为JSON;

现在的HTML表:

<div class="table-responsive">
    <table class="table table-striped table-hover" data-ng-model="roster">
        <thead class="thead-dark">
            <tr>
                <th class="text-center">Date</th>
                <th class="text-center">Shift 1</th>
                <th class="text-center">Shift 2</th>
                <th class="text-center">Sleep Over</th>
            </tr>
        </thead>
        <tbody>
            <tr class="text-center" data-ng-repeat="x in rosCalJ track by $index">
                <td>{{x.date | date:'EEE, MMMM dd, yyyy'}}</td>
                <td><select class="form-control form-control-sm" data-ng-model="rosData.monshift1" data-ng-options="x.id as x.name for x in obj"></select></td>
                <td><select class="form-control form-control-sm" data-ng-model="rosData.monshift2" data-ng-options="x.id as x.name for x in obj"></select></td>
                <td><select class="form-control form-control-sm" data-ng-model="rosData.monsleep" data-ng-options="x.id as x.name for x in obj"></select></td>
            </tr>
         </tbody>
    </table>

以及控制器中已更改的功能:

$scope.listStaff = function() {
    $http.get('php/rosstaff.php')
        .then(
            function (response) {
                $scope.obj = response.data;
            },
            function (response) {
                // error handling routine
            }
        );
        var i = 0;
        for (thisdate = $scope.rosData.date.startDate; thisdate <= $scope.rosData.date.endDate; thisdate = moment().add(i, "days"))
        {
            $scope.rosCal.push({date: thisdate});
            i++;
        }
    $scope.rosCal.push({date: $scope.rosData.date.endDate})
    $scope.rosCalJ = JSON.parse(JSON.stringify($scope.rosCal));
};