0

I need to nest an array I've created in JSON inside of another array to give it proper formating for jQuery Datatables library.

Here is my current code:

$.ajax({
    url: uMtbls,
    dataType: 'json',
    success: function(jMtbl) {
        $.each(jMtbl, function(i, item) {
            mAll = jMtbl[i];
            mOwn = jMtbl[i].OrigOwner;
            mPub = jMtbl[i].PublicationTblNm;
            mTbl = jMtbl[i].TableId;
            mMig = jMtbl[i].MigrationFreq;
            mGTYPE = jMtbl[i].GTYPE;
            exec();

        });

        oTable = $('#dtManaged').dataTable({
            "bStateSave": true,
            "bProcessing": true,
            "bServerSide": true,
            "aaData": jReform,
            "aoColumns": [{
                "sName": mOwn,
                "sTitle": "Original Owner",
                "sWidth": "10%"
            }, {
                "sName": mPub,
                "sTitle": "Table Name"
            }, {
                "sName": mTbl,
                "sTitle": "ID"
            }, {
                "sName": mMig,
                "sTitle": "Migration Frequency"
            }, {
                "sName": mGTYPE,
                "sTitle": "Oracle Gtype"
            }],
            "sPaginationType": "full_numbers",
            "iDisplayLength": 25,
            "aLengthMenu": [
                [25, 50, 100, -1],
                [25, 50, 100, "All"]
            ]
        });
    }
});

function exec() {
    jBld = mOwn + "," + mPub + "," + mTbl + "," + mMig + "," + mGTYPE;
    //console.debug("[" + mOwn + "," + mTbl + "]");
    jReform = jBld.split(",");
    console.debug(jReform);
}​

The output looks like this (captured in console.debug):

["text","text","text","text","text"]
["text","text","text","text","text"]

The sample should be:

{ "aaData": [
  [ 10126, 10002253, 415 ]
]}
11
  • 2
    Why don't you decode the JSON to a Javascript variable and then manipulate the data however you want? Commented Dec 4, 2012 at 20:17
  • that's what I'm currently doing. I'm having a problem nesting the array that I'm creating inside another array to make the data into the format that is required Commented Dec 4, 2012 at 20:21
  • 1
    Then please show only that code. The AJAX aspect is then irrelevant. Show ONLY the code that is necessary. Commented Dec 4, 2012 at 20:22
  • You don't know how to use arrays in any way, the exec() function is proof of it. That's no way to create a fixed length array Commented Dec 4, 2012 at 20:32
  • 3
    I know, but is it coming from a file? your server response? hardcoded? If it's coming from your server, it's probably easiest to format it there Commented Dec 4, 2012 at 20:35

1 Answer 1

1

Use Array.push. And make use of the var statement, you are creating a hell of global variables.

var aaData = [];
$.each(jMtbl, function (i, item) {
  aaData.push([
    item.OrigOwner,
    item.PublicationTblNm,
    item.TableId,
    item.MigrationFreq,
    item.GTYPE
  ]);
});

oTable = $('#dtManaged').dataTable({
  ...
  "aaData": aaData,
  ...
});

(Note: the links are not only for show, please read through them.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I don't get a chance to have my code reviewed(ever) so I'm wild wild west, which isn't a good thing...thanks that solved my issue and I'll be doing some reading tonight

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.