嘿,在那儿,
我已经在这个问题上挣扎了几天,我正在尝试创建一个角度指令模板表单,它接受一个数据源,并将使用该数据创建一个可搜索的表。到目前为止,我可以为表单独添加可搜索的列(例如search.BookId),它将在多个列上排序,但是我不知道列名会提前显示,所以我尽量保持它的流动性。
问题是当我使用search.{{key}时,它将不会显示正确的数据,而当检查元素时,只显示字面上的“search.{key}”。我试过使用search[{{key}}]来正确地获取密钥并将它们放在正确的位置,但是我无法按表上所需的方式进行筛选。
我的模板是这样的:

            '<table class="table table-striped table-bordered">' +
            '<thead>' +
            '<tr>' +
            //'<th><input type="text" ng-model="search.BookID"></input></th>' +

            '<th>' +
            '' +
            '</th>' +

                '<th ng-repeat="(key, value) in tableArray[0]">' +
                    '<input type="text" placeholder="{{key}}" ng-model="search.{{key}}"></input>' +
            '</th>' +

            '</tr>' +
            '</thead>' +
            '<tbody>' +
            '<tr ng-repeat="tr in tableArray | filter:search:strict">' +
            '<td ng-repeat="item in tr">{{item}}</td>' +
            '</tr>' +
            '</tbody>' +
            '</table>',

我的表数组:
$scope.tableArray = [
                    {
                        "BookID": "1",
                        "BookName": "Computer Architecture",
                        "Category": "Computers",
                        "Price": "125.60"
                    },
                    {
                        "BookID": "2",
                        "BookName": "Asp.Net 4 Blue Book",
                        "Category": "Programming",
                        "Price": "56.00"
                    },
                    {
                        "BookID": "3",
                        "BookName": "Popular Science",
                        "Category": "Science",
                        "Price": "210.40"
                    },
                    {
                        "BookID": "4",
                        "BookName": "Indian Studies",
                        "Category": "History",
                        "Price": "132.68"
                    },
                    {
                        "BookID": "5",
                        "BookName": "Derivatives and You",
                        "Category": "Math",
                        "Price": "206.73"
                    },
                    {
                        "BookID": "6",
                        "BookName": "The art of the Greeks",
                        "Category": "Social Studies",
                        "Price": "10.30"
                    },
                    {
                        "BookID": "7",
                        "BookName": "Chemistry 101",
                        "Category": "Chemistry",
                        "Price": "140.40"
                    }
                ];

我希望最终结果类似于角度文档过滤版本(plnkr:https://plnkr.co/edit/?p=preview)。但它们使用静态数据,而我使用的是动态数据。
我也尝试过一些事情:
'<table>' +
                '    <thead>' +
                '       <th ng-repeat="(key, value) in obj[0]"><input type="text" ng-model="search[\'{{key}}\']"></input></th>' +
                '    </thead>' +
                '</table>' +

但在过滤的时候也没用
 '<tr ng-repeat="tr in tableArray | filter:search:strict">' +

你知道我做错了什么吗?

最佳答案

ng-model中的值是根据当前作用域计算的AngularJS expression,因此不需要在其中使用双卷曲,只需执行ng-model="search[key]"
Here's a plunk显示它有效。

关于javascript - 如何动态创建搜索字段作为angular中的标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47643815/

10-09 17:55
查看更多