Agregar valor a botón proveniente de un json

Agregar valor a botón proveniente de un json

JManzanoAJManzanoA Posts: 2Questions: 1Answers: 0
edited December 2021 in Buttons

Es la primer ocasión en que uso botones en datatables. Obtengo una serie de datos en un json y los agrego a mi tabla, la cual tiene tres columnas con botones. Mi pregunta es, como puedo agregar un valor definido en el json a un botón en específico y evaluar ese valor para modificar la apariencia del botón (color, texto).

Mi tabla está de la siguiente maneray quiero agregar el valor como parámetro "idc" a cada uno de los botones:

    tabla = $('#asProfessor').DataTable( {
            "iDisplayStart": 0,
            "iDisplayLength": 10,
            "bProcessing": true,
            "processing": true,
            "paging": true,
            "language": {
                "url": "/ruta/datatable-esMx.json"
            },
            "searching": { "regex": true },
            "lengthMenu": [ [5,10, 25, 50, 100, -1], [5,10, 25, 50, 100, "Todos"] ],
            "pageLength": 5,
            "data": datos,  
            "columns": [
                { "data": "key" },
                { "data": "exam" },
                { "data": "datetimeApp" },                      
                { "defaultContent": "<button id='btnActiva' class='btn btn-primary btn-block activar' idc='VALOR1_JSON(key)'><span><i class='fa fa-power-off'></i> Activa</span></button> " },
                { "defaultContent": "<button id='btnMenciones' class='btn btn-success btn-block' idc='VALOR2_JSON(exam)'><span><i class='fas fa-plus'></i> Menciones</span></button>" },
                { "defaultContent": "<button id='btnAplica' class='btn btn-warning btn-block' idc='VALOR3_JSON(datetimeApp)'><span><i class='fas fa-users'></i> Aplica</span></button>" }
            ]
    } )

¿Cómo puedo obtener y asignar el valor VALOR1_JSON(key) a mi primer botón y así sucesivamente?

De antemano agradezco el apoyo que me puedan brindar.

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    Answer ✓

    It seems you're getting the JSON before the table is initialisation, so it should be fairly straightforward. The text of a button is set with buttons.buttons.text, and you would set the colour with a class using buttons.buttons.className. So, before initialising the table, check your JSON and then set those twp properties accordingly,

    Colin

  • JManzanoAJManzanoA Posts: 2Questions: 1Answers: 0

    Gracias colin, me fue de mucha ayuda tu respuesta. Adicionalmente encontré la solución a mi mayor problema, asignar un dato del json a los botones de forma dinámica, lo cual conseguí de la siguiente manera:

    "columns": [
        { "data": "key" },
        { "data": "exam" },
        { "data": "datetimeApp" },  
        {
            "data": null,
            "bSortable": false,
            "mRender": function(data, type, value) {
                return '<button id="btnActivar" class="btn btn-default btn-block act_'+value["active"]+'" axn='+value["active"]+' idc='+value["key"]+'><span><i class="fa fa-power-off"></i></span></button>';
            }
        },
    
Sign In or Register to comment.