【问题标题】:how to view the data when searching without submitting | angularjs不提交搜索时如何查看数据 | angularjs
【发布时间】:2017-12-24 14:24:55
【问题描述】:

为了使搜索过程更快,我的客户要求在仅填充搜索框时查看数据,甚至没有提交,我的代码在提交时工作正常,我应该用我的代码更改什么以便我可以得到想要的结果。这是我第一个使用 Angular js 的项目,我对这项技术非常陌生。非常感谢。

HTML View:
<input id="searchInput" type="text"/> // search box where

// the function below "getSearchResults()" will get results when submitting 
<input ng-click="getSearchResults()" type="submit"/>

<table>
    <thead>
        <tr> 
            <th>NOM</th>
            <th>TELEPHONE</th>
            <th>LOCATION</th>
            <th>CODE</th>
        </tr>
    </thead>
    <tbody >

        //view the data
        <tr ng-repeat="c in clients">
            <td>{{c.firstname}} {{c.lastname}}</td>
            <td>{{c.telephone}}</td>
            <td>{{c.location}}</td>
            <td>{{c.code}}</td>
        </tr>
    </tbody>
</table>

Js source:


var app = angular.module('DMDGroup', []);
$scope.clients;
app.controller('ClientALL', function($scope, $http) {

/* the function put all results in the scope variable (client) in a json 
     form and the results viewed with the ng-repeat on the tr tag*/

$scope.getSearchResults=function(){
    var searchKeyWord=$("#searchInput").val();
    var url = '../php/clients.php';
    var data = {"function":"getClients",
            "searchKeyWord":searchKeyWord};

    var options={
        type : "get",
        url : url,
        data: data,
        dataType: 'json',
        async : false,
        cache : false,
        success : function(response,status) {
            $scope.clients = response;
            $scope.$apply();
        },
        error:function(request,response,error){
            alert("Error: " + error + ". Please contact developer");
        }
    };
    $.ajax(options);
}
}

我想根据搜索结果直接更改表中的数据,我会附上我的视图的图像

【问题讨论】:

    标签: javascript angularjs json ajax search


    【解决方案1】:

    ng-change代替ng-click

    <input ng-change="getSearchResults(searchVal)" ng-model="searchVal" class="searchClientBtn" type="submit"/>
    

    在控制器功能中

    $scope.getSearchResults=function(value){
        var url = '../php/clients.php';
        var data = {"function":"getClients",
                "searchKeyWord": value};
    
        var options={
            type : "get",
            url : url,
            data: data,
            dataType: 'json',
            async : false,
            cache : false,
            success : function(response,status) {
                $scope.clients = response;
                $scope.$apply();
            },
            error:function(request,response,error){
                alert("Error: " + error + ". Please contact developer");
            }
        };
        $.ajax(options);
    }
    

    【讨论】:

    • 好吧,但是“ng-model="searchVal" 怎么办,不应该在其他地方使用吗?
    • 我对 Angular js 很陌生
    • @AlyAlAmeen 如果你使用 ng-model="searchVal" 那么不需要使用var searchKeyWord=$("#searchInput").val(); 函数来获取输入值
    • 但我没有组件在 html 和 angularjs 函数中都具有名称“searchVal”,那么 anuglarjs 函数如何在没有任何“searchVal”定义的情况下获取搜索输入?甚至没有控制器,我对问题进行了一些编辑,我添加了 js 控制器
    • @AlyAlAmeen 有api调用服务,$http查一下。
    猜你喜欢
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2014-06-29
    相关资源
    最近更新 更多