how to get column name from the table to use for href link

how to get column name from the table to use for href link

issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

I am using the following code to make a link for a new page:-
columnDefs: [
{
targets: [2],
render: function ( data, type, row, meta ) {
if(type === 'display'){
data = '<a target="_blank" href="link.php?name=' + row[2] +'">' + data + ' </a>';
}

                        return data;
                    },

                },

            ],  

i need to get the column name instead of data.

regards

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I'm not sure what you mean by the name. Where are you defining the name? Do you mean columns.name? If so, I don't see it in the above code.

    Allan

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    Hi Allan,
    Thank you for your response, i am using the following dynamic code.

    var data,
    tableName= '#Dynamic',
    columns,
    str,
    jqxhr = $.ajax('test.php')
    .done(function () {
    data = JSON.parse(jqxhr.responseText);

                // Iterate each column and print table headers for Datatables dynamic_Account_Analysis.php
                $.each(data.columns, function (k, colObj) {
                    str = '<th>'+'<h5><center>' + colObj.name + '</center></h5>'+'</th>';
                    $(str).appendTo(tableName+'>thead>tr');
                });
    
    
                        // Debug? console.log(data.columns[0]);
    
                $(tableName).dataTable({
                    dom: 'Bfrtip',
                    "data": data.data,
                    "Processing": true,
                    "ServerSide": true,
                    "Select": true,
                    "sPaginationType":"full_numbers",
                    "lengthMenu": [ [ 10, 25, 50, 100, -1], [ 10, 25, 50, 100, "All"] ],
                    "iDisplayLength": 50,
                    "stateSave": true,
                    "autoWidth": true,
                    "searching": true,
                    "sort": true,
                    "info": true,
                    "autoWidth": false,
                    "columns": data.columns,
                    "fnInitComplete": function () {
                        // Event handler to be fired when rendering is complete (Turn off Loading gif for example)
                        console.log('Datatable rendering complete');
                    },
    
                columnDefs: [
                    {
                        targets: [1],
                        render: function ( data, type, columns, meta ) {
                            if(type === 'display'){
                                data = '<a target="_blank" href="test.php?screen_name=' + screen_name +'&data_type=' + columns.name +'">' + data + ' </a>';
                            }
    
                            return data;
                        },
    
                    },
    
                ],                  
    
                buttons: [
                            {
                                extend: 'collection',
                                text: 'Export',
                                buttons: [
                                    'copy',
                                    'excel',
                                    'csv',
                                    'pdf',
                                    'print'
                                ]
                            }               
                    ]
                });
            })
            .fail(function (jqXHR, exception) {
                            var msg = '';
                            if (jqXHR.status === 0) {
                                msg = 'Not connect.\n Verify Network.';
                            } else if (jqXHR.status == 404) {
                                msg = 'Requested page not found. [404]';
                            } else if (jqXHR.status == 500) {
                                msg = 'Internal Server Error [500].';
                            } else if (exception === 'parsererror') {
                                msg = 'Requested JSON parse failed.';
                            } else if (exception === 'timeout') {
                                msg = 'Time out error.';
                            } else if (exception === 'abort') {
                                msg = 'Ajax request aborted.';
                            } else {
                                msg = 'Uncaught Error.\n' + jqXHR.responseText;
                            }
                console.log(msg);
            });
    
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks - however, I'm not actually clear on where the error is. Could you clarify where in the above code you are having a problem and what it is that you want it to do?

    Thanks,
    Allan

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    in line 36 above code it give me "undefined "
    i want have the column name or row[number] both are the same error

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    data = '<a target="_blank" href="test.php?screen_name=' + screen_name +'&data_type=' + columns.name +'">' + data + ' </a>'

    Is the line with the error.

    My guess is that you need to use columns[1] to get the data, since your data source appears to be arrays, not objects. Its difficult to say without a test case, but there is no columns.data option, which suggests that it is an array data source.

    Allan

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0
    edited October 2017

    test number 1:-
    this is the error message after using columns[2] :-

    Notice: Undefined index: column "in line number"

    Yes the data source is array correct

    test number 2:
    modify code using

    columns.data

    not working

  • Tester2017Tester2017 Posts: 145Questions: 23Answers: 17

    With console.log you can see what columns is and what it contains. Just put this line console.log('columns = ' + columns); before your line where filling data with a hyperlink.

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    I try the as following your code :-1:
    targets: 1,
    render: function ( data, type, row, meta ) {
    if(type === 'display'){
    console.log('columns = ' + columns);
    data = '<a target="_blank" href="Dynamic_tweets_report.php?screen_name=' + screen_name +'&data_type=' + data_type +'&column=25' +'&description=' + row[0] +'">' + data + ' </a>';
    }

    return data;
    },
    },

    No effect or any thing show in

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    You are initializing your columns variable:

    var data,
    tableName= '#Dynamic',
    columns,
    

    But I don't see where you are populating it with anything. Maybe you need to add something like this to your loop that builds the table headers:
    columns.push(colObj.name);

    And change your variable init to this:

    var data,
    tableName= '#Dynamic',
    columns = [],
    

    Kevin

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    Kevin,
    I am using a dynamic column creating when i get my query and send back to ajax, if you read the code it is a very good example created by datatable team it is work very good get the data. what i am try to do is a href link and try to send the data for my second page but not getting any data.

    Still waiting to get and find a solution for my problem. any way thank you Kevin for trying.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Rather than us guessing about the full initialisation and data, please post a link to a test case showing the issue so we can help to debug the live running code.

    Allan

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    Thank you allan he are the link :

    http://jbmruby.com.cws17.my-hosting-panel.com/datatabledemo/Dynamic_Details_report.php

    if you click on screen_name this is the correct result i want:-
    http://jbmruby.com.cws17.my-hosting-panel.com/datatabledemo/Dynamic_tweets_report.php?screen_name=_HugMeDolan_&data_type=4&column=25&description=@HayesGrier

    if you click on month of Oct.
    http://jbmruby.com.cws17.my-hosting-panel.com/datatabledemo/Dynamic_tweets_report.php?screen_name=_HugMeDolan_&data_type=2&column=12&description=*************

    I need to get the data value of description to be post link when you click on screen_name.

    I post the code above, thanking that enough.

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    Please Gentlemen, HELP needed ?????????

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    &description=' + row[1]

    Should be:

    &description=' + row.Description
    

    Your data object is an object not an array.

    Allan

  • issamymansour@gmail.comissamymansour@gmail.com Posts: 24Questions: 3Answers: 0

    Thanks allan, the best tools with the best support.

This discussion has been closed.