How to insert the ID of the JSON Response in the button of the last collum?

How to insert the ID of the JSON Response in the button of the last collum?

User123456User123456 Posts: 57Questions: 15Answers: 0
edited July 2017 in Free community support

I am doing server side activation, and I need to create 3 buttons in the last collum, simple CRUD buttons like this:

Usually their href is for the trash deleteX.php?cod=ID and getSomething.php?cod=ID.

I alredy did them and they appear okay and fine in the last collum with pure jQuery:

$(document).ready(function() {
                $('#companyTable').DataTable({
                    "processing": true,
                    "serverSide": true,
                    "ajax": "lib/server_processing.php",

                    "columns": [
                                { "data": 0,
                   "render": function ( data, type, row ) {
                                  var text = "";
                                  if (type == "display") {
                                    if (data == "1") {
                                       text = "<i class='ace-icon fa fa-circle green'></i>";
                                    } else {
                                       text = "<i class='ace-icon fa fa-circle red'></i>";
                                    }
                                    data = text
                                  }
                                  return data;
                     },
                  },
                                { "data": 1 },
                                { "data": 2 },
                                { "data": 3 },
                                { "data": 4 },
                                { "data": 5 },
                                { "data": 6 }
                            ],

                    "order": [ 1, "asc" ],

                    "columnDefs": [ 
                        { "orderable": false, "targets": 0 },
                        { "orderable": false, "targets": 7 },
                    {
                          "targets": 7,
                          "data": "",
                          "render": function(data, type, full){
                            return '<a data-toggle="modal" data-target="#infoModal" data-id="" id="getCompany" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company="><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';
                          }} ],

                    "sScrollX": "100%",
                    "sScrollXInner": "103%",
                });
            });

Howerever in a JSON like this:

["0", "FooBarBaz", "42.875.000/1486-83", "13630-275", "Rua Sofia Levi", "Aiur", "Vila Industrial", "4"]

How can I get the last position (in this case number "4" which is the ID), and put inside the button?

This question has an accepted answers - jump to answer

Answers

  • gyrocodegyrocode Posts: 126Questions: 6Answers: 30
    edited July 2017 Answer ✓

    Use third argument full for columns.render function to get access to full data for the row. Your id would be in full[7].

    For example:

    "render": function(data, type, full){
       return '<a data-toggle="modal" data-target="#infoModal" data-id="" id="getCompany" class="blue"><i class="ace-icon fa fa-search-plus bigger-130"></i></a> <a class="red" href="deleteCompany.php?id_Company=' + full[7] + '"><i class="ace-icon fa fa-trash-o bigger-130"></i></a> <a class="orange" data-id="" id="blockCompany"><i class="ace-icon fa fa-eye-slash bigger-130"></i></a>';
    }
    

    See more articles about jQuery DataTables on gyrocode.com.

  • User123456User123456 Posts: 57Questions: 15Answers: 0
    edited July 2017

    It's not working @gyrocode:

    Uncaught SyntaxError: Unexpected string

    I think it's beacuse of the simple '. Do I need to escape these '?

  • gyrocodegyrocode Posts: 126Questions: 6Answers: 30

    You're right, I corrected the answer.

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    This is a very efficient forum, it's always helping me, thanks for the help.

  • gyrocodegyrocode Posts: 126Questions: 6Answers: 30

    @User123456, great username by the way :smile: , had a tough time selecting more meaningful one?

  • User123456User123456 Posts: 57Questions: 15Answers: 0

    Yes, it was very hard for me to choose between, User1234, User12345 and User123456.

This discussion has been closed.