首页 > 解决方案 > 使用编辑器数据表从记录中获取 id

问题描述

我正在使用数据表编辑器,我想创建一个自定义列来获取记录的 id。代码是:

editor = new $.fn.dataTable.Editor({
    ajax: "assets/dados/tabelaProfissionais.php",
    table: "#tabelaProfissionais",
    fields: [
        { label: "CPF:", name: "cpf", type: "mask", mask: "000.000.000-00" },
        { label: "Nome:", name: "nome" },
        { label: "Telefone:", name: "telefone", type: "mask", mask: "(00)00000-0000" },
        { label: "Endereço:", name: "endereco" },
        { label: "CNS:", name: "cns", type: "mask", mask: "000000000000000" },
        { label: "RG:", name: "rg" },
        { label: "Sexo:", name: "sexo", type: "select", options: [
            { label: "Feminino",  value: "F" },
            { label: "Masculino", value: "M" }
        ]},
        { label: "Conselho:", name: "conselho" },
        {
            label: "Image:",
            name: "imagem",
            type: "upload",
            display: function ( file_id ) {
                return '<img src="'+editor.file( 'files', file_id ).web_path+'"/>';
            },
            clearText: "Clear",
            noImageText: 'Em imagem'
        }
    ],

$('#tabelaProfissionais').DataTable({
    dom: "Bfrtip",
    ajax: {
        url: "assets/dados/tabelaProfissionais.php",
        type: "POST"
    },
    serverSide: true,
    columns: [
        { data: "cpf" },
        { data: "nome" },
        { data: null, defaultContent: '<a href="" class="btn btn-primary" style="background-color:#5079d7"><i class="fas fa-search-plus"></i>'+editor.field('nome').get()+'</a>', searchable: false }
    ],
    select: true,
    buttons: [
        { extend: "create", editor: editor  },
        { extend: "edit",   editor: editor },
    ]
});

我的自定义字段在{ data: null, defaultContent: '<a href="" class="btn btn-primary" style="background-color:#5079d7"><i class="fas fa-search-plus"></i>'+editor.field('nome').get()+'</a>', searchable: false }其中,我尝试在标签 a 上获取字段“nome”的值。

任何的想法?

标签: jquerydatatables

解决方案


我是巴西人。试试这个:

    {
            data: 'user_id',
            render: function ( data, type, row, meta ) {
                return '<a href="" class="btn btn-primary" style="background-color:#5079d7"><i class="fas fa-search-plus"></i>'+(data)+'</a>';
            }       
    }

所以会有ID,可以用作基础。如果它不起作用,请告诉我我能做什么,或者如果有疑问......


推荐阅读