简体   繁体   English

AngularJS-我想对我的HTML中的可编辑字段实施if-check

[英]AngularJS - I want to implement an if-check for an editable field in my HTML

I have built my first AngularJS page and it checks in new visitors and checks out any active visitors on the list. 我建立了我的第一个AngularJS页面,它签入新的访问者并签出列表中所有活动的访问者。 The app also allows the user to select any active visitor and edit/modify their information. 该应用程序还允许用户选择任何活跃的访问者并编辑/修改他们的信息。 However my issue is that, during modification, I have no control (as per my limited understanding). 但是我的问题是,在修改期间,我无法控制(根据我的有限理解)。

The HTML looks like this: HTML看起来像这样:

<table>
    <tr>
       <td></td>
        <td><b>Visitor</b></td>
        <td><b>Title</b></td>
        <td><b>Visitee</b></td>
        <td><b>Arrival</b></td>
        <td><b>Departure</b></td>
        <td><b>Status</b></td>
     </tr>
     <tr data-ng-repeat="user in userList">
        <td><a data-ng-click="selectUser(user)">Select</a></td>
        <td>{{user.Visitor}}</td>
        <td>{{user.Title}}</td>
        <td>{{user.Visitee}}</td>
        <td>{{user.Arrival}}</td>
        <td>{{user.Departure}}</td>
        <td><b>{{user.Status}}</b></td>
     </tr>
</table>

Above is the table structure that shows information about checked in visitors (also shows checked-out visitors with a non-active status field). 上面的表格结构显示有关签入访客的信息(还显示带有非活动状态字段的签出访客)。

There is a "Select" link that allows the user to select any particular visitor and their information gets populated into an editable group of textboxes: 有一个“选择”链接,该链接允许用户选择任何特定的访问者,并将他们的信息填充到可编辑的文本框组中:

<div>
    <div style="display: inline-block;">
        Visitor: 
    </div>
    <div style="display: inline-block; margin-left: 28px; width: 383px;">
        <input type="text" data-ng-model="currentUser.Visitor">
    </div>
</div>
<div style="margin-top: 20px"></div>
<div class="row" style="margin-left: 30px">
    <div style="display: inline-block;">
        Title: 
    </div>
    <div style="display: inline-block; margin-left: 40px;">
        <input type="text" data-ng-model="currentUser.Title">
    </div>
</div>
<div style="margin-top: 20px"></div>
<div class="row" style="margin-left: 30px">
    <div style="display: inline-block;">
        Visitee: 
    </div>
    <div style="display: inline-block; margin-left: 24px;">
        <input type="text" data-ng-model="currentUser.Visitee" >
    </div>
</div>
<div style="margin-top: 20px"></div>
<div class="row" style="margin-left: 30px">
    <div style="display: inline-block;">
        Arrival:
    </div>
    <div style="display: inline-block; margin-left: 25px;">
        <input type="text" data-ng-model="currentUser.Arrival">
    </div>
</div>
<div style="margin-top: 20px"></div>
<div class="row" style="margin-left: 30px">
    <div style="display: inline-block;">
        Departure:
    </div>
    <div style="display: inline-block; margin-left: 0px;">
        <input type="text" data-ng-model="currentUser.Departure">
    </div>
</div>

To checkin and checkout visitors I have the following buttons in html that call their respective functions in my JavaScript code: 为了签入和签出访问者,我在html中有以下按钮,它们在我的JavaScript代码中调用了各自的功能:

<div>
    <div style="margin: 2% 0 0 8%; display: inline-block">
        <button data-ng-click="addNew(currentUser)" class="btn btn-primary" type="button">Add New Check-in</button>
    </div>
    <div style="margin: 2% 0 0 1%; display: inline-block">
        <button data-ng-click="removeItem(currentUser)" class="btn btn-primary" type="button">Check-out</button>
    </div>
</div>

My JS file looks like this: 我的JS文件如下所示:

var app = angular.module('myApp', ['ngTouch', 'ui.grid']);
app.controller('UserController', ['$scope', '$filter', function ($scope, $filter) {
    $scope.selectUser = function (user) {
        $scope.currentUser = user;
    }
    $scope.userList = [
       { Visitor: "John Doe1", Title: "Lawyer", Visitee: "James Smith1", Arrival: "01/01/2016 10:45 AM", Departure: "01/01/2016 12:55 PM", Status: "N"},
       { Visitor: "John Doe2", Title: "Contractor Dude", Visitee: "James Smith2", Arrival: now, Departure: tentative_departureTime, Status: "A" },
       { Visitor: "John Doe3", Title: "Attorney", Visitee: "James Smith3", Arrival: now, Departure: tentative_departureTime, Status: "A" },
       { Visitor: "John Doe4", Title: "Maintenance Guy", Visitee: "James Smith4", Arrival: past_checkinTime, Departure: expired_departureTime, Status: "N" }
    ];
    $scope.addNew = function (currentUser) {      
        var now = new Date();
        var departureTime = new Date(now.getTime() + 2 * 60 * 60 * 1000);
        departureTime = departureTime.format("MM/dd/yyyy, hh:mm tt");
        $scope.currentUser.Arrival = $filter('date')(new Date(), 'MM/dd/yyyy, hh:mm a');
        $scope.currentUser.Departure = $filter('date')(departureTime);
        $scope.currentUser.Status = "Active";
        $scope.userList.push(angular.extend({}, currentUser));
        $scope.currentUser = {}; //clear out Employee object
    };
    $scope.removeItem = function (currentUser) {
        if ($scope.userList.indexOf(currentUser) >= 0) {
            var now = new Date();
            now = now.format("MM/dd/yyyy, hh:mm tt");
            if ($scope.currentUser.Departure > now) {
                $scope.currentUser.Departure = $filter('date')(now);
            }
            $scope.currentUser.Status = "N";
            $scope.currentUser = {}; // clear out Employee object
        }
        else {
            alert("Please select a visitor before atttempting to check-out.");
        }
    };
});

The issue is, lets say I have a VisitorA , and the person checked in at 10:00 AM, and is mistakenly marked as checked out at 12:00 PM, the status of their visit is set to "N" (not active) when the current time is 12:05 PM. 问题是,假设我有一个VisitorA ,并且该人在10:00 AM签入,并且被错误地标记为12:00 PM签出,他们的访问状态设置为“ N”(未激活)当前时间是12:05 PM。

I would like to set the checkout time to 01:00 PM (tentative, now that it is in the future). 我想将结帐时间设置为01:00 PM(暂定,因为现在是将来)。 I would like to add a check that will set the status back to "Active" when the departure time is set in the future. 我想添加一张支票,该支票将在将来设置出发时间时将状态重新设置为“有效”。 I am not sure how to change the status, since I cannot figure out where the "Edit" is taking place. 我不确定如何更改状态,因为我无法弄清楚“编辑”的位置。

My understanding is that you need a way to update the status of your users as and when it updates in real time. 我的理解是,您需要一种实时更新用户状态的方法。

Using ES6 Javascript you can define a User class which has a getter function allowing you to re-calculate the Status property on every digest cycle without changing the code in your view. 使用ES6 Javascript,您可以定义一个User类,该类具有一个getter函数,使您可以在每个摘要周期重新计算Status属性,而无需更改视图中的代码。

class User {
    constructor({ Visitor, Title, Visitee, Arrival, Departure } = {}) {
        this.Visitor = Visitor;
        this.Title = Title;
        this.Visitee = Visitee;
        this.Arrival = new Date(Arrival);
        this.Departure = new Date(Departure);
    }
    get Status() {
        return this.Departure > new Date() ? 'A' : 'N';
    }
}

Here we can simply call User.Status and it will calculate the status on the fly. 在这里,我们可以简单地调用User.Status ,它将即时计算状态。

Your JS then becomes something like the following (I've kept most of your calculation logic in tact): 这样,您的JS就会变成如下所示的样子(我将大部分计算逻辑保持不变):

var app = angular.module('myApp', ['ngTouch', 'ui.grid']);
app.controller('UserController', ['$scope', '$filter', function ($scope, $filter) {

    class User {
        constructor({ Visitor, Title, Visitee, Arrival, Departure } = {}) {
            this.Visitor = Visitor;
            this.Title = Title;
            this.Visitee = Visitee;
            this.Arrival = new Date(Arrival);
            this.Departure = new Date(Departure);
        }
        get Status() {
            return this.Departure > new Date() ? 'A' : 'N';
        }
    }

    $scope.userList = [
       new User({ Visitor: "John Doe1", Title: "Lawyer", Visitee: "James Smith1", Arrival: "01/01/2016 10:45 AM", Departure: "01/01/2016 12:55 PM" }),
       new User({ Visitor: "John Doe2", Title: "Contractor Dude", Visitee: "James Smith2", Arrival: now, Departure: tentative_departureTime }),
       new User({ Visitor: "John Doe3", Title: "Attorney", Visitee: "James Smith3", Arrival: now, Departure: tentative_departureTime }),
       new User({ Visitor: "John Doe4", Title: "Maintenance Guy", Visitee: "James Smith4", Arrival: past_checkinTime, Departure: expired_departureTime })
    ];

    $scope.addNew = function () {
        // Note: I removed the currentUser argument from the function call since it 
        // was redundant (we can use $scope.currentUser directly)

        var now = new Date();
        var departureTime = new Date(now.getTime() + 2 * 60 * 60 * 1000);
        departureTime = departureTime.format("MM/dd/yyyy, hh:mm tt");

        var newUser = new User({
            Arrival: new Date($filter('date')(new Date(), 'MM/dd/yyyy, hh:mm a')),
            Departure: new Date($filter('date')(departureTime)),
        });
        $scope.userList.push(newUser);
        $scope.currentUser = {}; // clear out Employee object
    };

    $scope.removeItem = function () {
        // Note: I removed the currentUser argument from the function call since it 
        // was redundant (we can use $scope.currentUser directly)

        if ($scope.userList.indexOf($scope.currentUser) >= 0) {
            var now = new Date();
            now = now.format("MM/dd/yyyy, hh:mm tt");
            if ($scope.currentUser.Departure > now) {
                $scope.currentUser.Departure = new Date($filter('date')(now));
            }
            $scope.currentUser = {}; // clear out Employee object
        }
        else {
            alert("Please select a visitor before atttempting to check-out.");
        }
    };
});

And your HTML remains unchanged! 而且您的HTML保持不变!

I agree with the comments that you're best off comparing dates by comparing Javascript Date objects instead of strings, so I've cast the dates in your example to Date objects. 我同意这样的意见,即最好通过比较Javascript Date对象而不是字符串来比较日期,因此我已将示例中的日期转换为Date对象。 For a challenge you could try implementing some setters and getters on the User class which allow you to assign a string date value and have it converted into a Date object inside the class. 遇到挑战时,您可以尝试在User类上实现一些setter和getter,以允许您分配字符串日期值并将其转换为类中的Date对象。

Read more about ES6 classes here: https://coryrylan.com/blog/javascript-es6-class-syntax . 在此处阅读有关ES6类的更多信息: https ://coryrylan.com/blog/javascript-es6-class-syntax。

Note that for support in older browsers, you may need to transpile ES6 using a bundler like Webpack with Babel. 请注意,为了获得较旧版本浏览器的支持,您可能需要使用捆绑器(如Webpack和Babel)来转换ES6。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何检查我所有的html内容都已加载到angularjs中? - How do i check all my html contents are loaded in angularjs? 如何在Angular中实现可取消的可编辑字段? - How do I implement a cancellable editable field in Angular? 我想将拼写检查器插入html可编辑div吗? - I want a spell checker plug in to html editable div? 如何使html输入字段的占位符文本可编辑? - How can I make the placeholder text of an html input field editable? 我想使用angularJs和ionic实现类似于Facebook的注释部分 - I want to implement comments section similar to that of facebook using angularJs and ionic 如何将金额输入字段设置为不可编辑和可编辑我正在使用 angularJS 编程语言 - How can i set amount input field as non-editable and editable i am using angularJS programming language AngularJS:我想在单击时将值插入输入字段 - Angularjs : i want to insert value to input field on click 我的 json 中有一个字典列表,我想在 angularjs 中使用它 - I have a list of dictionaries in my json, I want to use it in angularjs 我想将外部HTML加载到我的文件中 - I want to load an external html in to my file 在angularjs指令中,我想在HTML中找到一个ID并在其上添加类 - In angularjs directive I want to find an id in a HTML and add class on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM