0

我正在开发一个 AngularJS 站点,并使用 angular-daterangepicker 来处理日期。
角度日期范围选择器

在我的页面上,datepicker 工作正常,它以今天的开始日期初始化,并在从今天起的 5 天内结束日期(当页面加载时它会这样做)。

然后我从 startdate 迭代到 enddate,将每个日期添加到关联数组中,然后尝试将该数组用于具有 ng-repeat 的表,以便每个日期都是一个新行 - 但我的问题是没有日期显示在表格中,在调试中我可以看到正在创建关联数组,其中包含所有日期。

理想情况下,如果我的表格显示日期(并且日期格式正确),这就是我的表格的外观 - 但我无法让表格显示任何日期。 在此处输入图像描述

*附加问题- 由于我使用 ng-repeat 在表格中显示日期,“Shift 1”下的所有“选择”框最终都绑定到同一个对象 - 我该如何阻止这个?当用户选择了所有内容(即:shift 1、shift 2、sleep over)时,所有这些数据都将提交到数据库。

这是我的 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 控制器:

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++;
            //}
        };
    });
4

1 回答 1

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));
};
于 2018-07-14T08:41:11.297 回答