0

我正在使用codeIgnitor 的类从数据库表(即类别)创建HTML 表。

这是我的控制器:

测试页面.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);
}
}
?>

这是我的观点:

book_view.php

<html>
<body>
    <div id='results' align="center">
    <?
            echo $this->table->generate($categories); 
            echo $this->pagination->create_links();

    ?>    

    </div>
</body>
</html>

注意我正在使用$this->table->set_template($tmpl);我的表格样式。

  1. 我很困惑我应该在哪里放置<a href=" '#' onclick=". 什么应该在这里" ">使<td>可点击。我搜索了很多,但找不到解决方案。
  2. 如何<td>blah</td>在 CodeIgniter 中单击时嵌入链接并调用函数?

这是图像:

在此处输入图像描述

我想在突出显示的类别上添加链接Yellow

4

2 回答 2

1

只是一个想法,使用 Jquery ,首先将 class 或 id 添加到您<td>的 $tmpl 数组中,然后使用它:

<script type="text/javascript">
   $(document)ready(function(){
     $('your-class').click(function(){
        window.location.replace('your link');
     });
   });
</script>

祝你好运 !。

于 2013-03-15T12:22:30.043 回答
0

我建议你使用 html 而不是 javascript

您只需对控制器和查看代码稍作调整即可完成此操作:

    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);
    }

现在在您看来,只需从生成函数中删除 $categories :

<html>
<body>
    <div id='results' align="center">
    <?
            echo $this->table->generate(); 
            echo $this->pagination->create_links();

    ?>    

    </div>
</body>
</html>
于 2013-03-15T14:08:15.810 回答