【问题标题】:Jquery data table with knockoutjs ,requirejs and mvc4带有knockoutjs、requirejs和mvc4的jquery数据表
【发布时间】:2015-04-26 04:23:44
【问题描述】:

我正在使用 Knockoutjs、requirejs 和 mvc4 来构建 Web 应用程序。我有一个 公司列表要显示在数据表中 我的 Common.js 如下:

require.config({
    baseUrl: "/Scripts/",
    paths: {
        "jquery": "jquery-1.8.2",
        "jqueryui": "jquery-ui-1.8.24",
        "jqdatatable": "jquery.dataTables",
        "bootstrapdatatable": "dataTables.bootstrap",
        "KO": "knockout-2.2.0"
    },
    shim: {
        "jqdatatable": "jquery.dataTables",
        "bootstrapdatatable": "dataTables.bootstrap"
    }
});

我的 CompanyGrid.js 如下所示:

define(["jquery", "jqdatatable", "bootstrapdatatable","KO"], function ($,ko) {
    $(document).ready(function () {
        var urlPath = window.location.pathname; 
        ko.applyBindings(CompanyListVM);
        CompanyListVM.getComapanies();

        var CompanyListVM = {
            Company: ko.observableArray([]),
            getComapanies: function () {

                $.ajax({
                    type: "GET",
                    url: '/Company/FetchCompanies',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        self.Company(data);
                        ShowGrid();
                    },
                    error: function (err) {
                        alert(err.status + " : " + err.statusText);
                    }
                });
            },
        };



    }





    );
}



);


function ShowGrid() {

    (function ($) {
        $("#liCompanyList").addClass("active");
        $("#liHome").removeClass("active");
        $("#liUserManagement").removeClass("active");
        $("#liReport").removeClass("active");
        $('#example').dataTable();
    }(jQuery));


}



//Model
function Company(data) {

    this.CompName = ko.observable(data.CompName);
    this.CompLanguage = ko.observable(data.CompLanguage);
    this.CompEmail1 = ko.observable(data.CompEmail1);
    this.CreatedBy = ko.observable(dat.CreatedBy);
}

这是显示公司列表。但我想显示另一个显示用户列表的网格。我已经使用必要的更改编写了相同的函数。代码是为 Usergrid.js 提供的。

define(["jquery", "jqdatatable", "bootstrapdatatable", "KO"], function ($, ko) {
    $(document).ready(function () {


        ko.applyBindings(person);
        ko.applyBindings(UserListVM);
        UserListVM.getUsers();
        var urlPath = window.location.pathname;
        var UserListVM = {
            User: ko.observableArray([]),
            getUsers: function () {
                var self = this;
                $.ajax({
                    type: "GET",
                    url: '/UserManagement/FetchUsers',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {

                        self.User(data);
                        ShowGrid();
                    },
                    error: function (err) {
                        alert(err.status + " : " + err.statusText);
                    }
                });
            },
        };



    }





    );






}



);


function ShowUserGrid() {

    (function ($) {
        $("#liCompanyList").addClass("active");
        $("#liHome").removeClass("active");
        $("#liUserManagement").removeClass("active");
        $("#liReport").removeClass("active");
        $('#tblUserList').dataTable();
    }(jQuery));


}



//Model
function User(data) {

    this.Usr_Id = ko.observable(data.Usr_Id);
    this.Usr_Name = ko.observable(data.Usr_Name);
    this.Usr_Email = ko.observable(data.Usr_Email);

}

但是这次用户网格没有来。webconsole显示以下错误“TypeError: ko is undefined”

请帮助我,我对 knockout.js 很陌生。

【问题讨论】:

    标签: asp.net-mvc-4 knockout.js requirejs


    【解决方案1】:

    您正在使用 require.js 并且您的参数不匹配。

    define(["jquery", "jqdatatable", "bootstrapdatatable", "KO"], function ($, ko) {
    

    jquery 正在加载到$

    jqdatatable 正在加载到ko

    【讨论】:

    • 谢谢。但我怀疑它在 companygrid 中运行良好。 js.it 仅在 usergrid 中显示错误。 js.请帮助我并消除我的疑问
    猜你喜欢
    • 2013-06-14
    • 2016-05-27
    • 2014-01-14
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2012-10-02
    相关资源
    最近更新 更多