Pass 'id' to Edit link in new column for user created edit page

Pass 'id' to Edit link in new column for user created edit page

RobertsuggsRobertsuggs Posts: 5Questions: 0Answers: 0
edited November 2012 in General
I have created a column (column 0) to have a hyperlink, so that when clicked will open another page (edit.php). I would like to pass the id (which is in column 1) to a link in this manner (edit.php?id='id from column 1). BTW, using server side processing.

Here is most of my javascript, which is where I think a change would need to happen. Currently, each "Edit" link has the value of null, because the mData is null (I think).
[code]

var asInitVals = new Array();

$(document).ready(function() {

var oTable = $('#example').dataTable( {
"iDisplayLength":25,
"aLengthMenu":[[25,50,100,-1],[25,50,100,"ALL"]],
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",
"aoColumns": [
{ "mData": null,
"mRender":function(data, type, full) {
return 'Edit';
}
},
{ "mData": 0 },
{ "mData": 1 },
{ "mData": 2 },
{ "mData": 3 },
{ "mData": 4 },
{ "mData": 5 }
],
"oLanguage": {
"sSearch": "Search all columns:"
},
} );
[/code]

I am very appreciative of anyone who can offer assistance. I have searched the forums for about 2 hours, but nothing seemed to work for me. Thanks!!

Replies

  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
    > Currently, each "Edit" link has the value of null, because the mData is null (I think).

    Exactly that! Just set mData to the array index with your id in it - `0` in this case from what you say. The same data property can be used in multiple columns (as would be the case here) if you want :-)

    Allan
  • RobertsuggsRobertsuggs Posts: 5Questions: 0Answers: 0
    edited November 2012
    Perfect, so by doing this, it works:

    [code]
    { "mData": 0,
    "mRender":function(data, type, full) {
    return 'Edit';
    }
    [/code]

    My problem now is that the columns sort for the following column.
    Columns: ID | NAME | EMAIL
    When I sort ID, NAME is actually begin sorted, sort NAME and EMAIL is sorted. Off to search the forumns! Thanks Allan, great software you have here!
  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
    Since you are using server-side processing the sorting / filtering is done at the server-side, so most likely the server-side script isn't taking into account that the column index is 'off by one' due to the first column. The mDataProp_{} parameter that is sent to the server might be useful.

    This script shows an example of mDataProp_{} being used on the server: https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/objects.php

    Allan
  • RobertsuggsRobertsuggs Posts: 5Questions: 0Answers: 0
    edited November 2012
    I completely agree the sorting is done at the server side and that the server side script doesn't take into account that I added an extra column at the beginning...I just don't know how to tell it otherwise.
    I've looked at your example, but I'm not understanding what its telling me. Also, hasn't mDataProp been replaced with mData?
    I think I need another nudge. Thank you!!
  • allanallan Posts: 63,530Questions: 1Answers: 10,473 Site admin
    > Also, hasn't mDataProp been replaced with mData?

    For initialisation yes. But not in what is sent to the server since that would break backwards compatibility - see the server-side documentation: http://datatables.net/usage/server-side .

    > I just don't know how to tell it otherwise.

    You need to make the column index to the data property. For example in my code:

    [code]
    $iColumnIndex = array_search( $_GET['mDataProp_'.$_GET['iSortCol_'.$i]], $aColumns );
    [/code]

    Exactly how you should do it will depend upon your own script and how it is constructed, but you need a mapping of some kind.

    Allan
  • RobertsuggsRobertsuggs Posts: 5Questions: 0Answers: 0
    After many echo statements to understand how it works, I think I figured it out...
    For some reason, when using the array_search function, I could never get a value assigned to $iColumnIndex. So I altered the code as follows:
    [code]
    $iColumnIndex = $_GET['mDataProp_'.$_GET['iSortCol_'.$i]];
    [/code]
    and it works great! Thanks for the help.
This discussion has been closed.