在CodeIgniter中使用类别和子类别进行链选择

时间:2011-02-21 23:04:52

标签: codeigniter

如何在CI中创建下拉选择菜单,以便在选择类别时,子类别将出现在同一页面上而不刷新浏览器?

3 个答案:

答案 0 :(得分:2)

这就是我做的,(很容易做到)

  1. 下载jQuery,+ jquery.chained.js
  2. 将jQuery + jquery.chained.js插入CI视图
  3. 创建用于从数据库生成选择的函数
  4. 调用函数
  5. 到目前为止我的表结构如下(我知道它可以在一个表中轻松完成)

    <强>类别

    CREATE TABLE IF NOT EXISTS `category` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(64) NOT NULL,
      `active` tinyint(1) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
    
    INSERT INTO `category` (`id`, `name`, `active`) VALUES
    (3, 'Science', 1),
    (4, 'History', 1);
    

    <强>子类

    CREATE TABLE IF NOT EXISTS `subcategory` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `category_id` int(11) NOT NULL,
      `name` varchar(64) NOT NULL,
      `active` int(11) NOT NULL,
      PRIMARY KEY (`id`),
      KEY `category_id` (`category_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
    
    INSERT INTO `subcategory` (`id`, `category_id`, `name`, `active`) VALUES
    (2, 3, 'Mathematics', 1),
    (3, 3, 'Physics', 1),
    (4, 3, 'Medicine', 1),
    (5, 4, '21st Century', 1),
    (6, 4, '18-20th Century', 1),
    (7, 4, '15-17th Century', 1),
    (8, 4, 'Before 15th Century', 1);
    
    ALTER TABLE `subcategory`
      ADD CONSTRAINT `subcategory_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`);
    

    现在我只加载所有活动类别/子类别,并为它们创建<select>个。

    请注意,我使用的是twitter-bootstrap 2,因此还有一些额外的HTML

    请注意,我正在使用我自己的 MY_Controller 文件扩展 CI_Controller ,因此我可以设置“global”$data[](传递给 view < / strong>)通过$this->data['key']向ex_ CI_Controller执行此操作tutorial

    请注意,它内置了“重新填充功能”,因此无论何时传递(有效或无效)category_id && || $subcategory_id,如果参数正确,它会在数据库中查找,如果存在,则会存在cateogry / subcategory。

    public function category_chain($category_id = FALSE, $subcategory_id = FALSE) { 
        $this->load->model('general_model');
    
        $repopulate['category'] = ''; 
        $repopulate['subcategory'] = ''; 
    
        if (($category_id !== FALSE && $subcategory_id !== FALSE) || ($category_id !== "FALSE" && $subcategory_id !== "FALSE")) { 
    
            if ($this->general_model->_isInDBWhere('subcategory', array('id' => $subcategory_id, 'category_id' => $category_id))) { 
    
                $repopulate['category'] = $category_id; 
                $repopulate['subcategory'] = $subcategory_id; 
    
            } 
    
        } 
    
        if (($category_id !== FALSE && $subcategory_id === FALSE) || ($category_id !== "FALSE" && $subcategory_id === "FALSE")) { 
    
            if ($this->general_model->_isInDB('category', 'id', $category_id)) { 
    
                $repopulate['category'] = $category_id; 
                $repopulate['subcategory'] = ''; 
    
            } 
    
        } 
    
        $categories = $this->general_model->_getAllWhere('category', 'active', '1'); 
        $subcategories = $this->general_model->_getAllWhere('subcategory', 'active', '1'); 
    
        $return = "<div class=\"control-group\"> 
                    <label class=\"control-label\">.category </label> 
                        <div class=\"controls\"> 
                        <div class=\"input-prepend\"> 
                                <span class=\"add-on\"><i class=\"icon-th-large\"></i></span>"; 
        $return .= "<select name=\"category_id\" id=\"category\">"; 
        $return .= "<option value=\"\">--</option>"; 
    
        foreach ($categories as $category) { 
    
            $return .= "<option value=\"".$category->id."\"".(($repopulate['category'] == $category->id) ? " selected": "").">".$category->name."</option>"; 
    
        } 
    
        $return .= "</select>"; 
        $return .= "</div></div></div>"; 
    
    
        $return .= "<div class=\"control-group\"> 
                    <label class=\"control-label\">.subcategory </label> 
                        <div class=\"controls\"> 
                        <div class=\"input-prepend\"> 
                                <span class=\"add-on\"><i class=\"icon-th\"></i></span>"; 
        $return .= "<select name=\"subcategory_id\" id=\"subcategory\">"; 
        $return .= "<option value=\"\">--</option>"; 
    
        foreach ($subcategories as $subcategory) { 
    
            $return .= "<option value=\"".$subcategory->id."\" class=\"".$subcategory->category_id."\"".(($repopulate['subcategory'] == $subcategory->id) ? " selected": "").">".$subcategory->name."</option>"; 
    
        } 
    
        $return .= "</select>"; 
        $return .= "</div></div></div>"; 
    
        $this->data['category_chain'] = $return; 
    
    }
    

    最后创建application/models/general_model.php并创建此功能

    function _getAllWhere($table, $where, $value) { 
            $q = $this->db->get_where($table, array($where => $value)); 
            return ($q->num_rows() > 0) ? $q->result() : FALSE; 
    } 
    

    最后一步是以我们需要它的方式调用函数。 在所需的控制器中,只需拨打$this->category_chain(),然后在视图中就会有一个变量avalible $category_chain(只需像<?=$category_chain?>那样回复它)

    多数民众赞成;)

答案 1 :(得分:1)

您应该通过AJAX执行此操作并检索子类别。准备好子类别<select>或下拉列表。

将PHP返回的值附加到下拉列表中。

一些伪代码:

$.each(data, function(key, value)
{   
     $('#subcategories').
          append($("<option></option>").attr("value",key).text(value)); 
});

答案 2 :(得分:0)

实际上这是我希望实现的代码,我希望在选择父级时显示子项。

if(count($navlist)){

foreach($navlist as $key => $list){
    echo "<select>";
        foreach($list as $topkey => $toplist){
            echo '<option value="'. $topkey .'">'. $toplist["name"]. '</option>';


                if(count($toplist['children'])){
                    /*foreach($toplist['children'] as $subkey => $subname){
                        echo "\n<li class='subcat'>";
                        echo anchor("welcome/cat/$subkey", $subname);
                        echo "</li>";
            }       */
        }
    }   
}

echo "</select>";
}

?>

由于