带有来自ajax源

时间:2017-05-03 19:57:28

标签: jquery datatables

我正在尝试使用以下代码在数据表行中添加一个按钮:

$("#tbllandinfo").DataTable({
            searching: false,
            ordering: false,
            paging: false,
            bInfo: false,   
            ajax: {
                url: '/land_and_crops/id/' + $("#eid").val(),
                dataSrc: '', 
            },
            columns: [
                { data: 'ldacres' },            
                { data: 'ldyields' },           
                { data: 'cname' },
            { 
                sortable:false,
                defaultContent:  "<button class='btn btn-danger btn-delete form-control pull-right' dbcolid='ldid' dbcolidval= dbtable='land_det'>X</button>",
            }
        ]
    });

按钮显示但我遇到的问题是我需要将来自ajax JSON的ldid值放入按钮中的dbcolidval属性中。

每一行按钮都有自己的值。

示例数据:

 {"ldid":4,"ldentity":7,"ldacres":"5.00","ldyields":2,"ldcrop":5,"cname":"RICE"},

 {"ldid":7,"ldentity":7,"ldacres":"10.00","ldyields":1,"ldcrop":6,"cname":"MAIZE",

 {"ldid":6,"ldentity":7,"ldacres":"4.00","ldyields":1,"ldcrop":3,"cname":"CORN"}

1 个答案:

答案 0 :(得分:2)

使用render而不是defaultContent。请参阅此处讨论的内容:(https://datatables.net/forums/discussion/23649/how-do-i-access-columns-data-inside-of-defaultcontent