dtup3446 2019-03-08 13:53
浏览 147

使用select2插件进行多选,通过网络中的AJAX获取数据,返回一个空数组

I have this code in JS. Data results are loaded via handle from Presenter.

      $('.selectTypeAhead').select2({
            multiple: true,
            ajax: {
                url: url,
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        query: params.term,
                        page: params.page || 1
                    };
                },
                processResults: function (data, params) {
                    return {
                        results: JSON.parse(data.results),
                        pagination: {
                            more: true
                        }
                    };
                },
                cache: true
            },
            escapeMarkup: function (markup) {
                return markup;
            },
            minimumInputLength: 2,
            language: 'cs',
            templateResult: function (result) {
                return '<div>' + result.text + '</div>'
            },
            templateSelection: formatRepoSelection
        });

        function formatRepoSelection(repo) {
            return repo.full_name || repo.text;
        }


    }
});

Handle sending data into JS:

public function handleSelect2Array()
{

    $testData[] = [
        'id'=> '1',
        'text' => 'example'
    ];
    $this->payload->results = json_encode($testData);
    $this->sendPayload();
}

Form factory in nette

 public function createForm()
{
    $form = new Form();
    $form->addMultiSelect('multiselect', 'label description' );
    $form->addSubmit('send', 'Uložit');
    return $form;
}

Presenter stuff

   protected function createComponentForm()
{
    $form = $this->FormFactory->createForm();
    $form->onSuccess[] = [$this, 'FormSucceeded'];     
    return $form;
}

And finally, here is my latte template in nette:

     <div class="container">
        {snippet examplesnippet}
            {form Form, class=>'form'}
            <div class="modal-body">

                <div n:class="form-group">
                    <div class="input-group" id="select2example" data-link=" 
                     {link select2Array!}">
                        <div class="input-group-addon">
                            {label multiselect}
                        </div>
                            {input multiselect, class=>' form-control selectTypeAhead'}
                    </div>
                </div>
            {/form}
        {/snippet}
      </div>

Everything works all right. I can select multiple contents in my multiselect box on the page. Problem is, when I click on submit button. I get all other values from the form, but the multiselect returns empty array. I have tried to play with snippets and stuff around ajax in nette, but I found out that the problem is most possibly in configuration of select2...what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongsao8279 2019-06-16 19:56
    关注

    I had same problem. When you are using multiselect, you must also specify values on Nette side (It's probably for some security reasons). Make sure if you have same ids on both sides. :)

    Nette form:

    $form->addMultiSelect('multiselect', 'label description', [
    1 => 'item',
    2 => 'another item'
    ] );
    

    JSON response for select2:

    {
      "results": [
        {
          "id": 1,
          "text": "item"
        },
        {
          "id": 2,
          "text": "another item"
        }
      ],
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?