Add buttons in table rows

1

I'm using: link and with it I'm able to create the table and load the json data. The problem is that I'm not able to add another column with edit and delete actions, for example. Where would be 2 buttons. The back is with Java.

    
asked by anonymous 28.06.2017 / 20:40

1 answer

2

Do this:

$('#my-final-table').dynatable({
dataset: {
    records:
            [
                {
                    "band": "Weezer",
                    "song": "El Scorcho",
                    "editar":"<button onclick='javascript:editar();'>editar</button>",
                    "excluir":"<button onclick='javascript:excluir();'>excluir</button>"
                },
                {
                    "band": "Chevelle",
                    "song": "Family System",
                    "editar":"<button onclick='javascript:editar();'>editar</button>",
                    "excluir":"<button onclick='javascript:excluir();'>excluir</button>"
                }
            ]
}
});

If you are getting json from an ajax, where json is generated you will have to mount the elements (HTML buttons) together in json as the example above, so you only have to implement the javascript part to edit / delete , if you have any doubts about this part, just flag.

EDIT:

I did not ask but as in the site does not have an easy example to directly manipulate the json that is already in use by the plugin I found it necessary to post how to do to delete an item, based on this example the edit would be in the same line of reasoning, which is to edit the json, deleting or changing a record, then load it again in the plugin follows example:

var MeuArray = 
        [
            {            
                "idx":0,
                "band": "Weezer",
                "song": "El Scorcho",
                "editar": "<button onclick='javascript:editar(0);'>editar</button>",
                "excluir": "<button onclick='javascript:excluir(0);'>excluir</button>"
            },
            {   
                "idx":1,
                "band": "Chevelle",
                "song": "Family System",
                "editar": "<button onclick='javascript:editar(1);'>editar</button>",
                "excluir": "<button onclick='javascript:excluir(1);'>excluir</button>"
            }
        ];
$('#my-final-table').dynatable({ dataset: { records: MeuArray } });

function excluir(index){
    for(var i=0; i<MeuArray.length; i++){
        var obj = MeuArray[i];
        if(obj.idx == index){ 
            MeuArray.splice(i, 1);            
            break;
        }        
    }    
    var dynatable = $('#my-final-table').data('dynatable');
    dynatable.settings.dataset.originalRecords = MeuArray;
    dynatable.process();    
}
    
30.06.2017 / 13:17