0

I am using DataTables to generate a table. There is a column containing username

For example: ...

I need every row in this column to have a hyperlink when anyone clicks on username then it redirects to edit page, For example, the first row would be a hyperlink to view?id=1321755 etc.

What is the simplest way I can do so?

Here is my code of view:

<table id="book-table" class="table table-bordered table-striped table-hover">
     <thead>
     <tr class="">
      <th>Name</th>
      <th>Date</th>
      <th>Work</th>
      <th>Partner</th>
      <th>Director</th>
      <th>Time</th>
      <th>Task</th>
      <th>Status</th>
    </tr>
     </thead>
     <tbody>
     </tbody>
     </table>

<script type="text/javascript">
$(document).ready(function() {
    $('#book-table').DataTable({
        "ajax": {
            url : "<?php echo site_url("digital_admin/hodm/books_page") ?>",
            type : 'GET'
        },
    });
});
</script>

Here is my controller code:

public function books_page()
     {

          // Datatables Variables
          $draw = intval($this->input->get("draw"));
          $start = intval($this->input->get("start"));
          $length = intval($this->input->get("length"));


          $books = $this->pojo->get_books();

          $data = array();

          foreach($books->result() as $r) {

               $data[] = array(
                    $r->user_name,
                    $r->date,
                    $r->t_name,
                    $r->partner,
                    $r->director,
                    $r->duration,
                    $r->task,
                    $r->status
               );
          }

          $output = array(
               "draw" => $draw,
                 "recordsTotal" => $books->num_rows(),
                 "recordsFiltered" => $books->num_rows(),
                 "data" => $data
            );
          echo json_encode($output);
          exit();
     }

Kindly help me to find the answer

4
  • 1
    Possible duplicate of Jquery - Add hyperlink to datatables Commented Mar 29, 2017 at 11:28
  • @markpsmith link which you mention is only for one row and my problem for lots of rows which fetched from database Commented Mar 29, 2017 at 11:37
  • Easiest is to put the link already in the query something like: '<a href="view?id='. $r->user_id .'" >'. $r->user_name .'</a>'; Commented Mar 29, 2017 at 11:38
  • @MichaelK I didn't get you . how i add link and where Commented Mar 29, 2017 at 11:42

2 Answers 2

1

Easiest way would be to put it directly in the array something like:

      foreach($books->result() as $r) {

           $data[] = array(
                '<a href="/view?id='.$r->user_id.'" >'. $r->user_name .'</a>',
                $r->date,
                $r->t_name,
                $r->partner,
                $r->director,
                $r->duration,
                $r->task,
                $r->status
           );
      }

But be aware that the sorting on such column in DataTables may not apply to the actual username shown, but the whole content string. Replace /view?id='.$r->user_id.' in the above example with the id and url you want to use.

Note that json_encode() escapes some characters, for instance the double quotes, so you might need to do add backslash before them:

'<a href=\"/view?id='.$r->user_id.'\" >'. $r->user_name .'</a>'
Sign up to request clarification or add additional context in comments.

Comments

0

@Michael Thanx for the help.

Here is the final code which is working.

$data[] = array(
                    '<a href="edit_hodm/'.$r->id.'" >'. $r->user_name .'</a>',
                    $r->date,
                    $r->t_name,
                    $r->partner,
                    $r->director,
                    $r->duration,
                    $r->task,
                    $r->status
               );

1 Comment

Glad it helped, please accept my answer if it worked for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.