te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Select2 TypeError: b is undefined - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select2 TypeError: b is undefined - Stack Overflow

programmeradmin1浏览0评论

I'm using select2 to show ajax results in a dropdown but when I append data to select2 its showing error

TypeError: b is undefined

JS Code

        var baseurl = $("#baseurl").val();
        $(".myselect").select2({
            placeholder: "Select a inspector",
            allowClear: true,
            ajax: {
                url: baseurl + '/admin/getdata',
                dataType: 'json',
                type: "GET",
                quietMillis: 50,
                data: function (term) {
                    return {
                        term: term.term
                    };
                },
                results: function (data) {
                    var myResults = [];
                    $.each(data, function (index, item) {
                        myResults.push({
                            'id': item.id,
                            'text': item.firstname
                        });
                    });
                    return {
                        results: myResults
                    };
                }
            }
        });

term.term contains the value of input text in dropdown search box.

HTML

  <select class="myselect" style="width: 50% !important">
        <option></option>
        <option value="AL">Alabama</option>
        <option value="WY">Wyoming</option>
        <option value="KY">Kentucky</option>
  </select>

JSON RESPONSE

[{"id":9858,"firstname":"Testing3","status":2,"state":"VA","phone":""},{"id":9857,"firstname":"Testing2","status":2,"state":"VA","phone":""},{"id":9856,"firstname":" david polosky ","status":3,"state":"FL","phone":"(000)000-4141"}]

SELECT2 CDN LINKS

    <link rel="stylesheet" href=".0.3/css/select2.min.css">
    <script src=".0.3/js/select2.min.js"></script>

PHP SERVERSIDE CODE (LARAVEL)

 $searchtext = $request->get('term');
 $data = Inspector::latest('id')
                ->select('id', 'firstname', 'status', 'state', 'phone')
                ->where('firstname', 'LIKE', '%' . $searchtext . '%')
                ->get()->toArray();
 echo json_encode($data);

Any help is appreciated.

I'm using select2 to show ajax results in a dropdown but when I append data to select2 its showing error

TypeError: b is undefined

JS Code

        var baseurl = $("#baseurl").val();
        $(".myselect").select2({
            placeholder: "Select a inspector",
            allowClear: true,
            ajax: {
                url: baseurl + '/admin/getdata',
                dataType: 'json',
                type: "GET",
                quietMillis: 50,
                data: function (term) {
                    return {
                        term: term.term
                    };
                },
                results: function (data) {
                    var myResults = [];
                    $.each(data, function (index, item) {
                        myResults.push({
                            'id': item.id,
                            'text': item.firstname
                        });
                    });
                    return {
                        results: myResults
                    };
                }
            }
        });

term.term contains the value of input text in dropdown search box.

HTML

  <select class="myselect" style="width: 50% !important">
        <option></option>
        <option value="AL">Alabama</option>
        <option value="WY">Wyoming</option>
        <option value="KY">Kentucky</option>
  </select>

JSON RESPONSE

[{"id":9858,"firstname":"Testing3","status":2,"state":"VA","phone":""},{"id":9857,"firstname":"Testing2","status":2,"state":"VA","phone":""},{"id":9856,"firstname":" david polosky ","status":3,"state":"FL","phone":"(000)000-4141"}]

SELECT2 CDN LINKS

    <link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/css/select2.min.css">
    <script src="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/js/select2.min.js"></script>

PHP SERVERSIDE CODE (LARAVEL)

 $searchtext = $request->get('term');
 $data = Inspector::latest('id')
                ->select('id', 'firstname', 'status', 'state', 'phone')
                ->where('firstname', 'LIKE', '%' . $searchtext . '%')
                ->get()->toArray();
 echo json_encode($data);

Any help is appreciated.

Share Improve this question edited Oct 10, 2016 at 6:36 Aamir asked Oct 10, 2016 at 6:07 AamirAamir 2,2832 gold badges33 silver badges61 bronze badges 8
  • Where's the PHP code? – Rax Weber Commented Oct 10, 2016 at 6:14
  • @RaxWeber The response I shown is the json response returned by php code. – Aamir Commented Oct 10, 2016 at 6:15
  • @RaxWeber Added PHP Code. – Aamir Commented Oct 10, 2016 at 6:18
  • are you able to get into results callback and see the data? – thecodejack Commented Oct 10, 2016 at 6:20
  • @thecodejack If I alert('anything') in the callback, its not working. – Aamir Commented Oct 10, 2016 at 6:24
 |  Show 3 more ments

2 Answers 2

Reset to default 14

In your ajax configuration you use results you should use processResults

Try this

var baseurl = $("#baseurl").val();
    $(".myselect").select2({
        placeholder: "Select a inspector",
        allowClear: true,
        ajax: {
            url: baseurl + '/admin/getdata',
            dataType: 'json',
            type: "GET",
            quietMillis: 50,
            data: function (term) {
                return {
                    term: term.term
                };
            },
            processResults: function (data) {
                var myResults = [];
                $.each(data, function (index, item) {
                    myResults.push({
                        'id': item.id,
                        'text': item.firstname
                    });
                });
                return {
                    results: myResults
                };
            }
        }
    });

Aamir@, sometimes errors are misleading. If you see from your code, it doesn't have a reference to b at all. I believe that must be an error that is being thrown from a method outside of your code, due to an error in your code.

You might have to set a break point by clicking on the line number on browser console and then check the call stack to find the error in the code.

发布评论

评论列表(0)

  1. 暂无评论