 |
|
ผมต้องการให้แสดง 2 table ในหน้าเดียวกันครับ แต่ติดปัญหาคือกด ลบ แล้วมันไม่ลบต้อง refresh หน้าเอง รบกวนด้วยครับ
VIEW fixigrid_tem.php
Code (PHP)
<?php
echo $js_grid;
?>
<script type="text/javascript">
function test(com,grid)
{
if (com=='Select All')
{
$('.bDiv tbody tr',grid).addClass('trSelected');
}
if (com=='DeSelect All')
{
$('.bDiv tbody tr',grid).removeClass('trSelected');
}
if (com=='Delete')
{
if($('.trSelected',grid).length>0){
if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
var items = $('.trSelected',grid);
var itemlist ='';
for(i=0;i<items.length;i++){
itemlist+= items[i].id.substr(3)+",";
}
$.ajax({
type: "POST",
url: "<?=site_url("/ajax1/deletec");?>",
data: "items="+itemlist,
success: function(data){
$('#flex1').flexReload();
alert(data);
}
});
}
} else {
return false;
}
}
}
</script>
<?php
echo $js_grid2;
?>
<script type="text/javascript">
function test(com,grid)
{
if (com=='Select All')
{
$('.bDiv tbody tr',grid).addClass('trSelected');
}
if (com=='DeSelect All')
{
$('.bDiv tbody tr',grid).removeClass('trSelected');
}
if (com=='Delete')
{
if($('.trSelected',grid).length>0){
if(confirm('Delete ' + $('.trSelected',grid).length + ' items?')){
var items = $('.trSelected',grid);
var itemlist ='';
for(i=0;i<items.length;i++){
itemlist+= items[i].id.substr(3)+",";
}
$.ajax({
type: "POST",
url: "<?=site_url("/ajax1/deletecDomain");?>",
data: "items="+itemlist,
success: function(data){
$('#flex2').flexReload();
alert(data);
}
});
}
} else {
return false;
}
}
}
</script>
Controller flexigrid.php / ajax1.php ตามลำดับ
Code (PHP)
<?php
class Flexigrid extends Controller {
function Flexigrid ()
{
parent::Controller();
$this->load->helper('flexigrid_helzzzz');
}
function index()
{
$colModel['id'] = array('ID',40,TRUE,'center',2);
$colModel['iso'] = array('ISO',40,TRUE,'center',0);
$colModel['name'] = array('Name',180,TRUE,'left',1);
$colModel['printable_name'] = array('Printable Name',120,TRUE,'left',0);
$colModel['iso3'] = array('ISO3',130, TRUE,'left',0);
$colModel['numcode'] = array('Number Code',80, TRUE, 'right',1);
$colModel['actions'] = array('Actions',80, FALSE, 'right',0);
$colModel2['id'] = array('ID',40,TRUE,'center',2);
$colModel2['name'] = array('NAME',40,TRUE,'center',0);
$colModel2['last_name'] = array('LAST NAME',180,TRUE,'left',1);
/*
* Aditional Parameters
*/
$gridParams = array(
'width' => '800',
'height' => 400,
'rp' => 15,
'rpOptions' => '[10,15,20,25,40]',
'pagestat' => 'Displaying: {from} to {to} of {total} items.',
'blockOpacity' => 0.5,
'title' => 'Hello 1111',
'showTableToggleBtn' => true
);
$gridParams2 = array(
'width' => '500',
'height' => 400,
'rp' => 15,
'rpOptions' => '[10,15,20,25,40]',
'pagestat' => 'Displaying: {from} to {to} of {total} items.',
'blockOpacity' => 0.5,
'title' => 'Hello 222',
'showTableToggleBtn' => true
);
/*
* 0 - display name
* 1 - bclass
* 2 - onpress
*/
$buttons[] = array('Delete','delete','test');
$buttons[] = array('separator');
$buttons[] = array('Select All','add','test');
$buttons[] = array('DeSelect All','delete','test');
$buttons[] = array('separator');
//Build js
//View helpers/flexigrid_helper.php for more information about the params on this function
$grid_js = build_grid_js1('flex1',site_url("/ajax1/index1"),$colModel,'id','asc',$gridParams,$buttons);
$data['js_grid'] = $grid_js;
$grid_js1 = build_grid_js1('flex2',site_url("/ajax1/index2"),$colModel2,'id','asc',$gridParams2,$buttons);
$data['js_grid2'] = $grid_js1;
$this->load->view('flexigrid_tem',$data);
}
}
Code (PHP)
class Ajax1 extends Controller {
function Ajax1 ()
{
parent::Controller();
$this->load->model('ajax_model');
$this->load->model('ajax_model1');
$this->load->library('flexigrid_lib');
}
function index1()
{
//List of all fields that can be sortable. This is Optional.
//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
$valid_fields = array('id','iso','name','printable_name','iso3','numcode');
$this->flexigrid_lib->validate_post('id','asc',$valid_fields);
$records = $this->ajax_model->get_countries();
$this->output->set_header($this->config->item('json_header'));
/*
* Json build WITH json_encode. If you do not have this function please read
* http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
*/
foreach ($records['records']->result() as $row)
{
$record_items[] = array($row->id,
$row->id,
$row->iso,
$row->name,
'<span style=\'color:#ff4400\'>'.addslashes($row->printable_name).'</span>',
$row->iso3,
$row->numcode,
'<a href=\'#\'><img border=\'0\' src=\''.$this->config->item('base_url').'public/images/close.png\'></a> '
);
}
//Print please
$this->output->set_output($this->flexigrid_lib->json_build($records['record_count'],$record_items));
}
//Delete Country
function deletec()
{
$countries_ids_post_array = split(",",$this->input->post('items'));
foreach($countries_ids_post_array as $index => $country_id)
if (is_numeric($country_id) && $country_id > 1)
$this->ajax_model->delete_country($country_id);
$error = "Selected countries (id's: ".$this->input->post('items').") deleted with success";
$this->output->set_header($this->config->item('ajax_header'));
$this->output->set_output($error);
}
function index2()
{
//List of all fields that can be sortable. This is Optional.
//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
$valid_fields = array('id','name','last_name');
$this->flexigrid_lib->validate_post('id','asc',$valid_fields);
$records = $this->ajax_model1->get_domains();
$this->output->set_header($this->config->item('json_header'));
/*
* Json build WITH json_encode. If you do not have this function please read
* http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
*/
foreach ($records['records']->result() as $row)
{
$record_items[] = array($row->id,
$row->id,
$row->name,
$row->last_name
);
}
//Print please
$this->output->set_output($this->flexigrid_lib->json_build($records['record_count'],$record_items));
}
//Delete Country
function deletecDomain()
{
$domains_ids_post_array = split(",",$this->input->post('items'));
foreach($domains_ids_post_array as $index => $domain_id)
if (is_numeric($domain_id) && $domain_id > 1)
$this->ajax_model1->delete_domains($domain_id);
$error = "Selected domains (id's: ".$this->input->post('items').") deleted with success";
$this->output->set_header($this->config->item('ajax_header'));
$this->output->set_output($error);
}
}
?>
Model ajax_model.php/ajax_model1.php
Code (PHP)
class Ajax_model extends Model
{
/**
* Instanciar o CI
*/
public function Ajax_model()
{
parent::Model();
$this->CI =& get_instance();
}
public function get_countries()
{
//Select table name
$table_name = "country";
//Build contents query
$this->db->select('id,iso,name,printable_name,iso3,numcode')->from($table_name);
$this->CI->flexigrid_lib->build_query();
//Get contents
$return['records'] = $this->db->get();
//Build count query
$this->db->select('count(id) as record_count')->from($table_name);
$this->CI->flexigrid_lib->build_query(FALSE);
$record_count = $this->db->get();
$row = $record_count->row();
//Get Record Count
$return['record_count'] = $row->record_count;
//Return all
return $return;
}
/**
* Remove country
* @param int country id
* @return boolean
*/
public function delete_country($country_id)
{
$delete_country = $this->db->query('DELETE FROM country WHERE id='.$country_id);
return TRUE;
}
Code (PHP)
class Ajax_model1 extends Model
{
/**
* Instanciar o CI
*/
public function Ajax_model1()
{
parent::Model();
$this->CI =& get_instance();
}
public function get_domains()
{
//Select table name
$table_name = "domains";
//Build contents query
$this->db->select('id,name,last_name')->from($table_name);
$this->CI->flexigrid_lib->build_query();
//Get contents
$return['records'] = $this->db->get();
//Build count query
$this->db->select('count(id) as record_count')->from($table_name);
$this->CI->flexigrid_lib->build_query(FALSE);
$record_count = $this->db->get();
$row = $record_count->row();
//Get Record Count
$return['record_count'] = $row->record_count;
//Return all
return $return;
}
/**
* Remove country
* @param int country id
* @return boolean
*/
public function delete_domains($domain_id)
{
$delete_domain = $this->db->query('delete FROM country WHERE id='.$domain_id);
return TRUE;
}
}
?>
Tag : PHP, MySQL, Ajax, jQuery, CakePHP
|
ประวัติการแก้ไข 2011-02-09 15:58:56
|
 |
 |
 |
 |
Date :
2011-02-09 15:51:00 |
By :
zachalich |
View :
1466 |
Reply :
0 |
|
 |
 |
 |
 |
|
|
|
 |