淘汰无集装箱的foreach不工作

时间:2017-09-20 09:05:43

标签: javascript jquery typescript knockout.js

我必须遗漏一些东西,所以请让我知道 我正在使用Knockout 3.4.2

我所有的其他绑定都在工作,但foreach没有运行。我错过了什么吗?

我的PageViewModel:

export class UserManagementViewModel implements IPageViewModel {
        userApi: AjAxConnection<any>;
        listOfUsers: KnockoutObservableArray<IUser>;
        isLoading: KnockoutObservable<boolean>;
        currentPage: KnockoutObservable<number>;
        totalPages: KnockoutObservable<number>;
        totalCount: KnockoutObservable<number>;
        totalRecordsPerPage: KnockoutObservable<number>;

        // New User
        newUserEmail: KnockoutObservable<string>;
        newUserFirstName: KnockoutObservable<string>;
        newUserLastName: KnockoutObservable<string>;
        newUserValidation: KnockoutValidationGroup;

        constructor() {
            this.userApi = new AjAxConnection("api/user");
            this.listOfUsers = ko.observableArray([]);
            this.isLoading = ko.observable(false);
            this.currentPage = ko.observable(0);
            this.totalPages = ko.observable(0);
            this.totalCount = ko.observable(0);
            this.totalRecordsPerPage = ko.observable(10);

            this.newUserEmail = ko.observable("").extend({
                required: {message: "Email Address is required."}
            });
            this.newUserFirstName = ko.observable("").extend({
                required: {message: "First Name is required."}
            });
            this.newUserLastName = ko.observable("").extend({
                required: {message: "Last Name is required."}
            });

            this.newUserValidation = ko.validatedObservable({
                email: this.newUserEmail(),
                firstName: this.newUserFirstName(),
                lastName: this.newUserLastName()
            });

        }

        init(): void {
            this.updateUserList();
        }

        updateUserList(): void {
            this.isLoading(true);
            this.userApi.get("?page=" + this.currentPage() + "&totalPerPage=" + this.totalRecordsPerPage(),
                    ContentTypes.json)
                .done((data: IPagableResourceModel<IUser>) => {
                    console.log(data);
                    if (data.isSuccessful) {
                        this.listOfUsers().push(data.results);
                    } else {

                    }
                }).always(() => {
                    this.isLoading(false);
                });
        }

        openNewUserModal(): void {
            $("#newUserModal").modal("show");
        }

        cancelNewUserModal(): void {
            this.closeNewUserModal();
            this.clearNewUserDetails();
        }

        createNewUser(): void {
            if (this.newUserValidation.isValid()) {
                this.userApi.post("",
                    {
                        "firstName": this.newUserFirstName(),
                        "lastName": this.newUserLastName(),
                        "userName": this.newUserEmail()
                    },
                    ContentTypes.json);
            }
        }

        private closeNewUserModal(): void {
            $("#newUserModal").modal("hide");
        }

        private clearNewUserDetails(): void {
            this.newUserEmail("");
            this.newUserFirstName("");
            this.newUserLastName("");
        }
    }

IUser是基本的:

export interface IUser {
    id: string;
    firstName: string;
    lastName: string;
    userName: string;
}

最后是我的观点:

<div id="Users" class="tab-pane active">
    <h3>Users</h3>
    <div class="container">
        <div class="row">
            <div class="col-md-2">Id</div>
            <div class="col-md-2">UserName</div>
            <div class="col-md-2">FullName</div>
            <div class="col-md-2">Organization</div>
            <div class="col-md-2">Clinic</div>
            <div class="col-md-2">Options</div>
        </div>
        <div class="row" data-bind="visible: isLoading()">
            <div class="col-md-12 text-center">
                <img src="~/images/loading.gif" alt="Loading" />
                <br />
                Please wait, loading...
            </div>
        </div>
        <div class="row" data-bind="visible: !isLoading() && listOfUsers().length == 0">
            <div class="col-md-12 text-center">
                No Users Found.
            </div>
        </div>
        <div data-bind="visible: !isLoading() && listOfUsers().length != 0">
            <!--ko foreach: listOfUsers-->
            <div class="row">
                <div class="col-md-2"><!--ko text: id--><!--/ko--></div>
                <div class="col-md-2"><!--ko text: userName--><!--/ko--></div>
                <div class="col-md-2"></div>
                <div class="col-md-2"></div>
                <div class="col-md-2"></div>
                <div class="col-md-2">[OPTIONS GO HERE]</div>
            </div>
            <!--/ko-->
        </div>
    </div>
</div>

呈现的HTML和Knockout上下文: html and knockout context

显示API调用的控制台: console

1 个答案:

答案 0 :(得分:0)

我终于弄清楚了我的错误,同时它很小但很大。

在我的PageViewModel之前有这个:

    updateUserList(): void {
        this.isLoading(true);
        this.userApi.get("?page=" + this.currentPage() + "&totalPerPage=" + this.totalRecordsPerPage(),
                ContentTypes.json)
            .done((data: IPagableResourceModel<IUser>) => {
                console.log(data);
                if (data.isSuccessful) {
                    this.listOfUsers().push(data.results);
                } else {

                }
            }).always(() => {
                this.isLoading(false);
            });
    }

我不得不改变它:

    updateUserList(): void {
        this.isLoading(true);
        this.userApi.get("?page=" + this.currentPage() + "&totalPerPage=" + this.totalRecordsPerPage(),
                ContentTypes.json)
            .done((data: IPagableResourceModel<IUser[]>) => {
                console.log(data);
                if (data.isSuccessful) {
                    this.listOfUsers().push(data.results);
                } else {

                }
            }).always(() => {
                this.isLoading(false);
            });
    }