首页 > 解决方案 > 数据表数字转换为八进制

问题描述

在数据表中,我面临一个挑战

我有一些前导零(0461)的数字。数据在数据表中正确显示,但是当我单击该数字并传入一种方法时,它会转换为其他数字,可能是八进制数

例如 0461 转换为 86。

请提出一些建议

我的数据表代码是

var table = $('#example').DataTable({
            processing : true,
            serverSide : true,
            pageLength : 10,
            ajax : {
                url : "/codes",
                data : function(data) {
                }
            },
            columns : [ {
                "data" : "code",
                "name" : "Code",
                "title" : "Code",
                "render" : function(data) {
                    // From here I am passing 0461
                    return '<a class="link" onclick="return searchCode('+data+');">' + data + '</a>';
                }
            } ],
            columnDefs : [ {
                "targets" : [ 1],
                "searchable" : false
            }, {
                "targets" : [ 0],
                "orderable" : true
            } ]
        });


// But here getting 86      
function searchCode(code) {
        console.log(code);
    }       

在数据表中的数据如下所示

在此处输入图像描述

标签: javascriptdatatables

解决方案


找到下面的解决方案

var table = $('#example').DataTable({
            processing : true,
            serverSide : true,
            pageLength : 10,
            ajax : {
                url : "/codes",
                data : function(data) {
                }
            },
            columns : [ {
                "data" : "code",
                "name" : "Code",
                "title" : "Code",
                "render" : function(data) {
                    // From here passing this instead of data
                    return '<a class="link" onclick="return searchCode(this);">' + data + '</a>';
                }
            } ],
            columnDefs : [ {
                "targets" : [ 1],
                "searchable" : false
            }, {
                "targets" : [ 0],
                "orderable" : true
            } ]
        });


// It will print what we will pass from above method    
function searchCode(code) {
        console.log(code.text); // print 0461
    }     

推荐阅读