简体   繁体   English

通过单击另一个数据表中的一行来过滤一个表中的数据AngularJS

[英]Filtering data in one table by clicking on a row in another datatables AngularJS

I have two AngularJS DataTables displayed. 我有两个显示的AngularJS数据表。 First one with counties and second with towns. 第一是县,第二是镇。 So, what I need to do is filter data in second table by clicking a row in first table. 因此,我需要做的是通过单击第一个表中的一行来过滤第二个表中的数据。 For example, when I click on row with Orange County in first table, in second should be filtered only towns from that county, not other ones. 例如,当我在第一个表格中单击橙县的行时,第二个中应仅过滤该县的城镇,而不过滤其他城镇。 Any suggestions and pointers on how to do that would be highly appreciated. 任何建议和指针如何做到这一点将不胜感激。

This is my code for displaying tables: 这是我显示表的代码:

<div>
        <datatables-lazy columns="countiesColumns" options="countiesOptions" class="grid"></datatables-lazy>
</div>
$scope.countiesColumns = [
        DTColumnBuilder.newColumn('name').withTitle(localizationService.getLabel('ime')),
        DTColumnBuilder.newColumn('code').withTitle(localizationService.getLabel('kod')),
        DTColumnBuilder.newColumn(null).withTitle('Akcije').notSortable().renderWith(actionsHtml)

    ];

    $scope.countiesOptions = DTOptionsBuilder.fromFnPromise(function () { return settingsService.getCounties(); })
        .withPaginationType('full_numbers')
        .withOption('createdRow', createdRow);

    function createdRow(row) {
        $compile(angular.element(row).contents())($scope);
    }

Here are services for getting data from database: 以下是用于从数据库获取数据的服务:

getCounties: function () {
        var deferred = $q.defer();
        $http.get(applicationSettings.humanResourcesAPIUrl + 'settings/getCountyList')
            .success(function (response) { deferred.resolve(response); })
            .error(function (err, status) { deferred.reject(err); });
        return deferred.promise;
    }, 
    getCounty: function (id) {

        var deferred = $q.defer();
        $http.get(applicationSettings.humanResourcesAPIUrl + 'settings/getCounty?countyId=' + id)
            .success(function (response) { deferred.resolve(response); })
            .error(function (err, status) { deferred.reject(err); });
        return deferred.promise;
    },

Everything is same for second datatable Towns. 第二个数据表城镇的情况相同。

You would need to setup an ng-click on the first table so that it assigns a value of the selected town to a variable via a function. 您将需要在第一个表上进行ng-click ,以便通过函数将所选镇的值分配给变量。

eg. 例如。 ng-click="setTown(town)"

$scope.activeTown = "";
$scope.setTown = function(town) {
    $scope.activeTown = town;
}

Then in the second table, all you would need is to filter on this activeTown variable. 然后在第二张表中,您只需要对此activeTown变量进行过滤。

<tr ng-repeat="place in places | filter:activeTown">

Here is an example http://plnkr.co/edit/os5X3ugAZOVC5v1zn0tF?p=preview 这是一个示例http://plnkr.co/edit/os5X3ugAZOVC5v1zn0tF?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM