【问题标题】:AngularJS refresh smart table after inserting new record in Bootstrap Modal Form在 Bootstrap 模态表单中插入新记录后,AngularJS 刷新智能表
【发布时间】:2015-01-10 10:43:18
【问题描述】:

我使用 angular-bootstrap 和 Angular Smart 表。 我有一个模式表单,它向数据库添加新记录并在我的智能表中显示列表。添加新记录工作正常,但是在将新记录插入数据库后如何刷新智能表。

控制器

//ajax request to gather data and list in smart table
october.controllers['dashboard/feeds'] = function ($scope, $filter , $request, $modal, $log, $http) {

    $scope.api = $request('onFeeds', {success: function(data, scope){
        this.success(data).done(function() {
            $scope.rowCollection = [];
            $scope.rowCollection = angular.fromJson(data.result);
            $scope.displayedCollection = [].concat($scope.rowCollection);


            });
        }
    });
}

//Modal

var ModalInstanceCtrl = function ($scope, $modalInstance,  $request, $filter) {


    $scope.save = function (data) {

        $request('onAddFeed',

        {data:data, 
                success: function(data){
                this.success(data);
                    $scope.refresh();
                    $modalInstance.close();

            }
        });

    };
};

模板

        <script type="text/ng-template" id="myModalContent.html">
            <div class="modal-header">
                <h3 class="modal-title">neue Feed eintragen!</h3>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                  <input type=hidden ng-init="formInfo.user_id = {% endverbatim %} {{ user.id }} ">  
                  {%verbatim %}
                  <div class="form-group">
                    <label for="inputName3" class="col-sm-2 control-label">Feed Name</label>
                    <div class="col-sm-8">
                      <input class="form-control" id="inputName3" placeholder="Feed Name" ng-model="formInfo.name">
                  </div>
              </div>
              <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label">Feed Type</label>
                <div class="col-sm-8">
                  <select ng-model="formInfo.feed_type" class="form-control">
                      <option>Select Feed Source</option>  
                      <option value="api">API</option>
                      <option value="xml">XML</option>
                      <option value="csv">CSV</option>
                      <option value="json">JSON</option>
                      <option value="upload">Upload</option>
                  </select>
              </div>
        </div>

        <div class="form-group" ng-show="formInfo.feed_type =='api'">
            <label for="selectAPI" class="col-sm-2 control-label">Select API</label>
            <div class="col-sm-8">
              <select ng-change="selectAction(item.id)" ng-model="formInfo.network_id" ng-options="item.id as item.name for item in networks" class="form-control"> </select>
            </div>
        </div>

        <div class="form-group" ng-repeat="setup in setups">
            <label for="setup" class="col-sm-2 control-label">{{setup}}</label>
            <div class="col-sm-8">
                <input ng-model="formInfo['api_credentials'][setup]" class="form-control" placeholder="Enter your {{setup}}">

         </div>
        </div>

        <div class="form-group" ng-show="formInfo.feed_type =='xml' || formInfo.feed_type =='csv' ">
            <label for="inputPassword3" class="col-sm-2 control-label">Source</label>
            <div class="col-sm-8">
                <div class="input-group">
                  <div class="input-group-addon"><i class="fa fa-link"></i></div>
                <input ng-model="formInfo.source" type="text" class="form-control" id="sourceInput" placeholder="URL">
        </div>

      </div>
  </div>
</div>
<span>{{formInfo}}</span>
</form>
</div>
<div class="modal-footer">
    <button type="submit" class="btn btn-primary" ng-click="save(formInfo)">Save</button>
    <button type="button" class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>

【问题讨论】:

  • 不能直接调用$scope.api刷新表格吗?
  • 我一直在尝试似乎不起作用

标签: angularjs angularjs-directive angular-bootstrap smart-table


【解决方案1】:
{data:data, 
            success: function(data){
            this.success(data);
                $scope.refresh();
                $modalInstance.close();

        }

在上面的代码中,而不是 $scope.refresh() 使用 $route.reload() 来 在关闭 Modal Instance 之前更新记录。

reload() 使 $route 服务重新加载当前路线,即使 $location 没有改变

【讨论】:

  • 这对我有用,但您似乎不必强制重新加载。我原以为只需将其添加到 smart-table 正在监视的数组中,但这绝对行不通。
【解决方案2】:

要刷新智能表,您可以添加或删除项目,或者只是重新创建数组

$scope.tableData = [].concat($scope.tableData);

【讨论】:

    【解决方案3】:

    对我有用(并且似乎导致您出现问题)的方法是将displayedCollection 设置为一个空数组。 Smart Table 将使用您指定的任何分页、过滤或排序数据填充它。像这样的:

    <table st-table="displayedCollection" st-safe-src="rowCollection">
    

    突然间,rowCollection.push(data) 自动更新了智能表。

    【讨论】:

    • 我使用 Smart Table 作为您的,但这个解决方案对我不起作用,因为它清除了表格但不刷新它。
    • 您是在控制器中设置$scope.displayedCollection = []; 还是设置$scope.displayedCollection = [].concat(rowCollection);?如果您执行第一个,它将起作用。
    • 是的,我做了第一个。没有$route.reload() 它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多