如何数据之后,在AngularJS通过AJAX加载我重新初始化数据表?(How do I re-in

2019-10-24 01:29发布

我使用AngularJS和数据表连同服务器端脚本通过AJAX来获取数据。

我的控制器是这样的:

var currentJobId = $stateParams.jobId;
var selectedJobId = $rootScope.currentJob;

if (currentJobId !== selectedJobId) {
    $rootScope.currentJob=$stateParams.jobId;
    var data = {
        id: $stateParams.jobId
    }
    $http.post('http://localhost/angular/scripts/getJob.php', data).success(function (thedata) {
        $scope.job = thedata.information;
        $scope.jobNotes = thedata.notes;
        $scope.jobMaterialUsage = thedata.materialUsage;
        $scope.jobItems = thedata.jobItems;
        $scope.jobSubcontractorCosts = thedata.subcontractorCosts;
        $scope.jobBondMiscCosts = thedata.bondMiscCosts;
        $scope.jobEmployeeTime = thedata.employeeTime;
    });
}

一个例子表如下所示:

<table class="table table-striped table-hover row-links" id="employeeTimeTable" ui-jq="dataTable" ui-options="tableOptions">
    <thead>
        <tr>
            <th>Employee Name</th>
            <th>Total Hours</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="time in jobEmployeeTime" ng-click="goToEmployee(job.id,time.id);">
            <td>{{time.name}}</td>
            <td>{{time.total_minutes}}</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th>Employee Name</th>
            <th>Total Hours</th>
        </tr>
    </tfoot>
</table>

因此,它基本上是看是否作业ID发生了变化,如果有变化,就使Ajax调用新作业信息 - 如果它并没有改变它什么都不做。 我的问题是,我的表被初始化的数据加载之前的数据表时,我打刷新。 从这个页面离开并回到它正确初始化数据表,而是通过URL去时,直接到页面或刷新它只是把数据在表格的上方,同时仍保持了“在表中没有可用的数据”和无数据表功能工作。

Answer 1:

随着角UI路由器 ,你可以用解决您的后端数据的状态控制器被调用之前做到这一点。

请各位看看下面或在此演示的jsfiddle 。

这是一个与出数据表,但是这可能是比较容易的部分补充。

 (function() { 'use strict'; angular.module('demoApp', ['ui.router', 'angularSpinner']) .run(StartUp) .factory('jobsDataService', JobsDataFactory) .config(Config); function JobsDataFactory($http) { var backendUrl = 'http://jsonplaceholder.typicode.com'; return { get: function(id) { return $http.jsonp(backendUrl + '/posts/'+id +'?callback=JSON_CALLBACK') .then(function(response) { return response.data; }); } }; } function Config($urlRouterProvider, $stateProvider) { $urlRouterProvider.otherwise('/jobs/1'); $stateProvider .state('jobs', { url: '/jobs/:id', templateUrl: 'partials/post.html', resolve: { job: function(jobsDataService, $stateParams) { console.log($stateParams.id); return jobsDataService.get($stateParams.id); } }, controller: function($scope, job) { console.log(job); $scope.job = job; } }); } function StartUp($rootScope){ // only used for loading spinner $rootScope .$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ $rootScope.loading = true; }); $rootScope .$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){ $rootScope.loading = false; }); } })(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/2.3.1/spin.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-spinner/0.6.2/angular-spinner.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.js"></script> <div ng-app="demoApp"> <span us-spinner="" ng-if="loading"></span> <a href="#" ui-sref='jobs({id: 1})'>job 1</a> <a href="#" ui-sref='jobs({id: 2})'>job 2</a> <a href="#" ui-sref='jobs({id: 3})'>job 3</a> <div ui-view=""></div> <script type="text/ng-template" id="partials/post.html"> <p>debug id = {{job.id}}</p> <h1>{{job.title}}</h1> <p>{{job.body}}</p> </script> </div> 



文章来源: How do I re-initialize datatables AFTER data has loaded via AJAX in AngularJS?