How to add different hyperlinks on different columns in a row?

How to add different hyperlinks on different columns in a row?

GrumbledoreGrumbledore Posts: 2Questions: 1Answers: 0

Hi there,

1st of all sorry for my bad language. sometimes it's troublesome for me to get to the point.

I am working on a small community based project for an android game called Hackerz. In that game you have to gather ip adresses from other players and loot their miners.

The german community for this game created a website, where users could add ip adresses of other players to the database.

I am currently working on redoing the whole page in codeigniter since the original developer made a lot of mistakes (like 11 mysql queries while looping through all the rows in the database. resulting in over 20k queries in a single page call). I managed to fix that and started to develope a new version. the new version is no where ready to go live so i won't be able to provide a live link.

For the new version i switched from tablesorter to datatables since datatables is better documented and easier to use in general. kudos to you and your team allan.

i am not that familiar with javascript so there might be somethings i won't understand directly.

OK enough of that this is my basic setup:

Codeigniter v3.1.4.
DataTables-1.10.15 with Bootstrap
jQuery-2.2.4
Serverside processing with pagination and sorting (working)

The image attached shows the table.

I want to add hyperlinks to all columns wich provide call different .php sites for data processing.

Fav
- If value of cell is 0 button for favourite is shown. If user clicks on it that ip will be saved as favourite in the database
- If value of cell is 1 button for unfavourite is shown. If user clicks on it that ip will be deleted from favourites.

Melden
- If user has not reported that ip (row[9] not visible) report button is shown.
- If user has reported that ip unreport button is shown. call to .php for reporting/unreporting depending what the value was

Edit
- Just a plain hyperlink for editing that specific row button shown as well.

regarding the issue i am using this code:

<script type="text/javascript">
    var rep = "<?php echo $_SESSION['Rep'];?>"

    $(document).ready(function(){
    $('#myTable').DataTable({
        "pageLength" : 10,
        "processing": true,

        "serverSide": true,
        "order": [
            [1, "asc" ]
        ],
        "columnDefs": [ {
            "targets": 'nosort',
            "orderable": false,
            targets: 7,
            data: "null",
            render: function (data, type, row, meta) {
                if (type === 'display') {
                    if (row[7] === 0) {
                        return '<a id="' + row[8] + '" onclick="fav.php?' + row[8] + '" data-placement="top" data-toggle="tooltip" title="Favourite" class="btn btn-warning btn-xs"><span class="glyphicon glyphicon-star-empty"></span></a>'
                    }
                    else {
                        return '<a id="' + row[8] + '" onclick="unfav.php?' + row[8] + '" data-placement="top" data-toggle="tooltip" title="UnFavourite" class="btn btn-success btn-xs"><span class="glyphicon glyphicon glyphicon-star"></span></a>'
                    }
                }


            },
            targets: 8,
            data: "null",
            render: function (data, type, row, meta) {
                if (type === 'display') {


                    if (row[9] === 0) {
                        data = '<a id="' + row[8] + '" onclick="report.php?' + row[8] + '" data-placement="top" data-toggle="tooltip" title="Report" class="btn btn-warning btn-xs"><span class="glyphicon glyphicon-ok"></span></a>'
                    }
                    else {
                        data = '<a id="' + row[8] + '" onclick="unreport.php?' + row[8] + '" data-placement="top" data-toggle="tooltip" title="UnReport" class="btn btn-success btn-xs"><span class="glyphicon glyphicon glyphicon-alert"></span></a>'
                    }
                }
                return data;
            }
        } ],

        "ajax": {
            url : "<?php echo site_url("table/ips_page") ?>",
            type : 'GET'
        },

        "createdRow": function( row, data, dataIndex ) {
            if ( data[2] > rep * 0.75 ) {
                $(row).addClass('info');

            }
            if ( data[2] < rep * 0.25 ) {
                $(row).addClass('warning');

            }

        },




        "dom": "<'row'<'col-sm-6'l><'col-sm-6'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row text-center'<'col-md-8 text-center'p>>",
    
    
    
    

});
 

});

</script>



As you can see in the provided screenshot column Fav doesnt show the button just the regular value. column Melden does show it like it should. if i specify target: [7,8], for one of them it does work on both. so what am i doing wrong here?

Thx a lot in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin
    Answer ✓

    You've only got one object in your array. So the second set of parameter is overwriting the first. For example:

    var i;
    i = 0;
    i = 1;
    

    i can't be both 0 and 1 - it is now 1!

    Just use two different objects in your columnDefs array - one for column index 7 and one for index 8.

    Allan

  • GrumbledoreGrumbledore Posts: 2Questions: 1Answers: 0

    Thx alot Allan. i wasted hours searching for it -.- and everytime i didnt noticed i missed a closing and an opening bracket. jeez.

    sorry for the stupid question. i was just blind -.-

    keep up the great work :D

This discussion has been closed.