【问题标题】:Cannot load data in select 2 jquery ajax in codeigniter无法在codeigniter中的select 2 jquery ajax中加载数据
【发布时间】:2019-12-10 06:04:57
【问题描述】:

我想使用 ajax 加载 select2 中的数据,更改搜索字段中的文本,因为我正在使用以下代码,但它不起作用。控制台中没有错误,并且没有调用 jquery 函数。

jQuery('#t_name').select2({
  ajax: {
    url: 'get-all_data',
    processResults: function (data) {
      return {
            text: item.text,
                        id: item.id
      };
    }
  }
});

public function getAllData()
    {
        $json = [];
        $results=$this->db->select('*')->from($this->user)->get()->result_array();
        foreach($results as $row)
        {
        $json[] = ['id'=>$row['id'], 'text'=>$row['name']];
        }
        return $json;  

    }

【问题讨论】:

  • 网址正确吗?
  • 是的,它是正确的
  • getAllData() 在 php 文件中? ajax 中的 url 不指向 php 文件。
  • 在codeigniter路由中,正确
  • 我认为你需要在 "url: 'get-all_data'" 中传递完整路径,例如 "url:192.168.1.90/projectPath/get-all_data"

标签: php jquery codeigniter jquery-select2 codeigniter-2


【解决方案1】:

您应该尝试以下方法:

processResults: function (data) {
            return {
                results: $.map(data, function (item) {
                    return {
                        text: item.NAME,
                        id: item.ID
                    }
                })
            };
        }

【讨论】:

    【解决方案2】:

    试试这个

    在你的控制器中

    public function getAllData()
    {
        //if( $this->input->get('q') != '' ){do something}
    
        $json = [];
        $results=$this->db->select('*')->from($this->user)->get()->result_array();
        foreach($results as $row)
        {
            $json[] = ['id'=>$row['id'], 'text'=>$row['name']];
        }
    
        header('Content-Type: application/json');
        header('Pragma: no-cache');
        header('Cache-Control: no-store, no-cache');
        echo json_encode( $json );
    
    }
    

    在你的 javascript 中

    $(document).ready(function(){
    
    $(".select2").select2({
                minimumInputLength: 2,
                ajax: {
                    url: '<?=base_url()?>/get-all_data',
                    dataType: 'json',
                    delay: 250,
                    processResults: function (data) {
                        return {
                            results: data
                        };
                    },
                    cache: false
                }
            });
    

    });

    【讨论】:

      【解决方案3】:

      对于使用 ajax 的 select2 需要 JSON 格式的数据,并且您在 processResults 中犯了拼写错误。

      请检查以下代码。

      $('.js-data-example-ajax').select2({
          ajax: {
              url: 'get-all_data',
              dataType: 'json',
              processResults: function (data) {
                  return {
                      results: data
                  }
              }
          }
      });
      

      PHP 代码

      public function getAllData()
      {
          $json = [];
          $results=$this->db->select('*')->from($this->user)->get()->result_array();
          foreach($results as $row)
          {
              $json[] = ['id'=>$row['id'], 'text'=>$row['name']];
          }
          echo json_encode($json);
      }
      

      更新

      建议:另外,您可以在 PHP 函数中使用此代码,不需要使用 for 循环来设置日期。

      public function getAllData() {
          $results = $this->db->select('id, name as text')->from($this->user)->get()->result_array();
          echo json_encode($results);
      }
      

      【讨论】:

        【解决方案4】:

        请尝试在您的ajax url中添加项目的base_url:

            jQuery('#t_name').select2({
              ajax: {
                url: base_url + 'get-all_data',
                processResults: function (data) {
                  return {
                        text: item.text,
                                    id: item.id
                  };
                }
              }
            });
        

        【讨论】:

          猜你喜欢
          • 2011-03-30
          • 1970-01-01
          • 1970-01-01
          • 2014-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多