esvit / ng-table

Simple table with sorting and filtering on AngularJS
http://esvit.github.io/ng-table
BSD 3-Clause "New" or "Revised" License
2.77k stars 851 forks source link

Where to put code after $defer.resolve() #658

Open lrz-hal opened 9 years ago

lrz-hal commented 9 years ago

I have this config in my angular app

 $scope.events = [];
        $scope.search = function ($defer, params) {
            $("body").isLoading({ text: 'Daten werden geladen.', transparency: 0.1 })
            var query = $scope.searchTerm;
            $http({ method: 'GET', url: "http://localhost:53182/api/Elastic/Query?query=" + query, headers: { 'Authorization': 'Bearer ' + localStorage.getItem("accessToken") } }).success(function (data) {
                $scope.events = data;
                $defer.resolve(data);
                var find = query;
                var re = new RegExp(find, 'g');
                var table = document.getElementById("tableResult");
                for (var r = 0, n = table.rows.length; r < n; r++) {
                    for (var c = 0, m = table.rows[r].cells.length; c < m; c++) {
                        if (table.rows[r].cells[c].innerHTML.indexOf(query) != -1 && query) {
                            table.rows[r].cells[c].innerHTML = table.rows[r].cells[c].innerHTML.replace(re, '<mark>' + query + '<mark>')
                        }
                    }
                }

                $("body").isLoading("hide");
            }).error(function (msg, status, headers, config) {
                $("body").isLoading("hide");
                ModalDialog.open(msg.message);
            })
        };

        $scope.tableParams = new ngTableParams({
            count: $scope.events.length
        }, {
            counts: [],
            getData: $scope.search
        });

        $scope.searchTerm = "";

What i want to achieve is to mark cells that contains a word. If i put it after $defer.resolve(data), $defer.resolve() is calling later and override it with the new data. I have try $defer.resolve(data).then() because i thought $defer is a promise. But that's not working. So my question is, where i have to put code after ngtable is reloaded.

Many thanks,

Martin

IgorWolbers commented 9 years ago

I would change the logic. Iterate through your returned list from your http GET first and enrich the list to mark each item if the word was found or not. At the end of your logic call $defer.resolve. Include an angular expression in your ngTable HTML to then highlight records where the term has appeared based on your added boolean marker property.

This will also give you a cleaner SOC (separation of concerns) by leaving markup in your HTML and logic of if something is found or not based on a search in your javascript.