简体   繁体   English

Select2 转换数据

[英]Select2 Transform Data

I have data like this one.我有这样的数据。

kode科德 explanation解释
082.B.2 082.B.2 Test1测试1
083.B.3 083.B.3 Test2测试2

Here is my function:这是我的 function:

       public function selectprogram(Request $req)
      {
        if ($req->ajax())
        {
          $search = $req->get('q');
          $page = $req->get('page');
          $resultCount = 10;
          $offset = ($page - 1) * $resultCount;
          $data = Refprogram::where('kode_program','LIKE','%'.$search .'%')
          ->orWhere('uraian','LIKE','%'.$search.'%')
          ->orderby('kode_program')
          ->skip($offset)
          ->take($resultCount)
          ->get(['kode_program',DB::raw("CONCAT(kode_program,' - ', uraian) AS text")]);
           $count = Refprogram::count();
           $endCount = $offset + $resultCount;
           $morePages = $endCount < $count;
           $results = array(
                "results" => $data,
                "pagination" => array(
                  "more" => $morePages
                )
              );
          return response()->json($results);
        }
      }

Here is my JS Select2:这是我的 JS Select2:

        $('#select2-program').select2({
            multiple:false,
            placeholder:'Please select one',
            allowClear: true,
            pagination:true,
            ajax : {
              method : 'get',
              url : '{{$breadcrumbs['select2_url_program']}}',
              data :function (params) {
                return {
                  q: params.term || '',
                  page: params.page || 1
                }
               
              },

              cache:true
            }
          });

My question and problem is, how can i change or transform default set id by select2 that using numeric into text.我的问题是,我如何通过使用数字的 select2 将默认设置 id 更改或转换为文本。 I want use kode as a value for that, not id (numeric).我想使用 kode 作为它的值,而不是 id(数字)。

 $('#mySelect2').select2({ ajax: { url: '/example/api', processResults: function (data) { // Transforms the top-level key of the response object from 'items' to 'results' return { results: data.items }; } } });

https://select2.org/data-sources/ajax#transforming-response-data https://select2.org/data-sources/ajax#transforming-response-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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