javascript - 搜索时如何查看数据而不提交| Angular js

标签 javascript angularjs json ajax search

为了使搜索过程更快,我的客户要求在搜索框填满时查看数据,甚至没有提交,我的代码在提交时工作正常,我应该对我的代码进行哪些更改,以便我可以获得所需的结果结果。这是我使用 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);
}
}

我希望根据搜索结果直接更改表中的数据,我将附上我的 View 的图像 i want the result to be viewed in the table as the if i submit, not like the data list

最佳答案

放置ng-change而不是ng-click

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

在 Controller 功能

$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);
}

关于javascript - 搜索时如何查看数据而不提交| Angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47961396/

相关文章:

javascript - 错误 : ENOENT: no such file or directory, 打开 '/moviedata.json'

javascript - Mapbox GL JS 禁用滚动缩放

javascript - 使用 LiveValidation 滚动到第一个表单错误

javascript - AngularJS : set parent controller $scope variable from child directive

javascript - 我的网络应用程序如何与串行端口通信

java - 我可以检查 json 是否具有确定的结构吗?

MySQL 追加到 JSON 字段

javascript - Magento:可配置产品选项未填充下拉列表

javascript - 在框架内题写和居中图像

javascript - 在 VSCode for Intellisense 中记录 Javascript 代码