数据表columnDefs不工作:jquery / javascript

datatables columnDefs not working: jquery / javascript

本文关键字:jquery javascript 工作 columnDefs 数据表      更新时间:2023-09-26

除了columndefs不能工作外,我的数据表加载得很好。有人知道吗?请帮助。我只想为列1中的每个单元格添加一个click事件。我也没有得到错误。它在本例中用于结束列…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>"} ,  
                ]
            },
        });

找到一篇很棒的关于堆栈溢出的文章,确实很有帮助。并改变它,以适合我这是后编辑jQuery数据表字段

这是我的工作。我过于关注API而不是Jquery。窍门是在"drawCallback"之后执行jquery:

来源:@ jerome 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>');
        }
        });
        };
    });
});