gpt4 book ai didi

mvvm - ajax调用后kendo mvvm不更新

转载 作者:行者123 更新时间:2023-12-03 19:47:11 26 4
gpt4 key购买 nike

我有一个页面(下面的相关代码)执行以下操作:

  • 用户在自动完成文本框中输入值
    2,选择自动完成选项后,将进行 ajax 调用以填充 2 个下拉列表
  • 用户需要从每个下拉列表中选择一个值
  • 一旦在两者上都选择了一个值,他们点击添加按钮,我的绑定(bind)表就会更新
  • 用户可以删除添加到表中的行

  • 在第 4 步中添加的行包含在可观察对象的数组中。
    页面第一次加载点 1 到 5 按预期工作.....

    但是,如果用户在自动完成框中输入新搜索并触发 select 事件,则第二次进行 ajax 调用时,我的 View 模型和 UI 对象之间的关系就会中断。

    正在执行的代码是相同的,所以请有人能解释一下为什么第二次会中断。
    <input type="text" id="txtBox" style="width:300px;" />

    <div id="fixturesBindable" style="padding:0 !Important;">
    <table>
    <thead>
    <tr>
    <th>Col1</th>
    <th>Col2</th>
    </tr>
    </thead>

    <tbody data-template="row-template" data-bind="source: Fixtures"></tbody>
    </table>

    <select id="a_teamsdropdown" data-role="dropdownlist" data-text-field="TeamFullName" data-value-field="Id" data-bind="source: Teams" style="width:200px;"></select>
    <select id="a_oppteamsdropdown" data-role="dropdownlist" data-text-field="TeamFullName" data-value-field="Id" data-bind="source:
    OpponentTeams" style="width:200px;"></select>

    <button type="button" data-bind="click: addFixture">Add Fixture</button>

    <script id="row-template" type="text/x-kendo-template">
    <tr>
    <td><input type="hidden" id="team" data-bind="attr: { name: TeamModelName, value: TeamId }" /></td>
    <td><input type="hidden" id="oppteam" data-bind="attr: { name: OppModelName, value: OppTeamId }" /></td>
    </tr>
    </script>

    </div>


    <script>
    $(document).ready(function () {
    var viewModel = kendo.observable({
    Teams: <%= Model.Teams %>,
    OpponentTeams: [],
    Fixtures: [],
    addFixture: function (e) {
    var Fixtures = this.get("Fixtures");
    var teamId = $("#a_teamsdropdown").val();
    var teamName = $("#a_teamsdropdown>option:selected").text();
    var oppteamId = $("#a_oppteamsdropdown").val();
    var oppteamName = $("#a_oppteamsdropdown>option:selected").text();

    this.get("Fixtures").push({
    TeamFullName: teamName,
    TeamId: teamId,
    OppTeamFullName: oppteamName,
    OppTeamId: oppteamId,
    OrderIndex: this.get("Fixtures").length,
    TeamModelName: 'Fixtures[' + this.get("Fixtures").length + '].TeamId',
    OppModelName: 'Fixtures[' + this.get("Fixtures").length + '].OpponentTeamId'
    });
    },
    resetFixture: function(){
    var Fixtures = this.get("Fixtures");
    $.each(Fixtures, function (key, fixture) {
    Fixtures.splice(0, 1);
    });
    }
    });

    opponents = $("#txtBox").kendoAutoComplete({
    minLength: 3,
    dataTextField: "Name",
    filter: "contains",
    dataSource: new kendo.data.DataSource({
    transport: {
    read: {
    url: "/url/Ajax",
    type: "POST",
    data: function () { return { searchText: $("#txtBox").val()}
    },
    complete: function (data) {
    opponents.list.width(400);
    }
    }
    },
    pageSize: 10,
    serverPaging: true,
    serverSorting: true,
    schema: {
    total: "count",
    data: "data",
    model: {
    id: "Id",
    fields: {
    Id: { editable: false }
    }
    }
    }
    }),
    change: function () {
    this.dataSource.read();
    },
    select: function (e) {
    $.each(opponents.dataSource.data(), function (index, value) {
    if (e.item.text() == value.Name) {
    selectedOpponent = value;
    $('#Fixture_OpponentTeam_Id').val(selectedOpponent.Id);
    $('#OpponentName').val(selectedOpponent.Name);
    $.ajax({
    url: 'GetOpponentTeams',
    data: { schoolId: selectedOpponent.Id, seasonId: seasonId, sportId: sportsId },
    type: 'GET',
    success: function (data) {
    viewModel.OpponentTeams = data;
    kendo.bind($("#fixturesBindable"), viewModel);
    },
    error: function (xhr, ajaxOptions, thrownError) {
    //alert('Error during process: \n' + xhr.responseText);
    alert(thrownError);
    }
    });
    return;
    }
    });
    }

    }).data("kendoAutoComplete");
    </script>

    最佳答案

    不确定这是否会解决您的问题,但总的来说,我建议不要在您的 ajax 成功回调中重新绑定(bind)所有内容。如果你只是 .set("OpponentTeams")而不是直接分配值,这有帮助吗?

    success: function (data) {
    viewModel.set("OpponentTeams", data);
    },

    调用 .set()应该会触发 #a_oppteamsdropdown 的刷新元素。

    关于mvvm - ajax调用后kendo mvvm不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14600319/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com