如何从Datatables中的Json对象读取子数组

时间:2014-09-18 02:56:10

标签: jquery json jquery-datatables

我长期以来一直坚持这个问题.. :(

我有一个试图从中读取子数组的对象。下面是JSON格式的外观。我试图从中提取qaccess值,就像qaccess.0.productqaccess.1.product一样。

  "qaccess": [
    {
      "product": "wm.od.prod.nsr",
      "status": "enabled",
      "roleIdentifiers": [

      ],
      "permissionIdentifiers": [

      ]
    },
    {
      "product": "gp.od.dev.nsr",
      "status": "Active",
      "roleIdentifiers": [

      ],
      "permissionIdentifiers": [

      ]
    }
  ]

以下是我的代码的样子:

addColumn: function(oObj, type, val){
    //however i checked now, and found that the `oObj, type, val` returns 'null' and hence i am unable to use the addColumn function anymore. that is one problem i faced and hence took up another approach of adding the function in the `mDataProp`, like the one shown in the EDIT part.

        propertySize = oObj.qaccess.length;

        for(i=0; i<propertySize; i=i+1){
        { 
        "mDataProp": "qaccess." + i + ".product",


            "sTitle": "Status",
            "sClass": "_status",
            "sWidth": "10%",
           }
        }


        });
})
        this.columns.push(col);
        // return the index of the new column
        return this.columns;


    },

要获取对象的多个值,我可以将其写入for循环,如上所示吗? 例如:

for(i=0; i<propertySize; i=i+1){ { "mDataProp": "qaccess." + i + ".product"}}

编辑:

我也尝试过这样的事情:

columns: [
 {"mRender": function(obj, val, data){

    propertySize =  data.qaccess.length;
           for(i=0; i<propertySize; i=i+1){
            col= {
                "mDataProp": "qaccess." + i + ".product",
                sTitle: "Account Name", sClass: "_accountName", sWidth: "25%"
            }
        }       
  }
 }]

上面的data返回:我试图在我的表格中获取'qaccess [0] .product'和'qaccess [1] .product'的值。

.   qaccess: Array[2]
.    0: Object
.       permissionIdentifiers: Array[0]
.       product: "wm.od.prod.nsr"
.       roleIdentifiers: Array[0]
.       status: "enabled"
.   1: Object
⁃       permissionIdentifiers: Array[0]
⁃       product: "gp.od.dev.nsr"
⁃       roleIdentifiers: Array[0]
⁃       status: "Active"

如何实现qaccess数组值的值。

感谢。

3 个答案:

答案 0 :(得分:1)

我将JSFiddle与一个如何访问数据的示例放在一起:http://jsfiddle.net/a3cc8vat/

底线是您使用

obj.qaccess[1].product

不正确:

obj.qaccess.1.product

你不应该在名字周围使用引号。你的循环应该更像是

for(i=0; i<propertySize; i=i+1){
        { 
        "mDataProp": this.qaccess[i].product,

不确定您的示例是否需要this,因为根据您的代码段很难说明上下文是什么。

答案 1 :(得分:0)

您可以使用以下代码获取价值:

"mDataProp": oObj["qaccess"][i]["product"]

答案 2 :(得分:0)

找到答案:

                mData: null,
                sTitle: "Products",
                sWidth: "10%",
                sClass: "_status",
                mRender: function (data, type, obj) {
                    var returnVal = "";
                  _.each(obj.qaccess, function(item, index){
                    returnVal += "<span class='_status'>" + item["product"] + "</span>";
                  });
                  return returnVal;

                }

这让我回复:val1 val2。

感谢@ jwatts1980的帮助,但我需要在这两个值之间添加逗号。 某些列可能有多个值,如果是这样,我需要添加逗号以便:

VAL1,VAL2。

任何帮助???

相关问题