首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用angularjs-datepicker将日期添加到表中

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

Stack Overflow用户
提问于 2018-07-14 06:32:46
回答 1查看 781关注 0票数 0

我正在开发一个AngularJS站点,并使用角数据采集器来处理日期。

角数据采集器

在我的页面上,datepicker很好地工作&它以今天的开始日期初始化,从今天起5天内结束(当页面加载时它就这样做了)。

然后,我从startdate迭代到enddate,将每个日期添加到关联数组中&然后尝试对带有ng的表使用该数组-重复,这样每个日期都是一个新行-但是我的问题是表中没有显示日期,在调试过程中,我可以看到关联数组是用所有日期创建的。

理想情况下,如果我的表显示日期(日期格式正确),这就是它的样子--但我无法让表显示任何日期。

*附加问题-在使用ng时重复显示表中的日期,“Shift 1”下的所有“选择”框最终绑定到同一个对象--我如何阻止这种情况?当用户选择了所有的数据(即: shift 1,shift 2,休眠),所有这些数据都将被提交到数据库中。

这是我的HTML:

代码语言:javascript
复制
<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>

这是我的角度控制器:

代码语言:javascript
复制
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++;
            //}
        };
    });
EN

回答 1

Stack Overflow用户

发布于 2018-07-14 08:41:11

我解决了我自己的大部分问题(但我没有解决我的“附加问题”);主要问题是我没有将数组转换成JSON;

HTML表现在:

代码语言:javascript
复制
<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>

以及控制器中更改的函数:

代码语言:javascript
复制
$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));
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51335932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档