简体   繁体   English

使用 select2 将 php 数组转换为 javascript 数组

[英]Convert php array to javascript array with select2

I have the following php array in a laravel variable called $salaries which is passed to a blade view:我在名为$salaries的 laravel 变量中有以下 php 数组,该变量传递给刀片视图:

array:4 [▼
   2 => "£8, Per hour"
   3 => "£10, Per hour"
   23 => "Up to £10000, Per annum"
   24 => "£10,000 - £15,000, Per annum"
]

In my blade view I have a drop which when changed, I want to load a select2 dropdown with the above options.在我的刀片视图中,我有一个下拉菜单,当更改时,我想加载一个带有上述选项的 select2 下拉菜单。 Below is my code:下面是我的代码:

$(document).on("change", ".js-selector", function() {

     var options = {!! json_encode($salaries) !!};

     $("#salary_list").empty().select2({
        data: options
     });

})

However nothing is getting loaded into the dropdown.但是,没有任何内容被加载到下拉列表中。 So I've narrowed it down to the options data for select2 not being in the correct format.因此,我将其范围缩小到 select2 的选项数据格式不正确。

When I output options variable to console I'm getting the following object as opposed to a javascript array?当我 output 选项变量到控制台时,我得到以下 object 而不是 javascript 数组?

{2: "£8, Per hour", 3: "£10, Per hour", 23: "Up to £10000, Per annum", 24: "£10,000 - £15,000, Per annum"}

How do I transform the options into correct format for the select2 data?如何将选项转换为 select2 数据的正确格式?

You have to transform your data to the correct format, here you can see the correct data format:您必须将数据转换为正确的格式,在这里您可以看到正确的数据格式:

var data = [
    {
        id: 2,
        text: '£8, Per hour'
    },
    {
        id: 3,
        text: '£10, Per hour'
    }
];

You could pass the array in the correct format to the view, something like:您可以将正确格式的数组传递给视图,例如:

$salaries = \App\Models\Salary::all(); // eloquent collection

$salaries = $salaries->map(function ($salary) {
    return ['id' => $salary->id, 'text' => $salary->text];
})->toArray();

It would give result in something like this:它会产生这样的结果:

array:1 [▼
  0 => array:2 [▼
    "id" => 2
    "text" => "£8, Per hour"
  ]
  0 => array:2 [▼
    "id" => 3
    "text" => "£10, Per hour"
  ]
]

Or you can transform the array in javascript, the Select2 documentation explains here how you can transform your data:或者您可以在 javascript 中转换数组,Select2 文档在此处解释了如何转换数据:

Select2 requires that the id property is used to uniquely identify the options that are displayed in the results list. Select2 要求 id 属性用于唯一标识结果列表中显示的选项。 If you use a property other than id (like pk) to uniquely identify an option, you need to map your old property to id before passing it to Select2.如果您使用 id 以外的属性(如 pk)来唯一标识选项,则需要先将旧属性 map 传递给 id,然后再将其传递给 Select2。

If you cannot do this on your server or you are in a situation where the API cannot be changed, you can do this in JavaScript before passing it to Select2:如果您无法在您的服务器上执行此操作,或者您处于无法更改 API 的情况,您可以在将其传递给 Select2 之前在 JavaScript 中执行此操作:

var data = $.map(yourArrayData, function (obj) {
  obj.id = obj.id || obj.pk; // replace pk with your identifier

  return obj;
});

In your case it would be something like this:在你的情况下,它会是这样的:

$(document).on("change", ".js-selector", function() {

    var options = {!! json_encode($salaries) !!};

    var data = $.map(options, function (value, key) {
        return {id: key, text: value};
    });

    $("#salary_list").empty().select2({
        data: data
    });

})

Your array must have the structure like this:您的数组必须具有如下结构:

[
   [
        'id' => 2,
        'text' => "£8, Per hour"
   ],
   [
        'id' => 3,
        'text' => "£10, Per hour"
   ],
   [
        'id' => 23,
        'text' => "Up to £10000, Per annum"
   ],
   [
        'id' => 24,
        'text' => "£10,000 - £15,000, Per annum"
   ],
]

https://select2.org/data-sources/arrays https://select2.org/data-sources/arrays

exactly as Serhii said, simply extract the values from you associative array before encoding it:正如 Serhii 所说,只需在编码之前从关联数组中提取值:

$(document).on("change", ".js-selector", function() {

     var options = {!! json_encode(array_values($salaries)) !!};

     $("#salary_list").empty().select2({
        data: options
     });

})

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

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