简体   繁体   中英

Select2 set selected not working on pagination option

I've using select2 pagination

here is my javascript code :

$("#propinsi").select2({
            width: "100%",
            placeholder: "Provinsi",
            ajax: {
                url: "{{ route('propinsi') }}",
                dataType: "json",
                data: function (params) {
                    return {
                        term: params.term || '',
                        page: params.page || 1,
                        page_limit: 10,

                    };
                }
                // cache: true
            }
        });

and then passing to the method on controller :

public function provinsi($page, $term, $page_limit, $kode_negara){                                        
        $offset = ($page - 1) * $page_limit;
        $result = [];
        $query = DB::table('m_wilayah_provinsi')->where(function($query) use ($kode_negara) {
            $query->where('id_negara', $kode_negara);
        })->select('id AS id', 'provinsi AS text');

        // if (!empty($term)) {
        //     $query->where('provinsi', 'like', '%'.$term.'%');  
        // }

        $count     = $query->count();
        $endCount  = $offset + $page_limit;
        $morePages =  $count > $endCount;
        $query     = $query->skip($offset)->take($page_limit)->get();

        $result = [
            'results'    => $query,
            'pagination' => [
                'more' => $morePages
            ]
        ];      

        return response()->json($result);
    }    

在此处输入图片说明

for displaying json data is working fine,but i cannot set selected.

I've try set setected like this but still not working :

$("#propinsi").val(1).trigger("change");

and

$("#propinsi").val(1);

Anyone can help me out ?

尝试这个:

$("#propinsi").select2('data', {id: 100, a_key: 'Lorem Ipsum'});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM