Knockout数据表附加值(不重新加载)

时间:2014-12-11 18:57:37

标签: javascript jquery ajax knockout.js jquery-datatables

我是修改了我的knockout.js DataTabls的问题。所以我有一个数据表在页面加载时正确加载(即附加:函数方法),我有多个按钮(显示全部,允许,不允许)根据条件显示表。现在,当我点击这些按钮时,表再次重新加载,即如果我有5条记录,我会在点击按钮后看到10(5 + 5)。这只发生一次(即它保持在10并且不会达到15,20 ......)

这是我的视图模型 原始方法:

$.ajax({
    type: "GET",
    url: "/api/listusers/GetContractorList",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    headers: appsecurity.getSecurityHeaders(),

    success: function (result) {
        if (result != null) {
            var mappedContractorList = $.map(result, function (item) {
                return new GKContractorObj(item);
            });

            viewmodel.ContractorList(mappedContractorList);

            tableObj= $('#tblContractorsList').DataTable()

        }
    },
    failure: function (error) {
        logger.logError('Failed to contractor list', 'Error', null, true);
    }
});

单击DisplayAll按钮时调用Ajax

$.ajax({
    type: "GET",
    url: "/api/listusers/GetContractorList",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    headers: appsecurity.getSecurityHeaders(),

    success: function (result) {
        if (result != null) {
            var mappedContractorList = $.map(result, function (item) {
                return new GKContractorObj(item);
            });

            viewmodel.ContractorList(mappedContractorList);

            tableObj = $('#tblContractorsList').DataTable();


        }
    },
    failure: function (error) {
        logger.logError('Failed to contractor list', 'Error', null, true);
    }
});

观点:

<div class="divListGKContractor">
  <div class="tab-pane fade in active" id="listContractor">
    <div class="row">
      <div class="">
          <table id="tblContractorsList" class="display table table-condensed table-bordered table-responsive" cellspacing="0">
            <thead><tr><td>Contractor Details</td><td>Details</td></tr></thead>
            <tbody data-bind="foreach: ContractorList">
              <tr>
                <td data-bind="text: Contractor_Name"></td>
                <td>               
                   <a title="List_Contractor" class="btn-borderless widget-button" data-bind="click: $root.listContractorDetails">
                    <i class="fa fa-edit text-success"></i></a>
                </td>
              </tr>
            </tbody>
          </table>
       </div>
    </div>
  </div>
</div>

我认为控制器调用没有任何问题。它只是因为我不确定如何刷新/重新加载表。

任何帮助/建议?

1 个答案:

答案 0 :(得分:0)

此代码在加载时清除表并解决了问题。

//Clear table on load 
viewmodel.ContractorList([]);
tableObj.destroy();
$('#tblContractorsList tbody tr').remove();