动态地为JSON子数组添加值?

时间:2013-02-21 05:14:43

标签: javascript jquery json algorithm angularjs

我正在尝试实现图像中所示的过滤器。我正在使用Angular JS框架,JQuery。

要求是,我有复选框过滤器,如应用程序名称,状态等。在选择应用程序或状态时,我需要动态创建一个JSON,如下所示。应根据复选框值自动添加或删除“filterValues”中的值。

根据json值,我将查询数据库并获取结果并显示。

我正在构建如下所示的复选框:

<div class="row-fluid">
<div ng-repeat="status in keywords.kw_Status">
    <label class="checkbox">
        <input type="checkbox" value="{{status}}" ng-model="isChecked" ng-change="change('Status','txt_Status',status,isChecked)">{{status}}
    </label>
</div>

同步控制器代码如下

$scope.change=function(filterCategory, filterField, filterValue, filterAdd){
$scope.filters[filterCategory]={"filterField":filterField,"filterValues":[filterValue]};
console.log($scope.filters);

}

非常感谢任何帮助!!

I am trying to implement a filter as shown in the image

{
"Region": {
    "filterField": "kw_Region",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"ApplicationName": {
    "filterField": "kw_ApplicationName",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"IssueType": {
    "filterField": "kw_IssueType",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"Outage": {
    "filterField": "kw_Outage",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"Priority": {
    "filterField": "kw_Priority",
    "filterValues": [
        "aa",
        "bb"
    ]
}

}

1 个答案:

答案 0 :(得分:0)

  

应根据复选框值自动添加或删除“filterValues”中的值。

使用JSON.stringify和replacer回调实现此目的:

function replacer(match, offset, fullstring)
  {
  return replacer.str;
  }

replacer.str = "\u0022filterValues\u0022:[\u0022hi\u0022,\u0022bye\u0022]"; /* Use DOM node value */

var foo = JSON.stringify({
"Region": {
    "filterField": "kw_Region",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"ApplicationName": {
    "filterField": "kw_ApplicationName",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"IssueType": {
    "filterField": "kw_IssueType",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"Outage": {
    "filterField": "kw_Outage",
    "filterValues": [
        "aa",
        "bb"
    ]
},
"Priority": {
    "filterField": "kw_Priority",
    "filterValues": [
        "aa",
        "bb"
    ]
}
}).replace(/"filterValues[^\]]+./g, replacer)