【问题标题】:datatables columnDefs not working: jquery / javascript数据表 columnDefs 不起作用:jquery / javascript
【发布时间】:2015-08-25 16:34:15
【问题描述】:

我的数据表加载完美,但 columndefs 不起作用。 有人有线索吗? 请帮忙。我只想为第 1 列中的每个单元格添加一个点击事件。我也没有收到任何错误。 它在这个示例中的最后一列中起作用...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>"} ,  

                ]
            },
        });

【问题讨论】:

    标签: javascript jquery datatables-1.10


    【解决方案1】:

    找到了一篇关于堆栈溢出的很棒的帖子,确实很有帮助。 并将其更改为适合我这是帖子Edit jQuery Datatable fields

    这就是我为我工作的。我过于关注 API 而不是只关注 Jquery。诀窍是在“drawCallback”之后执行 jquery:

    感谢@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>');
            }
            });
            };
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 2017-10-27
      相关资源
      最近更新 更多