问题
I am trying to implement tree dropdown option. I am being able to show options that i have in my database. But when clicked it is not being clicked or posted. Here is what i have done.
I have send array data from controller like this:
$this->data['category_tree'] = $this->general->get_category_tree();
and the function for this in general is:
public function get_category_tree()
{
$this->ci->db->where('is_display','1');
$query = $this->ci->db->get('product_categories');
if ($query->num_rows() > 0)
{
foreach($query->result() as $cat)
{
if($cat->parent_id=='0'){
//category
$categories_arr[$cat->id] = array('id' => $cat->id, 'parent_id'=>$cat->parent_id ,'name' => $cat->name, 'subcat' => array());
}else{
//subcategory;
$categories_arr[$cat->parent_id]['subcat'][] = array('id' => $cat->id, 'parent_id' => $cat->parent_id, 'name' => $cat->name);
}
}
return $categories_arr;
}
return false;
}
And in view:
<fieldset >
<h4>Choose Category</h4>
<div class="ddmenu"> <a id="chooseCategory" href="javascript:void(0)" class="main_btn">
<?php if($this->input->post('categoryName',TRUE)){echo $this->input->post('categoryName',TRUE);}?>
</a>
<ul>
<?php
if($category_tree){
foreach($category_tree as $category)
{
?>
<li <?php if(!$category['subcat']){ ?> onclick="addThis('<?php echo $category['name']; ?>','<?php echo $category['id']; ?>','0')" <?php } else { ?>class="dropdown-submenu"<?php }?>> <a href="javascript:void(0)" tabindex="-1"><?php echo $category['name']; ?></a>
<?php if($category['subcat']){ ?>
<ul class="dropdown-menu" >
<?php foreach($category['subcat'] as $subcat){?>
<li onclick="addThis('<?php echo $subcat['name']; ?>','<?php echo $category['id']; ?>','<?php echo $subcat['id']; ?>')"> <a href="javascript:void(0)" data-clickable="data-clickable" tabindex="-1"> <?php echo $subcat['name']; ?> </a> </li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php
}
?>
</ul>
</div>
<?php } ?>
</fieldset>
and I am getting this view
But when I click it, nothing happens. How can I make it posted. I have loaded the script. Is it problem with it?
回答1:
Sorry, I gave you the wrong JS. I didn't want to recreate your entire scenario, so I just did the following:
<?php
$name = 'cat';
$cat_id = 12;
$subcat_id = 11;
$subcat_name = 'Subcat';
?>
<ul>
<li onclick=location.href='<?php echo base_url() . $name . '/' . $cat_id . '/' . $subcat_id; ?>'>
<a href="javascript:;" data-clickable="data-clickable" tabindex="-1"> <?php echo $subcat_name; ?> </a>
</li>
</ul>
Of course, your setup is slightly different, but you should be able to replace the variables as appropriate. This did redirect me to a non-existent, but correctly requested page.
来源:https://stackoverflow.com/questions/44558856/how-to-make-tree-dropdown-in-codeigniter