Knockout JS 用 Ajax 请求填充 table

Knockout JS Fill table with Ajax Request

我是 Knockout JS 的新手,我一直在尝试用 Ajax 请求的结果填充 table 但没有成功。我遵循了几个教程和许多 Stack Overflow 问题,但我仍然没有得到我需要的结果。

我现在遇到的错误是我的可观察数组未定义。这是我的 JS 代码:

function FeatureRequest(data) {

    if(data != null){
        // console.log("Feature Request");
        // console.log(data);
        this.title = ko.observable(data.title);
        this.description = ko.observable(data.description);
        this.client_id = ko.observable(data.client_id);
        this.client_priority = ko.observable(data.client_priority);
        this.product_area_id = ko.observable(data.product_area_id);
        this.user_id = ko.observable(data.user_id);
        this.target_date = ko.observable(data.target_date);
        this.ticket_url = ko.observable(data.ticket_url);
        this.date_finished = ko.observable(data.date_finished);
    }
}

function FeatureRequestViewModel() {
    // Data
    var self = this;
    self.features = ko.observableArray();

    console.log("Sending requests...");
    $.getJSON("/api/obscure/request", function(response) {
        var mappedFeatures = $.map(response, function(item) {
            return new FeatureRequest(item)
        });
        self.features(mappedFeatures);

    });

}

ko.cleanNode($("body")[0]);
var viewModel = new FeatureRequestViewModel();
ko.applyBindings(viewModel);

"undefined" 的变量是 self.features 可观察数组。

这里是我的HTMLtable我要填写:

<tbody id="featuresTable" data-bind="foreach:features">
            <tr>
                <td data-bind="text: features.title"> </td>
                <td data-bind="text: features.description"> </td>
                <td data-bind="text: features.client_id"> </td>
                <td data-bind="text: features.client_priority"> </td>
                <td data-bind="text: features.product_area_id"> </td>
                <td data-bind="text: features.target_date"> </td>
                <td data-bind="text: features.ticket_url"> </td>
                <td data-bind="text: features.user_id"> </td>
                <td class="single line">
                    <a class="edit ui icon violet button" value="features.id">
                        <i class="edit icon"> </i>
                    </a>
                    <a class="finish ui icon green button" value="features.id">
                        <i class="check icon"> </i>
                    </a>
                    <a class="delete ui icon red button" value="features.id">
                        <i class="delete icon"> </i>
                    </a>
                </td>
            </tr>
        </tbody>

foreach: features 中,内部 HTML 的绑定数据是 features 数组的一个元素。

通过删除 table 单元格中的 features. 前缀来修复代码。

在 foreach 中:功能不使用功能。[属性] 但仅使用 [属性]。

<tbody id="featuresTable" data-bind="foreach:features">
            <tr>
                <td data-bind="text: title"> </td>
                <td data-bind="text: description"> </td>
                <td data-bind="text: client_id"> </td>
                <td data-bind="text: client_priority"> </td>
                <td data-bind="text: product_area_id"> </td>
                <td data-bind="text: target_date"> </td>
                <td data-bind="text: ticket_url"> </td>
                <td data-bind="text: user_id"> </td>
                <td class="single line">
                    <a class="edit ui icon violet button" value="id">
                        <i class="edit icon"> </i>
                    </a>
                    <a class="finish ui icon green button" value="id">
                        <i class="check icon"> </i>
                    </a>
                    <a class="delete ui icon red button" value=".id">
                        <i class="delete icon"> </i>
                    </a>
                </td>
            </tr>
        </tbody>