dongpi9164 2017-02-10 08:41 采纳率: 0%
浏览 74

用PHP设置select2表单

I'd like to insert the function select2TemplateResult as attribute 'templateResult' for a select2 form. I don't understand how the attribute 'templateResult' can be set as a custom function which I've defined in a .js file which is called before than creating the select2 component.

lib.js

function select2TemplateResult(data, container) {
    console.log('aaa');
    if (data.element) {
        $(container).addClass($(data.element).attr("status"));
        console.log($(data.element).attr("status"));
    }
    return data.text;
}

form.php

<script type="text/javascript" src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="lib.js"></script>

<div class="form-group {{ $errors->has('elements') ? 'has-error' : '' }}">
    <label>Example</label>
    <select name="elements[]" class="form-control select2" multiple="multiple" templateResult=select2TemplateResult() data-placeholder="Select an element" style="width: 100%;">
        <?php
        $key_name = "name";
        $key_status = "status";
        foreach ($elementSource->getElements() as $v){
            echo "<option selected='selected' status=$v[$key_status]>$v[$key_name]</option>";
        }
        ?>
    </select>
</div>

The php code is inside a laravel .blade.php file, and I need to add a class to the select2 pills (both selections and options, but let's just deal with the templateResult function for a sake of simplicity) in order to modify their appearance with a .css file. I know that by creating the select2 element with javascript this would be easy, but I'd like to keep creating the select2 form with the php code, if possible, in order to keep the code clean, also because I don't need always the templateResult function (there are many other cases in my pages).

There are probably many errors in this code... Please give me some hints! Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器