2011-03-27 58 views
1

我使用CI生成使用表点击行进行编辑/删除?

$query = $this->expenses_model->expenses_table(); 

//gary's code went here 

$this->load->library('table'); 
$tmpl = array ('table_open' => '<table class="table">'); 
$this->table->set_template($tmpl); 

// gary added 'Edit' at end of array 

$this->table->set_heading('Date', 'Plant', 'Expense', 'Category', 'Notes'); 

//when using gary's code, changed $query below to $data 

$table['tab'] = $this->table->generate($query);  
$this->load->view('vw/exp/expenses_vw', $table, TRUE); 

它通过jQuery的数据表上运行客户端

$(document).ready(function() { 
    /* Init DataTables */ 
    var oTable = $('.table').dataTable({ 
       "bJQueryUI": true, 
       "sScrollX": "", 
       "bSortClasses": false, 
       "aaSorting": [[0,'desc']], 
       "bAutoWidth": true, 
       "bInfo": true, 
       "sScrollY": "100%", 
       "sScrollX": "100%", 
       "bScrollCollapse": true, 
       "sPaginationType": "full_numbers", 
       "bRetrieve": true 
       });  
    }); 

问题#1 对数据库的每条记录都有一个唯一的自动增量ID record_id那需要传递给每一行。但是这个record_id列无法显示在前端(即需要隐藏)。我们如何通过CI来做到这一点?

问题2 我应该使用什么样的JS,以允许用户点击行,弹出与编辑表单/删除?

感谢您的帮助!

PS - 这里是生成表数据模型

function expenses_table() 
{ 
    $id = $this->tank_auth->get_user_id(); 

    $this->db->select('record_id, date_format(date, \'%c/%d/%Y\'), plant_name, concat(\'$ \', format(value_1, 2)), value_2, value_3', FALSE); 
    $this->db->from('data'); 
    $this->db->join('plants', 'plants.plant_id = data.plant_id_fk'); 
    $this->db->where('category_1', 'expenses'); 
    $this->db->where('data.id_fk', $id); 
    $this->db->order_by("date", "desc"); 
    $query = $this->db->get(); 

    return $query; 
} 

回答

2

1.添加新列Edit

$this->table->set_heading('Date', 'Plant', 'Expense', 'Category', 'Notes', 'Edit'); 

2.构建编辑链接基础上,RECORD_ID每个record and hide record_id

$data = array(); 

while ($row = $query->result_array()) 
{ 
    $anchor = '<a href="#" class="edit_record" record_id="' . $row['record_id'] . '">Edit</a>'; 

    // Hide the record_id in the table output 
    unset($row['record_id']); 

    // Let's add the link so we can edit this entry 
    $row[] = $anchor; 

    // Lets push the new row so it can be output 
    $data[] = $row; 

} 

$table['tab'] = $this->table->generate($data); 

3.使用jQuery与行互动:

$('a.edit_record').click(function() { 
var record_id = this.attr('record_id'); 

// Lets generate the magical lightbox here... 

}); 

有许多可用于许多的jQuery插件灯箱,可以接受HTML。所有你需要做的就是创建一个ajax控制器来处理请求,使用模型来编辑/删除并以JSON返回结果。

jquery lightbox html (Google)

+0

@gary绿色 - 感谢您的答复 - 请我的更新上面,我说我用它来生成表数据模型 - 我不使用循环或'while' - 任何想法如何'在我的情况下unset''record_id'? – pepe 2011-03-27 14:17:52

+0

@gary green - 另一个问题是,'unset'会删除列,它不会允许我使用'record_id'来使每行都是唯一的。 – pepe 2011-03-27 14:21:17

+0

你真正需要什么?现在你谈论的是让每一行都是独一无二的?...... – 2011-03-27 14:29:25

相关问题