Column without construct

Column without construct

Maksim250698Maksim250698 Posts: 3Questions: 1Answers: 0

if have code:
var table = $('#example').DataTable( {
ajax: {
url: "url"
},
columns: [
{ data: "col" },
{ data: "users.last_name" },
{ data: "users.phone" },
],
select: true
} );

how i can write this withount construct?
i try :
var table = $('#example').DataTable();
table.ajax.url("https://api.myjson.com/bins/1d6rqo");
table.columns([{ data: 'col' },{ data: 'users.last_name' },{ data: 'users.phone' }]);

but it doesn't work..

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,231Questions: 26Answers: 4,929
    Answer ✓

    how i can write this withount construct?

    Are you asking to dynamically build the options without using the passing them in the Datatables initialization?

    There are some options you can change through the API like ajax.url(). There are many that you can't change like columns. They need to be set during initialization.

    What problem are you trying to solve?

    Kevin

  • Maksim250698Maksim250698 Posts: 3Questions: 1Answers: 0

    I am trying to add static row to the top of the table, and other data i try to load from json.How i can do this?
    var table = $('#example').DataTable( {
    ajax: {
    url: "https://api.myjson.com/bins/1d6rqo"
    },
    columns: [
    { data: "col" },
    { data: "users.last_name" },
    { data: "users.phone" },
    ],
    select: true
    } );

    var row = table.rows.add( [ {
    "col": "Tiger Nixon",
    "Last name": "System Architect",
    "Phone #": "$3,120",
    }, {
    "col": "Garrett Winters",
    "Last name": "Director",
    "Phone #": "$5,300",
    } ] )
    .draw();

  • kthorngrenkthorngren Posts: 21,231Questions: 26Answers: 4,929
    edited April 2019 Answer ✓

    I am trying to add static row to the top of the table

    The Sorting with absolute positioned data blog discusses how to position specific rows to the top of the table.

    other data i try to load from json.How i can do this?

    Not sure I understand the question. When Datatables loads it will send an ajax request to the URL you specify and, assuming the JSON is the correct format, will load the data into the table. Is this working for you?

    It doesn't look like the data structure you have for your rows.add() matches what you have defined in the Datatable. You have:

    columns: [
    { data: "col" },
    { data: "users.last_name" },
    { data: "users.phone" },
    ],
    

    But the structure you are adding is:

    {
    "col": "Tiger Nixon",
    "Last name": "System Architect",
    "Phone #": "$3,120",
    },
    

    To match the Datatables config you would need this structure:

    {
    "col": "Tiger Nixon",
    "users": {
        "Last name": "System Architect",
        "Phone #": "$3,120",
     }
    },
    

    Kevin

  • Maksim250698Maksim250698 Posts: 3Questions: 1Answers: 0

    I want to add new row at top of table.I want the new row to be the first in the table
    var row = table.rows.add( [ {
    "col": "Tiger Nixon",
    "Last name": "System Architect",
    "Phone #": "$3,120",
    }, {
    "col": "Garrett Winters",
    "Last name": "Director",
    "Phone #": "$5,300",
    } ] )
    .draw();

    this code add row at bottom of the table, how i can move this row to top?

  • kthorngrenkthorngren Posts: 21,231Questions: 26Answers: 4,929

    this code add row at bottom of the table, how i can move this row to top?

    Did you read the Sorting with absolute positioned data doc I linked to? It explains how to do this.

    Kevin

This discussion has been closed.