使用CodeIgniter动态选择下拉列表

时间:2011-03-19 10:29:28

标签: codeigniter select drop-down-menu

我正在使用CodeIgniter开发一个Web应用程序,并想知道是否有人可以帮助解决已经出现的问题。

我有两个下拉菜单 - 分部和团队。我正在创建一个夹具,所以希望能够允许用户从第一个下拉列表中选择一个部门,然后在第二个下拉列表中只包含所选部门的团队。

我到目前为止的守则:

控制器:

function create() { 
    $data['division_list'] = $this->add_fixture_model->get_divisions();
    $data['team_list'] = $this->add_fixture_model->get_teams();
    $data['referee_list'] = $this->add_fixture_model->get_referee();


    // field name, error message, validation rules
    $this->form_validation->set_rules('team_name', 'Team Name', 'trim|required');
    $this->form_validation->set_rules('home_team', 'Home Team Name', 'trim|required');
    $this->form_validation->set_rules('away_team', 'Away Team Name', 'required');
    $this->form_validation->set_rules('division_name', 'Division', 'trim|required');
    $this->form_validation->set_rules('referee', 'Referee', 'trim|required');
    $this->form_validation->set_rules('fixture_week', 'Fixture Week', 'trim|required');
    $this->form_validation->set_rules('fixture_day', 'Fixture Day', 'trim|required');
    $this->form_validation->set_rules('fixture_month', 'Fixture Month', 'trim|required');
    $this->form_validation->set_rules('fixture_year', 'Fixture Year', 'trim|required');
    $this->form_validation->set_rules('confirm_day', 'Fixture Confirm Day', 'trim|required');
    $this->form_validation->set_rules('confirm_month', 'Fixture Confirm Month', 'trim|required');
    $this->form_validation->set_rules('confirm_year', 'Fixture Confirm Year', 'trim|required');




    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('includes/top');
        $this->load->view('includes/header');
        $this->load->view('league/add_fixture', $data);
        $this->load->view('includes/footer');
    }

    else
    {           
        if($query = $this->add_fixture_model->add_fixture())
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/fixture_added');
            $this->load->view('includes/footer');

        }
        else
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/fixture_added', $data);
            $this->load->view('includes/footer');       
        }
    }

}

型号:

类Add_fixture_model扩展了Model {

function add_fixture()
{

    $add_new_fixture = array(
        'team_name' => $this->input->post('team_name'),
        'home_team' => $this->input->post('home_team'),
        'away_team' => $this->input->post('away_team'),
        'division' => $this->input->post('division_name'),
        'ground' => $this->input->post('ground'),
        'ground_postcode' => $this->input->post('ground_postcode'),
        'referee' => $this->input->post('referee'),
        'fixture_week' => $this->input->post('fixture_week'),
        'fixture_day' => $this->input->post('fixture_day'),
        'fixture_month' => $this->input->post('fixture_month'),
        'fixture_year' => $this->input->post('fixture_year'),
        'confirm_day' => $this->input->post('confirm_day'),
        'confirm_month' => $this->input->post('confirm_month'),
        'confirm_year' => $this->input->post('confirm_year')                            
    );


    $insert = $this->db->insert('fixtures', $add_new_fixture);


    return $insert;

}


function get_divisions()
{
      $this->db->from('divisions');
      $this->db->order_by('division_name');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a division';
        $return[$row['id']] = $row['division_name'];
            }
        }

    return $return;
}

function get_teams()
{
      $this->db->from('teams');
      $this->db->order_by('team_name');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a team';
        $return[$row['id']] = $row['team_name'];
            }
        }

    return $return;
}

function list_teams() {
    $this->db->from('teams');
    $this->db->where('division_id', $this->input->post('id'));
    $this->db->order_by('team_name');
    $result = $this->db->get();

    return $result;
}

function get_referee() {        
      $this->db->from('referees');
      $this->db->order_by('id');
      $result = $this->db->get();
      $return = array();
      if($result->num_rows() > 0) {
        foreach($result->result_array() as $row) {
        $return[''] = 'Select a referee';
        $return[$row['id']] = $row['first_name'].' '.$row['surname'];
            }
        }

    return $return;

}

}

查看:

    <?php echo form_open('add_fixture/create') ;?>  

<label for="division" class="label">Division:</label><?php $js = 'id="division"'; echo form_dropdown('division', $division_list, set_value('division'), $js);  ?>

JavaScript的:

    <script type="text/javascript">
      $(document).ready(function() {
        $('select#division').change(function() {
               var div = $(this).val();   
               if(div != '') {
                         $.post("/add_fixture/list_teams/",{division_id: div}, function(data){
                                  $("select#team_name").html(data);
                         });
               }
      });
      }); 
</script>

非常感谢任何帮助:)

0 个答案:

没有答案