⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Default order on a table



geekygirl

geekygirl
  • profile picture
  • Member

Posted 14 August 2015 - 16:08 PM

I have a table (that displays data from and database table) and by default the table is sorted by "id" field from the database.

Since that field is now shown in the table by default if I use some other order (by clicking on column header) later I can't revert back so data is sorted by "id" (default).

The only way to get the default sorting back is to delete the cookies.

 

Have anyone noticed this?

Is there some solution for this problem?

 

Thanks!


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 17 August 2015 - 14:35 PM

Had shared the solution long time ago .. but it comes again .. so enjoy the same :)

/***********Add it in some common helper file and autoload it or manually load it.**********/

if ( ! function_exists('clearSearchCookies'))
{
    function clearSearchCookies($thisurl) {
        //Check if the referer is not same as this page then clear the cookies
        if(array_key_exists('HTTP_REFERER', $_SERVER)) {
            $refering_url = $_SERVER['HTTP_REFERER'];
            if($refering_url != '') {
                if(substr($refering_url, 0, strlen($thisurl)) == $thisurl) {
                    //Fine to go with .. since it is the same one continued in here
                } else {
                    deleteSearchCookies();
                }
            } else {
                deleteSearchCookies();
            }    
        } else {
            deleteSearchCookies();
        }
    }
}

if ( ! function_exists('deleteSearchCookies'))
{
    function deleteSearchCookies() {
        foreach ($_COOKIE as $key=>$val) {
            if(stripos($key, 'crud_page') !== FALSE) {
                delete_cookie($key);
            }
            if(stripos($key, 'per_page') !== FALSE) {
                delete_cookie($key);
            }
            if(stripos($key, 'hidden_ordering') !== FALSE) {
                delete_cookie($key);
            }
            if(stripos($key, 'search_text') !== FALSE) {
                delete_cookie($key);
            }
            if(stripos($key, 'search_field') !== FALSE) {
                delete_cookie($key);
            }
        }
    }
}


/***********One of the function reference from the actual controller.**********/
/**
     * Function to manage the retailers
     */
    function retailers($parent_retailer_id = 0) {
        //Clear the search related cookies if the the url is a fresh hit
        clearSearchCookies($thisurl = site_url() . "manage/retailers");
        $this->load->library('ajax_grocery_CRUD');
        $crud = new ajax_grocery_CRUD();
        $crud->set_table('retailers');
		......
		.....
		$this->load->view('retailer_crud', $output);
	}

Happy GCing ...