In Drupal 6, I am using tableselect theme with PagerSelect to generate table with checkboxes and pagination. Any thoughts on how to retain checkbox values on pagination using drupal API?

I used a method that does not follow the drupal standard coding like this :

function mymodule_form(){
	global $base_url;
	 drupal_add_js('misc/jquery.js', 'core', 'header');
     print drupal_get_js(); 
	 $url = $base_url.'/'.arg(1);
		
		<script type="text/javascript">
            var chkArray = new Array;
			var someGlobalArray = new Array;
			var hrefs = [];
			var burl = "<?php echo $base_url.'/'.arg(1); ?>";
			$(document).ready(function(){
					$('li.pager-item').find("a").each(function(){
						hrefs.push($(this).attr('href'));
					});

					$(".form-checkbox").click(function() {
						someGlobalArray=[];
						$('.form-checkbox:checked').each(function() {
							someGlobalArray.push($(this).val());
						});
						console.log(someGlobalArray);
						i = 0;
						$('li.pager-item').find("a").each(function(){
							//h = $(this).attr('href');
							h = hrefs[i] + "&ids[]=" + someGlobalArray.join(",");
							$(this).attr('href', h);
							//someGlobalArray = [];
							i++;
						});
					});
			});
        </script>

   
	
     $form = array(); 
	 $tableHeader = array(
		'test' => array('field' => 'n.title', 'data' => 'test'),
		'test1' => array('field' => 'l.name', 'data' => 'test1'),
		'test2' => array('field' => 'l.postal_code', 'data' => 'test2'),
		'test3' => array('field' => 'l.city', 'data' => 'test3'),
		'test4' => array('field' => 'cttc.field_activite_nid', 'data' => 'test4'),
		'test5' => array('field' => 'cttc.field_gestionnaire_value', 'data' => 'test5'),
     );
    $query = "SELECT nid FROM  table  ";
    // Pass the sql to the tablesort_sql here
    $query .= tablesort_sql($tableHeader);
	
   $result = pager_query($query,250,0,$sql_count);	
	
   while ($row =db_fetch_object($result)) {
		 $nid_list[] = $row->nid;
   }
	
    foreach($nid_list as $val){
	     $node = node_load($val);
		 //print "<pre>";print_r($node);
		 $options[$node->nid] = array(
			 'test' => $node->title,
			 'test1' => $node->location['street'],
			 'test2' => $node->location['postal_code'],
			 'test3' => $node->location['city'],
			 'test4' => $tmp_activite[$node->field_activite[0]['nid']],
			 'test4' => $node->field_gestionnaire[0]['value'],
         );
	}     
	if (!empty($options)) {
			$ids = isset($_GET["ids"]) ? $_GET["ids"] : array();
			$tab_check = array();
			global $tab_check;
			foreach($ids as $v) {
				//print_r($ids);
				$tids = explode(",", $v);
				foreach($tids as $id) {
					$tab_check[$id] = TRUE;
				}
			}
			//print "<pre>";print_r($tab_check);
			if(arg(2)!=""){
				$form['nodes'] = array(
				  '#type' => 'tableselect',
				  '#header' => $tableHeader,
				  '#options' => $options,
				  '#default_value' => $tab_check,
				  '#multiple' => TRUE,
				  '#advanced_select' => TRUE,
				);
			}else{
			    $form['nodes'] = array(
				  '#type' => 'tableselect',
				  '#header' => $tableHeader,
				  '#options' => $options,
				   '#default_value' => $tab_check,
				   '#multiple' => TRUE,
				  '#advanced_select' => TRUE,
				);
			}
			$form['pager'] = array('#value' => theme('pager', NULL, 250, 0));
    }
  return $form;
}

function mymodule_form_submit($form, &$form_state) {
	 $ids_del = isset($_GET["ids"]) ? $_GET["ids"] : array();
			$tab_check_del = array();
			foreach($ids_del as $v) {
				$tids_del = explode(",", $v);
				foreach($tids_del as $id) {
					$tab_check_del[] = $id;
				}
			} 
	
	 if(sizeof(tab_check_del)>0){
	  foreach($tab_check_del as $val){
	    $query = "DELETE from mytable WHERE nid ='".$val."' ";
		db_query($query);
	  }
	  
}

I know that i need to built #default_value of the tableselect with an array of checked checkbox id like this for example for 3 checkbox checked :
$temp = array();
$temp[1] = TRUE;
$temp[2] = TRUE;
$temp[3] = TRUE;

and put $temp in #default_value but , i dont find the right way to do this than using javascript inside my code

but I'm sure there are other clean method to do that

Thank you very much for your help

Comments

stomerfull’s picture

I'm using ajax to get it working

Thanks