如何以多重引导方式获取MySQL数据?

时间:2018-07-13 15:36:18

标签: javascript php jquery mysql ajax

我为MySQL数据库创建了一个管理面板,当我单击“编辑”按钮时,我想打印Bootstrap multi select中的所有数据。如果我不使用Bootstrap多项选择,而仅使用普通的多项选择,那么它将起作用。我该怎么办?

这是它的样子(在按钮中,它显示当前数据,但是当我打开时,它没有显示在选择菜单中):

enter image description here

index.php

<select name="classification[]" id="classification" multiple="multiple">
   <?php 
      while($row = $classificationResult -> fetch_array()) {
   ?>
   <option value="<?php echo $row['classification_id'];?>"><?php echo $row['name'];?></option>
   <?php
      }
   ?>
</select>

<script>
   // If I didn't add the multi select jquery, it's working
   $('#classification').multiselect({
        nonSelectedText: 'Select Framework',
        maxHeight : 400,
        includeSelectAllOption : false,
        enableFiltering : false,
        buttonWidth : '100%',
        dropRight : true
    });

        $(document).on('click', '.edit_data', function(){
            $("#classification option").prop("selected", false);
            var data_id = $(this).attr("id");  
            $.ajax({  
                url:"fetchRole.php",  
                method:"POST",  
                data:{'data_id':data_id},  
                dataType:"json",  
                success:function(data){  
                    $.each(data.classifications, function(i, e) {
                        $("#classification option[value='" + e + "']").prop("selected", true);
                    });
                    $('#data_id').val(data.id);  
                    $('#insert').val("Edit");  
                    $('#add_data_Modal').modal('show');  
                }  
            });  
        });  
</script>

fetchRole.php

$query2 = $conn -> prepare("SELECT classification.classification_id AS class_id
            FROM classification
            LEFT JOIN role_classification ON classification.classification_id = role_classification.classification_id
            WHERE role_classification.role_id = ?");
        $query2 -> bind_param('i', $role['id']);
        $query2 -> execute();
        $result2 = $query2 -> get_result();
        $query2 -> close();

        while ($classification = $result2 -> fetch_assoc()) {
            $classificationIdList[] = $classification["class_id"];
        }



        $return = array_merge($role, ["classifications" => $classificationIdList]);

    echo json_encode($return);

1 个答案:

答案 0 :(得分:1)

使用.prop("selected", true)设置新的选定选项后,您需要刷新多选。

$.each(data.classifications, function(i, e) {
    $("#classification option[value='" + e + "']").prop("selected", true);
});

$('#classification').multiselect('refresh');

refresh方法的说明:

  

此方法用于根据以下内容刷新选中的复选框:   选择中当前选择的选项。

您可以查看可用方法HERE的完整列表。