Skip to main content

JQuery category selection table.



JQuery make js effect very easy and providing fancy techniques to do some stuff. In here we are going to explain how we can create category selection table using JQuery. Lets try to understand this using real world example. In this case we are having a table as fallow and we need to populate categories to select one or multiple for users.

DB table,


In here am using joomla coding but we can apply pure php cording to populate above table data in the side of front end.

Populate table data,


$db =& JFactory::getDBO();

$sqlgetcatinfor="SELECT * FROM #__ci_categoriesselection GROUP BY
state";
$db->setQuery($sqlgetcatinfor);
$categories = $db->loadObjectList();
//var_dump($categories);

$stateul='';
$tdregionul='';
$tdcategoryul='';
$i=1;
foreach($categories AS $category){
$stateul=$stateul.'';
$sqlgetregions="SELECT * FROM #__ci_categoriesselection WHERE
state='".$category->state."' GROUP BY regions";

$db->setQuery($sqlgetregions);
$regions = $db->loadObjectList();
$regionul='
class="states" type="checkbox" id="check_state_'.$i.'"/>'.$category->state.'
';
$j=1;
foreach($regions AS $region){
$regionul=$regionul.'';
$sqlgetcategory="SELECT * FROM #__ci_categoriesselection LEFT JOIN #__ci_projects_categories ON (categoryid= category_id) WHERE regions='".$region->regions."' GROUP BY category_id";
$db->setQuery($sqlgetcategory);
$getcategories = $db->loadObjectList();
$categoryul='
onclick="regionchecked(\'check_state_'.$i.'\',\'check_state_'.$i.'_region_'.$j.'\')"/>'.$region->regions.'
';
foreach($getcategories AS $getcategory){
$categoryul=$categoryul.'';
}
$categoryul=$categoryul."";
$tdregionul=$tdregionul.$regionul;
$i++;
}
$stateul=$stateul.'
category_id.'">'.$getcategory->category.' : ('.$getcategory->price.'$)
';// End state list
In above code sample I have used three inner foreach loops and assign that information different three variables ($stateul, $regionul and $categoryul. If you check above coding sample carefully you can understand that way of how I used table names and input box ids and class names. Finally I printed above discussed three variables inside another table to populate category selection table.

echo"

'.CI_LABLES_CHECKBOX_SELECT_ALL.'

'.CI_LABLES_CHECKBOX_SELECT_STATES.'

'.CI_LABLES_CHECKBOX_SELECT_REGIONS.'

'.CI_LABLES_CHECKBOX_SELECT_CATEGORIES.'


'.$stateul.'

'.$tdregionul.'

'.$tdcategoryul.'
";

Finally I got this view.

To hide all regions and categories when first time page loading I used css "display: none" property in my css page as fallow.

table.regions, table.categoryselect{
display: none;
}


Understanding js part,

1. For display categories with its parent when mouse over.

function categoryover(id, type){
if(type=='over_state'){
jQuery("table.regions").hide();
jQuery("table.categoryselect").hide();
}

if(type=='over_region'){
jQuery("table.categoryselect").hide();
}

jQuery("table#"+id).show();
}

Using this code segment first I hide all region and category tables. At the end of the script we execute jquery show command to display categories or regions section according to mouse over parent.

2. When user check on category, region or state.
When user check a one category we need to select its all parent items and in some situation user remove parent category this js coding need to be handy to remove its child elements also.

Mainly we have four different type of click events.
  • Click on state check box
  • Click on region check box
  • Click on category check box
  • Click on Select all check box.
In my js codding I used four methods to handle above mentioned click events.
function categorychecked(state,classid, region){

if(!jQuery('#'+state).is(':checked')){
jQuery('#'+state).attr("checked", true);
}

if(!jQuery('#'+region).is(':checked')){
jQuery('#'+region).attr("checked", true);
}
}

function regionchecked(state, classid){
if(jQuery('#'+state).is(':checked')){
}else{
jQuery('#'+state).attr("checked", true);
}

if(jQuery('#'+classid).is(':checked')){
jQuery('.'+classid).attr("checked", true);
}else{
jQuery('.'+classid).attr("checked", false);
}
}

function statechecked(id, classid){
if(jQuery('#'+id).is(':checked')){
jQuery('.'+classid).attr("checked", true);
}else{
jQuery('.'+classid).attr("checked", false);
}
}

function selectallcats(){
if(jQuery('#category_all').is(':checked')){
jQuery('.states').attr("checked", true);
jQuery('.regions').attr("checked", true);
jQuery('.categories').attr("checked", true);
}else{
jQuery('.states').attr("checked", false);
jQuery('.regions').attr("checked", false);
jQuery('.categories').attr("checked", false);
}
}

Comments

Popular posts from this blog

How to extract vtiger DB data

Here is the code sample how extract data form vtiger DB with WHERE close. Sample:1 require_once('include/DatabaseUtil.php'); global $adb; $tabid = array('9','16'); $sql = "SELECT tabid, fieldname, fieldid, fieldlabel,columnname, tablename, uitype, typeofdata, presence FROM vtiger_field WHERE tabid in (" . generateQuestionMarks($tabid) . ")"; $result = $adb->pquery($sql, array($tabid)); // get result $noofrows = $adb->num_rows($result); // get number of rows $i=0; $data=array(); while($resultrow = $adb->fetch_array($result)) { $tabid=$resultrow['tabid']; $resultrow=['fieldlabel']; $data[$i]=array($tabid, $resultrow); } Sample:2 require_once('include/DatabaseUtil.php'); global $adb; $query = 'select idlists,from_email,to_email,cc_email,bcc_email from vtiger_emaildetails where emailid =?'; $result = $adb->pquery($query, array($focus->id)); $from_email = $adb->query_result($result,0,'from_...

Debugging PHP with Netbeans

Most of the PHP developers are complaining that they have not a good ID for PHP debugging and every time that they have to use var_dump() print statement or PHP die or exit commands to check the state of php script. But now we can use netbeans to overcome this issue with netbeans-xdebug. I tried this with Linux and feel that working it cool. Anyway still I had not a time to try out this with windows and hope that netbeans-xdebuger working fine with windows also. Now I am going to explain how we can enable netbeans-xdebuger in Ubuntu 1. First we should have a netbeans installation package (Download from here ) 2. After installing Netbeans we can install xdebuger. To do that open terminal and type fallowing commands. sudo apt-get install php5-dev php-pear sudo pecl install xdebug 3. Now you have installed xdebug successfully but not configure with PHP yet. To Configure xdebuger with php and netbeans ID. open php.ini file using this command; sudo gedit /etc/php5/apache2/php.ini A...

Unix VS Linux

Free Software says that people have freedom to share software and related technology free without any hesitating. And also this may enable the rapid software development because all brains become to one stage and play the game. On a technical level, Free software grantees the right view and also modify source code if some one need different think as his view. In this post I am not going to deeply discuss about what Unix is and What Linux but here lats we talk about some interesting happenings around Unix and Linux What is Unix. In 1980, A computer scientist called Richard Stallman decided to create a venerable operating system called Unix. At the time Unix ran many of industrial and academic system in the world. He started developing Unix operating system and most of the world wide people join with him to success this operating system. Stallman called his version as "GNU" (Pronounced G-noo). This was start of GNU but not Unix, any way this was the new legendary of Unix so th...