javascript - Angularjs/jQuery : Change table's width after ajax call finishes.

标签 javascript jquery ajax angularjs angularjs-ng-repeat

我有一个表格,其默认宽度是某个值,例如父 div 的 80%。该表最初使用“ng-if”隐藏,直到某个 ajax 调用完成如下:

HTML:

<div ng-if="!loading">
    <table id="table-stable">
        <thead>
            //columns
        </thead>
        <tbody>
            <tr ng-repeat="data in tableData">
                //rows
            </tr>
        </tbody>
    </table>
</div>

AngularJS Controller :

$scope.loading = true;
someFactory.getData().then(function(data) {
    $scope.loading = false;
    $scope.tableData = data;
    //now need to write code to change table's width dependant on data.
})

正如 Controller 中所解释的,根据从工厂获取的数据,我需要更改表格的宽度。

问题是,根据获取的数据中的值,表的 2 列没有显示,因此,如果这些列没有显示,那么我需要缩小表的宽度。

目前,在 ajax 调用完成后编写 jQuery 是行不通的。上面指定的 Controller 的代码:

$scope.loading = true;
    someFactory.getData().then(function(data) {
        $scope.loading = false;
        $scope.tableData = data;
        $('#table-stable').attr('width', '100px'); //not working.
    })

我的猜测是表格还没有完全渲染?好吧,我是在黑暗中拍摄的。我该如何解决这个问题?

最佳答案

My guess is that the table hasn't been rendered completely?

你是对的。使用 ng-if 会使事情变得复杂,请使用 ng-showng-hide 代替。另外,jQuery 在这里是令人反感的,它与 Angular 一起真正有用的场景并不多。你可以做

<div ng-show="!loading">
    <table id="table-stable" ng-style="lengthyTable && { width: '100px' }">
    ...

关于javascript - Angularjs/jQuery : Change table's width after ajax call finishes.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31370432/

相关文章:

php - 使用ajax设置时$_GET变量没有保持设置状态?

jquery - 如何在 jQuery 中放大缩略图中的图像?

c# - MVC3 (Razor) 单选按钮检查

javascript - 从“二进制格式”文件中提取值并创建具有固定字段大小的文本文件

javascript - rails 5 : update action not working for AJAXified form

javascript - Onclick函数获取id

javascript - QUnit测试AJAX回调结果

ajax - 不同类型的网络选项卡中的两个调用

javascript - 如何使用 Promise 设置变量值

javascript - 数组数组中字母分配的改进