Question about invalidating and re-drawing rows customized via createdRow option

Question about invalidating and re-drawing rows customized via createdRow option

jLinuxjLinux Posts: 981Questions: 73Answers: 75
edited October 2015 in Free community support

I have a DataTable instance that is loaded via AJAX, and using the createdRow, the values of the td cells will be altered into links, as well as some attributes being added to both the tr and td. When the value gets modified, then I need to row().invalidate() the row.

The question I had, was how can I have all of the JS thats ran via the createdRow, run again on the row that just got invalidated?

Heres the jQuery, just to give you a better idea of what im working with :

function do_datatable(data_src){
    // Static columns
    var columns = [
        { data: 'creator' },
        { data: 'created' },
        { data: 'modifier' },
        { data: 'modified' }
    ];
    
    // Compile the dynamic columns
    $.each(data_src.partition.visible_fields, function(f_name, f_data){
        columns.push({data: f_name});
    });

    // Initiate the DT
    $('#data-table').DataTable( {
        //ajax: {url: "/REST/partition/assets/partition_id/1/format/json", dataSrc: "assets"},
        data: data_src.assets,
        deferRender: true,
        columns: columns,
        initComplete: function( settings, json ) {
            // Initialize the Editable, hand down the DT instance for event handling
            assets.editable(this);
        },
        createdRow: function( row, data, dataIndex ) {
            var f_name, field, type, td, editable, truncated;

            // Adding attributes to the <TR>
            $(row).attr('data-row-id', data.asset_id);

            $.each(columns, function(k,c){
                f_name = c.data;
                td = $( row ).find( 'td' ).eq( k );

                // Adding attributes to the <TD>
                td.attr('data-field', f_name);

                editable = $( '<a>', {
                    'id': f_name,
                    'data-title': "Enter " + field.name,
                    'data-value': td.text(),
                    'class': 'string-edit',
                    'data-field-type': type,
                    'text': td.text(),
                    'placeholder': field.placeholder,
                    'href': '#'
                } );

                td.html(editable);
            });
        }
    } );
}

Thanks!.. Again

P.S. I know I ask a lot of questions, im just heavily relying on DataTables for this Open Source project, I try to pay back by helping in the forums myself when I can.

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited October 2015

    I was playing around with both the Javascript data source (local JSON), and AJAX data source (As you can probably see from the ajax setting commented out), and I found that if I use AJAX, and run the ajax.reload(), then I dont need to invalidate it, and it runs everything in the createdRow.

    So I guess that solves it, but is there a way to run ajax.reload() on just specific rows? If I have a ton of rows, I dont want it to try and update all of them.

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    Answer ✓

    The question I had, was how can I have all of the JS thats ran via the createdRow, run again on the row that just got invalidated?

    Currently you need to have it in a function that is called both by createdRow and by whatever is doing the update. createdRow will only ever run once in the life time or a row and there is currently no update event (something that will be added int he next major version).

    So I guess that solves it, but is there a way to run ajax.reload() on just specific rows? If I have a ton of rows, I dont want it to try and update all of them.

    No - you would need to make your own Ajax call and then use rows().remove() and rows.add() to delete and then add the rows required - which is basically what ajax.reload() does, just for all rows.

    Allan

This discussion has been closed.