Column name alias error.

Column name alias error.

scoobyscooby Posts: 6Questions: 1Answers: 0
edited September 2014 in Free community support

I am using datatables v1.10.2 server side code and am joining multiple tables. All is well until I give a column name an alias name. The reason I am giving the columns alias names is because I have same column names across multiple tables. The query still collects all the data just fine but datatables is not able to read the data back into the table.

I am receiving the following error:

DataTables warning: table id=DataTables_Table_0 - Requested unknown parameter 'table1.company_name AS 'name1'' for row 0. For more information about this error, please see http://datatables.net/tn/4

Unfortunately the link did not provide me with any solutions for this issue.

Here is the initialization code that I am using:

$('#my-table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": {
        "type": "POST",
        "dataType": 'json',
        "url": '/server.php'
    },
    "columns": [
        { 
            "title": "Company Name 1",
            "data": "table1.company_name AS 'name1'" 
        },{ 
            "title": "Company Name 2",
            "data": "table2.company_name AS 'name1'" 
        }
    ]
});

Is there something I can do within the server.php script so that datatables recognizes the parameter "table1.company_name AS 'name1"?

If I have not provided enough information please let me know.

Thanks to all!

Replies

  • allanallan Posts: 61,934Questions: 1Answers: 10,155 Site admin
    edited September 2014

    "data": "table1.company_name AS 'name1'"

    I don't think that is in the documentation anywhere - and if it is could you point it out to me so I can remove it? Aliases are not supported in the data option and likely never will be.

    What does your returned JSON data look like? Basically you want to use the columns.data option to set the property name of the data you want to use for that column.

    Allan

  • scoobyscooby Posts: 6Questions: 1Answers: 0
    edited September 2014

    Yeah I could not find anything on this within the documentation anywhere so that's why I resorted to the forums.

    On another note, I did remedy my situation by setting the columns.data back to normal and then simply adding a small tweak to the server.php code.

    So my datatables initialization code was like so:

    $('#my-table').DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": {
            "type": "POST",
            "dataType": 'json',
            "url": '/server.php'
        },
        "columns": [
            {
                "title": "Company Name 1",
                "data": "table1.company_name"
            },{
                "title": "Company Name 2",
                "data": "table2.company_name"
            }
        ]
    });
    

    And then when collecting all the columns within the server.php code I added a small check like so to add the column alias name:

    $cols = array();
    foreach ($columns as $column) {
        switch ($column['data']) {
            case 'table1.company_name':
                $cols[] = $column['data'] . " AS 'name1'";
                break;
            case 'table2.company_name':
                $cols[] = $column['data'] . " AS 'name2'";
                break;
            default:
                $cols[] = $column['data'];
                break;
        }
    }
    

    Then the rest of the script worked as it normally does..

    Thanks very much for the reply and confirming that Allan!

  • allanallan Posts: 61,934Questions: 1Answers: 10,155 Site admin

    Good to hear you got it working.

    Allan

This discussion has been closed.