سؤال

Is it possible to configure DataTables to hanlde array of objects instead of default array of arrays for aaData propery?

Here is my JSON:

{
    "sEcho": 1,
    "iTotalRecords": "57",
    "iTotalDisplayRecords": "57",
    "aaData": [
        {
            "userid": "tM4Ga0zX",
            "password": "ZKwMEiIa",
            "vpn_password": "K3lS2yn9",
            "status_id": 0,
            "expire_account": "0000-01-01 00:00:00",
            "created": "2014-02-11 09:29:02"
        },
        {
            "userid": "tM4Ga0zX",
            "password": "ZKwMEiIa",
            "vpn_password": "K3lS2yn9",
            "status_id": 0,
            "expire_account": "0000-01-01 00:00:00",
            "created": "2014-02-11 09:29:02"
        },
        {
            "userid": "tM4Ga0zX",
            "password": "ZKwMEiIa",
            "vpn_password": "K3lS2yn9",
            "status_id": 0,
            "expire_account": "0000-01-01 00:00:00",
            "created": "2014-02-11 09:29:02"
        }
    ]
}
هل كانت مفيدة؟

المحلول

Yes, add an aoColumns-definition your initialization :

..
"aoColumns": [
  { "mDataProp": "userid" }, 
  { "mDataProp": "password" },
  { "mDataProp": "vpn_password" },
  { "mDataProp": "status_id" },
  { "mDataProp": "expire_account" },
  { "mDataProp": "created" }
]
..

fiddle with your data as above -> http://jsfiddle.net/6tK7K/

Important : If you use dataTables v 1.9.2 or above, use the property name "mData" instead. As the docs says :

Note that prior to DataTables 1.9.2 mData was called mDataProp. The name change reflects the flexibility of this property and is consistent with the naming of mRender. If 'mDataProp' is given, then it will still be used by DataTables, as it automatically maps the old name to the new if required.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top