Reputation: 8856
I'm creating the HTML table from the database table (i.e Categories) by using the codeIgnitor's class.
Here is my controller:
Testpagi.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Testpagi extends CI_Controller{
function __construct(){
parent::__construct();
}
public function index($offset = 0){
$search_query = $this->db->get('Categories');
$count_records = $search_query->num_rows();
// Load the tables library
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/testpagi/index/';
$config['total_rows'] = $count_records;
$config['per_page'] = 10;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
$data['categories'] = $this->db->get('Categories', 10, $offset);
// Create custom headers
$header = array('S. No', 'Category Name', 'Status', 'Date Added', 'Last Modified');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$tmpl = array (
'table_open'=> '<table width="600px" bgcolor="#F7F7F7" align="center" border="2" cellpadding="4" cellspacing="0">',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th bgcolor="#0B548C" style="color:#FFFFFF">',
'heading_cell_end' => '</th>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td bgcolor="#D4620E" >',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td bgcolor="#B2D593">',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
$this->table->set_template($tmpl);
$data['message'] = 'Not Exist';
$this->load->view('books_view', $data);
}
}
?>
and here is my View:
books_view.php
<html>
<body>
<div id='results' align="center">
<?
echo $this->table->generate($categories);
echo $this->pagination->create_links();
?>
</div>
</body>
</html>
Note I'm using $this->table->set_template($tmpl);
for styling my table.
<a href=" '#' onclick="
. What should be here " ">
to make the <td>
clickable. I searched a lot but can't found the solution.<td>blah</td>
and call function on click in CodeIgniter?Here is the image:
I want to add links on the categories highlighted Yellow
.
Upvotes: 0
Views: 370
Reputation: 22862
I would advise you to use html instead of javascript
You could accomplish that with just a little tweaking on your controller and view code:
public function index($offset = 0){
$search_query = $this->db->get('Categories');
$count_records = $search_query->num_rows();
// Load the tables library
$this->load->library('table');
// Load Pagination
$this->load->library('pagination');
// Config setup
$config['base_url'] = base_url().'/testpagi/index/';
$config['total_rows'] = $count_records;
$config['per_page'] = 10;
// I added this extra one to control the number of links to show up at each page.
$config['num_links'] = 5;
// Initialize
$this->pagination->initialize($config);
// Query the database and get results
//CHANGE THIS
$categories = $this->db->get('Categories', 10, $offset)->result_array();
// Create custom headers
$header = array('S. No', 'Category Name', 'Status', 'Date Added', 'Last Modified');
// Set the headings
$this->table->set_heading($header);
// Load the view and send the results
$tmpl = array (
'table_open'=> '<table width="600px" bgcolor="#F7F7F7" align="center" border="2" cellpadding="4" cellspacing="0">',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th bgcolor="#0B548C" style="color:#FFFFFF">',
'heading_cell_end' => '</th>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td bgcolor="#D4620E" >',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td bgcolor="#B2D593">',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
$this->table->set_template($tmpl);
//AND ADD THIS CODE
foreach($categories as $category){
$this->table->row(array($category['id'], '<a href="'.site_url('contoller/method/'.$category['id']).'">'.$category['category_name'].'</a>', $category['status'], $category['date_added'], $category['last_modified']));
//remember to change the keys of $category (id,category_name,status,date_added,last_modified) for the ones that you have in your database
}
$data['message'] = 'Not Exist';
$this->load->view('books_view', $data);
}
Now on your view just remove the $categories from the generate function:
<html>
<body>
<div id='results' align="center">
<?
echo $this->table->generate();
echo $this->pagination->create_links();
?>
</div>
</body>
</html>
Upvotes: 0
Reputation: 4430
Just an Idea, Use Jquery , first add class or id to your <td>
on $tmpl array, then use this:
<script type="text/javascript">
$(document)ready(function(){
$('your-class').click(function(){
window.location.replace('your link');
});
});
</script>
good luck !.
Upvotes: 1