简体   繁体   English

使用Lou Jquery MultiSelect更新从数据库中提取的值

[英]Updating values being pulled from database using lou jquery multiselect

I am using lou's multiselect.js to update selected values that were previously stored in the database. 我正在使用lou的multiselect.js更新以前存储在数据库中的选定值。 I am getting the selected values from the database, however it's in the wrong column, as shown below. 我正在从数据库中获取选定的值,但是它在错误的列中,如下所示。 I would like to retrieve the values from the database that weren't selected(to be on the lef) along with those that have already been selected(to be on the right). 我想从数据库中检索未选中的值(位于左侧)以及已选中的值(位于右侧)。


The list of items below(on the left) were already selected items being brought back from the database, but they should be under the Selected Product Types column instead. 下面(左侧)的项目列表已经从数据库中带回了选定的项目,但是它们应该位于“ 选定的产品类型”列下。 And then under Available Product Types the list of items that have not been selected as yet should be shown. 然后在“ 可用产品类型 ”下,应显示尚未选择的项目列表。

在此处输入图片说明


HTML HTML

This gets the previously selected products from the database as shown in the picture above 如上图所示,这将从数据库中获取先前选择的产品

<select multiple id="product-multi-select" name="product-multi-select" ng-model="input_doc_type.product" required>
    @foreach($inputDocData[0]->product as $data)
    <option value="{{$data->id}}" selected>{{$data->name}}</option>
    @endforeach
</select>

JQuery JQuery的

Gets all the products 获取所有产品

<script type="text/javascript">
jQuery(function ($) {

    $("#product-multi-select").multiSelect({
        selectableHeader: "<div class='custom-header'>Available Product Types</div>",
        selectionHeader: "<div class='custom-header'>Selected Product Types</div>"
    });

    $(document).ready(function() {

        var products = [];

        $.get('/admin/products/all-products', function(data, response) {
            $.each(data, function(index, value) {
                products.push(value.name);
            });
            $("#product-multi-select").multiSelect('select', products);
            $(".ms-container").append('<i class="glyph-icon icon-exchange"></i>');
        });

    });

});
</script>

I'm not sure how to go about using this. 我不确定如何使用它。 Any assistance with this problem would be greatly appreciated. 任何有关此问题的帮助将不胜感激。

To solve the problem I had to add an extra function in the js file which supports getting recognizing selected values (shown below). 为了解决该问题,我必须在js文件中添加一个额外的功能,该功能支持识别选定的值(如下所示)。 It's a replica of the addOption function that came with the file, but with one slight update. 它是文件随附的addOption函数的副本,但稍有更新。

multiselect.js multiselect.js

'selectedAddOption' : function(options){
        var that = this;

        if (options.value) options = [options];
        $.each(options, function(index, option){
            if (option.value && that.$element.find("option[value='"+option.value+"']").length === 0){
                var $option = $('<option value="'+option.value+'" selected>'+option.text+'</option>'),
                    index = parseInt((typeof option.index === 'undefined' ? that.$element.children().length : option.index)),
                    $container = option.nested == undefined ? that.$element : $("optgroup[label='"+option.nested+"']")

                $option.insertAt(index, $container);
                that.generateLisFromOption($option.get(0), index, option.nested);
            }
        })
    },

After adding that piece of code to the js file. 将那段代码添加到js文件之后。 I then made a request from the database which pulled in all unused products and then dynamically them to the select (shown below). 然后,我从数据库发出一个请求,该请求提取了所有未使用的产品,然后将它们动态地选择 (如下所示)。 This was added over on the Available Product Types side 这被添加到“ 可用产品类型”

// retrieve all products
        $.get('/admin/products/all-products', function(data, response) {
            $.each(data, function(index, value) {
                $('#product-multi-select').multiSelect('addOption', { value: value.id, text: value.name, index: 0 });
            });
            $("#product-multi-select").multiSelect('refresh');
            $(".ms-container").append('<i class="glyph-icon icon-exchange"></i>');
        });

Finally, I then made another request which pulled in all products that were already selected, and this utlilized the selectedAddOption which I added to the js file. 最后,然后我又提出了一个请求,该请求提取了所有已选择的产品,并使用了我添加到js文件中的selectedAddOption And these were then also added to the select box, over on the Selected Product Types side 然后,将这些也添加到选择框的“ 选定产品类型”一侧

// retrieve all selected products
        <?php foreach ($inputDocData[0]->product as $val) { ?>
            $('#product-multi-select').multiSelect('selectedAddOption', { value: <?php echo $val->id; ?>, text: '<?php echo $val->name; ?>', index: 0 });
        <?php } ?>
        $("#product-multi-select").multiSelect('refresh');
        $(".ms-container").append('<i class="glyph-icon icon-exchange"></i>');

There are no present because they are dynamically added using jquery. 目前没有,因为它们是使用jquery动态添加的。

select multiple id="product-multi-select" name="product-multi-select[]" ng-model="input_doc_type.product" required></select>

The code did what it was supposed to do and there were no duplication of items in the list. 该代码完成了应做的工作,并且列表中没有重复的项目。

I hope this may become helpful to someone else. 我希望这可能对其他人有所帮助。

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

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