weixin_33691598 2015-02-08 18:15 采纳率: 0%
浏览 17

无法排序Ajax下拉列表

I have 2 dropdowns, second dropdown is dependent to first dropdown. Everything works fine except the second ajax dropdown is not sorting alphabetically. Here is my code : View :

$('#ctid').change(function() {
        $("#sditid > option").remove();
        var ctid = $('#ctid').val();
        if (ctid === '') {
            return false;
        }
        $.ajax({
            type: "POST",
            url: "<?php echo base_url() ?>items/get_dropdown_by_ajax/" + ctid,
            success: function(sditid) {
                var opt = $('<option />');
                opt.val('');
                opt.text('-- Select Item --');
                $('#sditid').append(opt);
                $.each(sditid, function(itid, item) {
                    var opt = $('<option />');
                    opt.val(itid);
                    opt.text(item);
                    $('#sditid').append(opt);
                });
            }
        });
    });

Dropdown form (view):

<?php echo form_label('Item: ', 'sditid') ?>
<?php echo "<select name='sditid' id='sditid'><option value=''>-- Select Item --</option></select>" ?>

Controller:

function get_dropdown_by_ajax($itctid) {
    $data = $this->items_m->get_dropdown_by_ajax($itctid);
    header('Content-Type: application/x-json; charset=utf-8');
    echo json_encode($data);
}

Model :

function get_dropdown_by_ajax($itctid) {
        $this->db->select('itid, itnm');
        $this->db->where('itctid', $itctid);
        $this->db->order_by('itnm', 'asc');
        $data = $this->db->get('items');
        if ($data->num_rows() > 0) {
            foreach ($data->result() as $item) {
                $data_arr[$item->itid] = $item->itnm;
            }
            return $data_arr;
        }
    return;
}

Items Table :

CREATE TABLE IF NOT EXISTS `items` (
  `itid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `itctid` int(11) unsigned NOT NULL,
  `itnm` varchar(255) NOT NULL,
  `itmrp` decimal(10,2) NOT NULL,
  `itstkcnt` varchar(10) NOT NULL,
  `itrol` decimal(10,2) NOT NULL,
  `ituom` varchar(150) NOT NULL,
  `itrmks` varchar(255) NOT NULL,
  `itcrid` int(11) unsigned NOT NULL,
  `itcrdtm` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `itmdfid` int(11) unsigned DEFAULT NULL,
  `itmdfdtm` datetime DEFAULT NULL,
  PRIMARY KEY (`itid`),
  UNIQUE KEY `itnm` (`itnm`),
  KEY `itctid` (`itctid`),
  KEY `itcrid` (`itcrid`),
  KEY `itmdfid` (`itmdfid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

Please Help! i had tried searching everywhere and spent almost a week now.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败
    • ¥15 Centos7 / PETGEM
    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
    • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗