0

Double 如何获得我正在使用 Codeigniter 和 Select2 jQuery 的下拉列表的正确选择值?

问题:我有这个结果代理我错过了什么?

html视图:

<div class="form-group">

                                    <label class="control-label full-width alert-info">Le Groups</label>
                                    <select class="full-width" id="groupslist" data-placeholder="Groups Name" name="products_groups_id">
                                      <option></option>
                                    </select>
                                  </div>

groups_model

__getGroupsList /** * 根据机构 id 创建群组列表 */

function __getGroupsList()
{

    //profiling
    $this->data['controller_profiling'][] = __function__;

    //load the models that we will use
    $this->load->model('products_model');

    //get post daataa
    $agencys_id = $this->input->post('data_mysql_record_id');

    //flow control
    $next = true;

    //validate post
    if (!is_numeric($agencys_id)) {

        //log this error
        log_message('error', 'AJAX-LOG:: [FILE: ' . __file__ . ']  [FUNCTION: ' . __function__ . ']  [LINE: ' . __line__ . "]  [MESSAGE: Fetch projects list error: invalid input]");

        //halt
        $next = false;

    }

    //get all milestones for this project
    if ($next) {

        //get list of milestone for the project
        $result = $this->products_model->getGroups('groups_name', 'ASC', $agencys_id, '');
        $this->data['debug'][] = $this->products_model->debug_data;

        if (is_array($result)) {

            //create a list from results
            $list = create_pulldown_list($result, 'groups', 'id');

            //json output
            $this->jsondata = array($list);

            //send headers
            header('HTTP/1.0 200 OK', true, 200);

        } else {

            //log this messsage
            log_message('error', 'AJAX-LOG:: [FILE: ' . __file__ . ']  [FUNCTION: ' . __function__ . ']  [LINE: ' . __line__ . "]  [MESSAGE: Database Error]");

            //halt
            $next = false;
        }
    }

    //an error occurred, send to javascript
    if (!$next) {

        //json output
        $this->jsondata = array(
            'result' => 'failed',
            'message' => $this->data['lang']['lang_requested_item_not_loaded'],
            'debug_line' => __line__);

        //send headers
        header('HTTP/1.0 400 Bad Request', true, 400);
    }

    //log debug data
    $this->__ajaxdebugging();

    //load the view for json echo
    $this->__flmView('common/json');
}

AJAX 控制器:

function getGroups($orderby = 'groups_name', $sort = 'ASC', $agencys_id = '')
{

    //profiling::
    $this->debug_methods_trail[] = __function__;

    //declare
    $conditional_sql = '';

    //check if any specifi ordering was passed
    if (!$this->db->field_exists($orderby, 'groups')) {
        $orderby = 'groups_name';
    }

    //check if sorting type was passed
    $sort = ($sort == 'asc' || $sort == 'desc') ? $sort : 'ASC';

    //if project_id has been specified, show only for this project
    if (is_numeric($agencys_id)) {
        $conditional_sql .= " AND groups_agencys_id = $agencys_id";
    }


    //----------sql & benchmarking start----------
    $this->benchmark->mark('code_start');

    //----------monitor transaction start----------
    $this->db->trans_start();

    //_____SQL QUERY_______
    $query = $this->db->query("SELECT *
                                      FROM groups
                                      WHERE 1 = 1
                                      $conditional_sql
                                      ORDER BY $orderby $sort");

    $results = $query->result_array(); //multi row array

    //----------monitor transaction end----------
    $this->db->trans_complete();
    $transaction_result = $this->db->trans_status();
    if ($transaction_result === false) {

        //log this error
        $db_error = $this->db->_error_message();
        log_message('error', '[FILE: ' . __file__ . ']  [FUNCTION: ' .      __function__ . ']  [LINE: ' . __line__ . "]  [MESSAGE: Database Error -     $db_error]");

        return false;
    }

    //benchmark/debug
    $this->benchmark->mark('code_end');
    $execution_time = $this->benchmark->elapsed_time('code_start', 'code_end');

    //debugging data
    $this->__debugging(__line__, __function__, $execution_time, __class__, $results);
    //----------sql & benchmarking end----------

    //return results
    return $results;

}

Javascript:

    $(document).ready(function(){
$("#agencyslist").change(function(){
    $("#groupslist").empty();
    var data_mysql_record_id=$("#agencyslist").val();
    var data_ajax_url=$(this).attr("data-ajax-url");{
        $.ajax({
        type:'post',
        url:data_ajax_url,
        dataType:'json',
        data:'data_mysql_record_id='+data_mysql_record_id,

        success:function(data)
        {

            $.each(data, function(groups, id) 
                {
                    var opt = $('<option />'); 
                    opt.val(id);
                    opt.text(groups);
                    $('#groupslist').append(opt); 
                });
        },
4

0 回答 0