数据表的角度方式与$http带来了数据
Datatables the angular way with $http brought data
所以我正在开发一个 AngularJS 应用程序,我需要使用 $http 服务从服务器获取数据,如下所示:
$http({
method: 'GET',
url: $rootScope.apiURL + 'getAllClientLocations/' + session,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (response) {
if (response.ErrorMessage === null && response.Result !== null) {
$scope.locations = Object.keys(response.Result).map(function (key) {
return response.Result[key]
});
}
});
我正在尝试使用角度数据表显示这些数据。数据显示良好,我得到了数据表控件(分页、按钮、搜索框),但它们都不起作用,我得到了"表中没有可用数据"图例。
这是我的 html 设置:
<table class="table table-striped table-bordered table-hover" datatable="ng" dt-options="dtOptions">
<thead>
<tr>
<th>{{ 'BRANCH' | translate }}</th>
<th>{{ 'NAME' | translate }}</th>
<th>{{ 'STATUS' | translate }}</th>
<th>{{ 'ACTIONS' | translate }}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="station in stations">
<td>{{ station.LocationName }}</td>
<td>{{ station.StationName }}</td>
<td>{{ station.StationStatus }}</td>
<td>
<button type="button" class="btn btn-primary btn-xs" ng-click="editStation(station.CliLocationSalesStationId)" ng-stow="action.edit">Update</button>
<button type="button" class="btn btn-success btn-xs" ng-if="!station.HardwareFingerprint" ng-click="authorizeStation(station.CliLocationSalesStationId)" ng-stow="action.edit">Authorize</button>
<button type="button" class="btn btn-success btn-xs" ng-if="station.HardwareFingerprint" ng-click="deauthorizeStation(station.CliLocationSalesStationId)" ng-stow="action.edit">Deauthorize</button>
</td>
</tr>
</tbody>
</table>
这些是我的dtOptions:
$scope.dtOptions = DTOptionsBuilder.newOptions()
.withPaginationType('full_numbers')
.withDOM('frtip')
.withButtons([
'copy',
'print',
'excel'
]);
我知道由于异步而存在某种问题,DOM 是在我从服务器获取数据之前构建的。有没有办法告诉我数据表在我从服务器成功获取数据后呈现?
您可以
按如下方式使用fromFnPromise(promiseFn)
:
//reference $q in your controller as service
//e.x anuglar.module("app",[]).controller($q,$scope..)
function getPromiseData()
{
var deferred = $q.defer();
$http({
method: 'GET',
url: $rootScope.apiURL + 'getAllClientLocations/' + session,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function (response)
{
if (response.ErrorMessage === null && response.Result !== null)
{
$scope.locations = Object.keys(response.Result).map(function (key)
{
deferred.resolve(response.Result[key]);
});
}
else
{
deferred.resolve("");
}
});
return deferred.promise;
}
$scope.dtOptions = DTOptionsBuilder.fromFnPromise(function ()
{
return getPromiseData();
})
.withPaginationType('full_numbers')
.withDOM('frtip')
.withButtons([
'copy',
'print',
'excel'
]);
相关文章:
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- 如何通过溢出来判断元素被切断了多少像素:隐藏在父级上
- spring和angularJS(我得到了类似HTTP状态404的错误)
- ES6是否引入了一种机制来生成块范围的函数语句(而不是表达式)
- 如何逃离<脚本>jquery html()中的标记.我使用了innerHTML和outerHTML来解决这个
- 使用angular$http和django-urlpatters来获得json文件
- JQuery/Javascript来获取给定URL的Http Header详细信息
- 有没有什么方法可以通过输入字段(type=file)来找出选择了多少个文件
- 在 Node.js 中使用流来缓冲 HTTP 通信
- AngularJS$http返回200正常,但失败了
- 角度 2:使用一个 http 调用的结果来执行另一个调用
- 是否有必要使用 === 来比较 Javascript 中的字符串,或者 == 就足够了
- 当我转到另一个单选按钮时;s的价值观来了
- Javascript 渲染动画在 HTML5 DOM 上来自 Array.第一次运行慢!第二次运行顺利.怎么来了
- 怎么来了![] 不是真的
- 数据表的角度方式与$http带来了数据
- 如何在AngularJS中使用$http来更新持久内存
- 为什么$http成功了't返回更新后的承诺
- 使用node-mock -http来测试没有发出事件的服务
- 我怎么能删除后h2标签开放类来了.我在努力,但不工作