DataTables table link

0

I need to create a link where I have the input name in the table using the DataTables, this link should call a javascript function. The problem is that when the table is loaded it runs the script for each row of the table, as if all the links were being clicked.

<script>
    $(function () {
        $("#insumos").dataTable({
            "bServerSide": true,
            "sAjaxSource": "/Insumo/BuscarInsumosDataTable",
            "bProcessing": true,
            "language": {
                "url": "/DataTables/Traducao"
            },
            "aoColumns":
                [
                    {
                        "sName": "InsumoId",
                        "mData": "InsumoId"
                    },
                    {
                        "sName": "Nome",
                        "mData": "Nome",
                        "render": function ( data, type, full, meta ) {
                            return '<a href="onclick=' + selecionarInsumo(data) + '">' + data + '</a>';
                        }
                    }
                ]
        });
    })

    function selecionarInsumo(nome) {
        alert("deu certo, " + nome)
    }
</script>
    
asked by anonymous 06.04.2016 / 22:40

1 answer

1

I was "simulating the clicks" because filling the table was invoking the function.

When you are writing: < return '<a href="onclick=' + selecionarInsumo(data) + '">' + data + '</a>';

SelectingInsumo (date) is a javascript function and therefore will run immediately.

The first constraint was found in the href attribute. It is started but is not finalized so it is necessary to change from href=" href=" # " The jsfiddle with the changes .: For some reason the selectInsumo is not identified in the jsfiddle, but I tested it with a page and it ran 100%.

    
08.04.2016 / 16:58