What is "Requested unknown parameter" mean?

What is "Requested unknown parameter" mean?

tekknowtekknow Posts: 13Questions: 2Answers: 0

I'm getting the error "Requested unknown parameter 'year' for row 1, column 0". Here is my code:

<

script>

    $(document).ready(function(){
        var columns = [{'data': 'year'}];   //column definitions
        var data2 = [{'year': '2018'}];     //corresponding data values

        $('#incomeTable').DataTable({
            "processing": true,
            "serverSide": true,
            "ajax": {
                "url": "/RetirePlan/getAccountsW.php",
                "dataSrc": function(json) {
                    var row = [];
                    console.log("json.data.length="+json.data.length);
                    for (var i=0; i<json.data.length; i++) {
                        var balance = Number(json.data[i].Value);
                        var income = balance * 0.05;

                        //create column definitions
                        var col1 = {};
                        col1.data = 'balance' +i;
                        var col1Name = col1.data;
                        columns.push(col1);
                        var col2 = {};
                        col2.data = 'income' +i;
                        var col2Name = col2.data;
                        columns.push(col2);

                        //create corresponding column data
                        var val1 = {};
                        val1[col1Name] = balance;
                        data2.push(val1);
                        var val2 = {};
                        val2[col2Name] = income;
                        data2.push(val2);
                    }
                    console.log("columns=" +JSON.stringify(columns));
                    console.log("data2=" +JSON.stringify(data2));
                    return data2;               
                }
            },
            "columns": columns
        });

    });

    </script>
    </head>
    <body>
        <table id="incomeTable" class="display" style="width:100%">
        </table>    
    </body>

Here is the console log output:

! json.data.length=19
! VM426:37 columns=[{"data":"year","mData":"year"},{"data":"balance0"},{"data":"income0"},{"data":"balance1"},{"data":"income1"},{"data":"balance2"},{"data":"income2"},{"data":"balance3"},{"data":"income3"},{"data":"balance4"},{"data":"income4"},{"data":"balance5"},{"data":"income5"},{"data":"balance6"},{"data":"income6"},{"data":"balance7"},{"data":"income7"},{"data":"balance8"},{"data":"income8"},{"data":"balance9"},{"data":"income9"},{"data":"balance10"},{"data":"income10"},{"data":"balance11"},{"data":"income11"},{"data":"balance12"},{"data":"income12"},{"data":"balance13"},{"data":"income13"},{"data":"balance14"},{"data":"income14"},{"data":"balance15"},{"data":"income15"},{"data":"balance16"},{"data":"income16"},{"data":"balance17"},{"data":"income17"},{"data":"balance18"},{"data":"income18"}]
! VM426:38 data2=[{"year":"2018"},{"balance0":82632},{"income0":4131.6},{"balance1":272607},{"income1":13630.35},{"balance2":69149},{"income2":3457.4500000000003},{"balance3":23130},{"income3":1156.5},{"balance4":17246},{"income4":862.3000000000001},{"balance5":9879},{"income5":493.95000000000005},{"balance6":148176},{"income6":7408.8},{"balance7":90094},{"income7":4504.7},{"balance8":219421},{"income8":10971.050000000001},{"balance9":14658},{"income9":732.9000000000001},{"balance10":68153},{"income10":3407.65},{"balance11":26323},{"income11":1316.15},{"balance12":28448},{"income12":1422.4},{"balance13":4089},{"income13":204.45000000000002},{"balance14":4075},{"income14":203.75},{"balance15":7335},{"income15":366.75},{"balance16":2102},{"income16":105.10000000000001},{"balance17":1723},{"income17":86.15},{"balance18":13661},{"income18":683.0500000000001}]

I'm not seeing the problem, do you?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916
    edited May 2018

    Your column definition: [{'data': 'year'}]

    "Requested unknown parameter 'year' for row 1, column 0

    Your data:

    data2=[{"year":"2018"},{"balance0":82632},{"income0":4131.6},...
    

    You have configured Datatables to use an array of objects. data2 is an array of objects with the first object being {"year":"2018"}, the second {"balance0":82632}, etc. When Datatables is populating row 1 it is seeing {"balance0":82632} in the array but is expecting an object with year.

    I'm not sure what you really want to display and your data structure but maybe you are looking for something more like this:

    data2=[
    {"year":"2018", "balance0":82632, "income0":4131.6, ....},
    {"year":"2018", ....},
    ....
    ]
    

    The Data doc describes the formats required by Datatables.

    Kevin

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    Well what I'm trying to do is create a table where each row will have a different year, starting with 2018. Then there will be two columns for each account, Balance and Income. The headers will have two rows. The top row will be the account name (colspan=2) and the second row will be "Year" (rowspan=2), "Balance", "Income", "Balance","Income", etc.

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    I should add that I currently have only 1 row of data, corresponding to year 2018. I plan to generate the rest of the rows dynamically in the client.

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916
    edited May 2018

    Ok, it sounds like you want your data formatted like I suggested above:

    data2=[
    {"year":"2018", "balance0":82632, "income0":4131.6, ....},
    {"year":"2017", "balance0":3842, "income0":5335,3, ....},
    ....
    ]
    

    Does that make sense?

    Kevin

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    I will reorganize the data so each year is one object. I think you are right there.

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    I reorganized the data. No error now but only "2018" showed up in the first column. Nothing else showed up. Here is how the data looks now:
    data2=[{"year":"2018","balance0":82632,"income0":4131.6,"balance1":272607,"income1":13630.35,"balance2":69149,"income2":3457.4500000000003,"balance3":23130,"income3":1156.5,"balance4":17246,"income4":862.3000000000001,"balance5":9879,"income5":493.95000000000005,"balance6":148176,"income6":7408.8,"balance7":90094,"income7":4504.7,"balance8":219421,"income8":10971.050000000001,"balance9":14658,"income9":732.9000000000001,"balance10":68153,"income10":3407.65,"balance11":26323,"income11":1316.15,"balance12":28448,"income12":1422.4,"balance13":4089,"income13":204.45000000000002,"balance14":4075,"income14":203.75,"balance15":7335,"income15":366.75,"balance16":2102,"income16":105.10000000000001,"balance17":1723,"income17":86.15,"balance18":13661,"income18":683.0500000000001}]

    Does the column definitions also need to be in the same format?

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916

    Does the column definitions also need to be in the same format?

    Yes. See the docs for columns.data. And take a look at the Data Objects documentation.

    Kevin

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    Every example I see from your links shows the columns as an array of objects, just the way I have it.

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    What's different is {"data":"year","mData":"year"}. I did not add the "mData":"year" part, javascript did.

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916

    Maybe you can show us what you have configured now for columns.data. Please also post an example of your JSON response and any errors (alerts or browser console) that you are getting.

    mData is a legacy option name used in Datatables 1.9. It will work, backwards compatible, but the preference is to use the current naming convention. The current should be lower case where legacy names use camel case. You can see a conversion chart here:
    https://datatables.net/upgrade/1.10-convert

    Kevin

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    The columns variable is still the way it was in the original post. See "console log output" above.

    Here is the latest code:

                $('#incomeTable').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "ajax": {
                        "url": "/RetirePlan/getAccountsW.php",
                        "dataSrc": function(json) {
                            var row = {'year': '2018'};
                            console.log("json.data.length="+json.data.length);
                            for (var i=0; i<json.data.length; i++) {
                                var balance = Number(json.data[i].Value);
                                var income = balance * 0.05;
    
                                //create column definitions
                                var col1 = {};
                                col1.data = 'balance' +i;
                                var col1Name = col1.data;
                                columns.push(col1);
                                var col2 = {};
                                col2.data = 'income' +i;
                                var col2Name = col2.data;
                                columns.push(col2);
    
                                //create corresponding column data
                                row[col1Name] = balance;
                                row[col2Name] = income;
                            }
                            data2.push(row);
                            console.log("columns=" +JSON.stringify(columns));
                            console.log("data2=" +JSON.stringify(data2));
                            return data2;               
                        }
                    },
                    "columns": columns
                });
    

    There are no errors shown in the chrome console, no alerts, but only "2018" shows up in the table.

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916
    Answer ✓

    Well, the problem is your columns are being built using this:
    var columns = [{'data': 'year'}]; //column definitions

    Its not using what you are building in the ajax call. The reason is the columns are built before the ajax response arrives so its using what you defined in line 2 of your first post.

    Since you are using server side processing you will either need to statically define the columns, like the examples. Or if you want dynamic columns you will need to use a separate ajax call to get the columns before initializing Datatables.

    Kevin

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916
    Answer ✓

    I had a couple minutes to build an example for you:
    http://live.datatables.net/qimukefe/1/edit

    This example isn't perfect since its using data supplied by datatables.net. The getDT() function has an ajax call that gets all the data from the server to build the columns. You don't want this. You will want an ajax URL that will simply return the desired columns. Once the column array is built then Datatables is initialized and gets the first page of data.

    Kevin

  • tekknowtekknow Posts: 13Questions: 2Answers: 0

    Oh wow, thanks so much Kevin! What a great example. I see what you mean now. Your example builds the datatable after the ajax returns. Tricky stuff!

This discussion has been closed.