set columns name

set columns name

marco.rudello@gmail.commarco.rudello@gmail.com Posts: 12Questions: 4Answers: 0

Hi,
Its possible an example to set columns name by array?
eg: arcol = [colonna1,colonna2,colonna3.....,colonna4]
for (I=o;i < arcol.length;i++) { column.name = arcol[I]}
thanks

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,803Questions: 1Answers: 10,515 Site admin

    I'm afraid I don't really understand your question. The columns.name documentation contains an example showing how that property can be used.

    The column name is only useful for use with the column() selector method.

    Allan

  • marco.rudello@gmail.commarco.rudello@gmail.com Posts: 12Questions: 4Answers: 0

    Sorry for my English writing, I try to explain with example. In your doc if possible fill grid by this instruction:
    $(document).ready(function() {
    $('#example'String).DataTable( {
    data: dataSet,
    columns: [
    { title: "Name" },
    { title: "Position"},
    { title: "Office" },
    { title: "Extn."},
    { title: "Start date"},
    { title: "Salary"}
    ]
    } );
    } );
    now in this case we kown how many columns and the columns name, but if I have array of data where the first row contains the columns name and the other row my data how declare the precedent instruction?
    I hope you understand me....... sorry

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

    Can you please paste what your array of data looks like. I think I follow you but need to be sure.

  • marco.rudello@gmail.commarco.rudello@gmail.com Posts: 12Questions: 4Answers: 0
    edited June 2016

    var ar1 = new Array(50);
    ar1[0,0] = "NAME";
    ar1[0,1] = "COGNOME";
    ar1[0,2] = "DATA ASSUNZIONE";
    ar1[0,3] = "DATA CESSAZIONE";
    // do other
    ar1[1,0] = "MARCO";
    ar1[1,1] = "ROSSI";
    ar1[1,2] = "01/06/2016";
    ar1[1,3] = null;
    // other ....
    ar1[10,0] = "PAOLO";
    ar1[10,1] = "FLINSTON"
    ar1[10,2] = "01/05/2010";
    ar1[10,3]= null;

    now this bidimensional array contains on first row [0,x] name of columns and the other row data. How bind this array with datatable?
    remember this array is dynamic and I don't know how many columns and data are there

    be patience
    thanks

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

    Instead of a bi-dimensional array, could you make is as follows

    ar1 = {
       'headers': ["Name", "Cognome", etc]
       'data': [
           {
                 'first_name': 'Marco',
                 'last_name': 'Rossi',
                 etc.
           }
       ]
    }
    

    Use ar1.headers to append necessary th elements to table.

    ar1.each(function() {
       $('<th>'+this+'</th>').appendTo(table thead);
    })
    

    Reason i bring it up like that is you could change your DataTable config to following

    $('#example'String).DataTable( {
       data: ar1.data,
       columns: [
         { data: "first_name" },
         { data: "last_name"},
         etc.
       ]
    } );
    
  • allanallan Posts: 63,803Questions: 1Answers: 10,515 Site admin
    Answer ✓

    Yup - jr42.gordon's method is absolutely valid and a good option if you can modify the JSON format.

    If you can't you'd need to preprocess the array using a for loop as you originally suggested in your code in the first post. Are you having difficultly doing that? If so, can you post the code you have tried so far.

    Allan

  • marco.rudello@gmail.commarco.rudello@gmail.com Posts: 12Questions: 4Answers: 0

    Hi Allan,
    I you can send me example for array with using for loop
    Thanks very match

  • allanallan Posts: 63,803Questions: 1Answers: 10,515 Site admin
    Answer ✓

    Certainly - this would fall under the DataTables support options.

    However, if you prefer to do it yourself, I would suggest you use Array.prototype.shift() to get the first "row" from your array and then use a trivial for loop like you have above that sets an object with a name property.

    Allan

This discussion has been closed.