DOM, Can't set object names for row

DOM, Can't set object names for row

1DMF1DMF Posts: 16Questions: 5Answers: 0
edited May 2016 in Free community support

Hi,

I seem to be going round in circles trying to set names for the columns so .row() returns an object and not an array.

I have this HTML

<table id="uploads">
            <thead>
                <tr>
                    <th>DocID</th>
                    <th>Date</th>
                    <th>Type</th>
                    <th>Description</th>
                    <th>User</th>
                </tr>
            </thead>
            <tbody>                            
            <tmpl_loop name='cpd_uploads'>
                <tr>
                    <td><tmpl_var name='DocID'></td>
                    <td><tmpl_var name='UDate'></td>                    
                    <td><tmpl_var name='File_Ext'></td>
                    <td><tmpl_var name='Description'></td>
                    <td><tmpl_var name='User_Name'></td>
                </tr>
            </tmpl_loop>
            </tbody>
        </table>

but when I try to initialise it with...

UPLOAD = $('#uploads').DataTable({        
    "sDom": 'R<"H"fr>t<"F"ip>',             
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "iDisplayLength": 5,
    "columns": [
        { "data": "DocID" },
        { "data": "UDate" },
        { "data": "File_Ext" },
        { "data": "Description" },
        { "data": "User_Name" }
    ],
    "aoColumnDefs": [
        {"sType":"uk_date", "aTargets":[1], "sWidth":"90px",},
        {"sWidth":"90px",  "aTargets":[2]},
        {"sWidth":"200px", "aTargets":[4]}  
    ],      
    "order": [[ 1, "desc" ]],
    "oLanguage": {
        "sEmptyTable": "You do not have any uploaded documents. Use form below to add one."
    },

}); 

I get an error (warning)...

DataTables warning: table id=uploads - Requested unknown parameter 'DocID' for row 0.

If I leave out the columns property, it works fine only I get an array not an object.

I don't understand what I'm doing wrong?

This question has an accepted answers - jump to answer

Answers

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    edited May 2016

    "data" corresponds to a variable in the passed object. So each row object should have variables: DocID, UDate, File_Ext, Description, User_Name.

    Can you please paste the source data so we can see what it looks like?

  • 1DMF1DMF Posts: 16Questions: 5Answers: 0
    edited May 2016

    I did paste the source data it's a DOM table.

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    ahh ok well if you want to use objects, then you have to use https://datatables.net/reference/api/row.add() to add an object with the aforementioned variables. Then when you use https://datatables.net/reference/api/row().data() , it will return the object used.

  • 1DMF1DMF Posts: 16Questions: 5Answers: 0
    edited May 2016

    Sorry I still don't understand. I don't want to add any data, the template has already does that.

    The datatables object is populated with the data via a normal HTML DOM table.

    But I want row() to return {object} NOT [array].

    The docs say : https://datatables.net/examples/advanced_init/object_dom_read.html

    But when I use the example syntax I'm getting that error?

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49
    edited May 2016

    MY bad i misunderstood what you were asking.

    First, that page mentions "Please note that this feature requires DataTables 1.10.3 or newer." So are you? If so, try removing everything except the "columns" declaration so that your code matches the example. If that works then begin re-adding each option until you figure out which one broke it.

    Once you have everything working, to get the data you have some options:

    UPLOAD.data()   // returns array of objects in order based on initial HTML makeup
    UPLOAD.rows().data()   // returns array of objects in order based on current table sort
    

    I believe I got an example working http://live.datatables.net/qoqemata/1/edit?html,js,console,output

  • allanallan Posts: 63,794Questions: 1Answers: 10,514 Site admin
    Answer ✓

    Use columns.data. If it is a DOM sourced table and you use columns.data to assign object names, DataTables will write them into those object names rather than an array. Example.

    Allan

This discussion has been closed.