2

My datatable loads perfectly except the columndefs do not work. Anybody got a clue? Please help. I just want to add a click event to every cell in column 1. I get no errors either. It works in this example on the end column...https://datatables.net/examples/ajax/null_data_source.html

var table =  $mytable.DataTable( {            
            "serverSide": true,
            "ajax": {
                "url": url_string,
                "cache": true,
                "columnDefs": [ 
                {"targets": 1,"data": null,"defaultContent": "<button>Select Image ID</button>"} ,  

                ]
            },
        });

1 Answer 1

0

Found a great post on stack overflow that really helped. and changed it to suit me this is the post Edit jQuery Datatable fields

And here is what I have working for me. I was concentrating too much on the API and less on just Jquery. The trick was execute the jquery after "drawCallback":

Credit to @Jeromy French

var table =  $spr_cnt_tbl.DataTable( {            
            "serverSide": true,
            "ajax": {
                "url": url_string,
                "cache": true,
                "columnDefs": [ 
                {"targets": 1,"data": null,"defaultContent": "<button>Select Image ID</button>"} ,  

                ]
            },
            "drawCallback": function( settings ) {
            apply_label();
            }
        });

        var apply_label=function(){
        $spr_cnt_tbl.find("td:nth-child(2)").not(':has(.label)').each(function(){
        if( this.innerHTML===""){
            $(this).wrapInner("<button class=btn btn-success id='sel_img' type='button'>Select Image</button>");
        }
        else {
            $(this).wrapInner('<span class="label label-success"></span>');
        }
        });
        };
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.