Editor.add() pushes the element to the head instead of appends to the end

Editor.add() pushes the element to the head instead of appends to the end

zipperzipper Posts: 35Questions: 9Answers: 0

This makes the order of the fields reversed.
[code]
var editor = new $.fn.dataTable.Editor({
table: '#example'
});

    for (var i = 0; i<json.tb_titles.length; i++){
      var predecessor = (i == 0) ? null : json.tb_titles[i-1];
      editor.add({
          label: json.tb_titles[i],
          name: json.tb_field_names[i]
        },predecessor);
    }

[/code]

Replies

  • allanallan Posts: 61,721Questions: 1Answers: 10,108 Site admin

    Could you send me the contents of your json object so I can attempt to recreate this please?

    Thanks,
    Allan

  • zipperzipper Posts: 35Questions: 9Answers: 0
    edited March 2017

    The json object:

    {
        tb_titles: ['ID',
            'Product Name',
            'Catalogue',
            'Description',
            'Create Time',
            'Status'
        ],
        tb_field_names: ['id',
            'name',
            'catalogue',
            'description',
            'create_time',
            'status'
        ],
        tb_data: [
            [1,
                'as_o_agent',
                'nibiru',
                'Act as orionation agent',
                '2017-03-02T12:20:45.000Z',
                'active'
            ],
            [2,
                'as_t_agent',
                'nibiru',
                'Act as termination agent',
                '2017-03-05T03:51:49.000Z',
                'active'
            ]
        ]
    }
    

    I have to do bellow to fix:

    var r_count = json.tb_titles.length;
    for (var i = r_count; i > 0;) {
      var predecessor = (i == r_count) ? null : json.tb_titles[i];
      editor.add({
        label: json.tb_titles[--i],
        name: json.tb_field_names[i]
      }, predecessor);
    }
    
  • allanallan Posts: 61,721Questions: 1Answers: 10,108 Site admin

    Hi,

    Sorry for the long delay in getting back to you about this. The problem is in how the predecessor variable is being set - it should be using json.tb_field_names. As the add() documentation notes it is the field.name that should be given as the predecessor.

    var predecessor = (i == r_count) ? null : json.tb_file_names[i];
    

    allows it to work as expected.

    Allan

  • zipperzipper Posts: 35Questions: 9Answers: 0

    Yes sir, I realized that as well. thank you very much.

This discussion has been closed.