无法使用Modal中的Form在JSGrid上过滤和显示数据

时间:2018-05-25 12:01:09

标签: php jquery jsgrid

我正在使用JSGrid表。 Jsgrid documentation链接。 我在桌子上面做了一个名为“过滤器”的按钮。我想基于Filter_form数据过滤Jsgrid表的数据。我一直在通过在Filter_form(Bootstrap Modal)中的“Submit”按钮上调用onclick函数来实现它。 ajax代码似乎无法正常工作。另外,如何捕获从PHP后端文件返回的数据,并在Jsgrid表中仅显示过滤后的数据。伙计们请帮我解决这个问题。非常感谢。

Jsgrid_table_image filter_form_modal_image Filter_form的HTML代码(Bootstrap Modal)

    <!-- Filter Modal Start-->
<div class="modal fade" id="filterModal" tabindex="-1" role="dialog" aria-labelledby="Filter data dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <!-- Modal Header -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                       <span aria-hidden="true">&times;</span>
                       <span class="sr-only">Close</span>
                </button>
                <h1>
                    Filter Data
                </h1>
            </div>

            <!-- Modal Body -->
            <div class="modal-body">
            <div class="container-fluid">

                <form class="form-inline" role="form" id="filter_form">

                        <div class="col-md-4">
                          <div class="form-group">
                            <label for="column_name">Column Name</label>
                                <select class="form-control" id="column_name" name="column_name" aria-label="Column Name" style="width:100%;">
                                    <option value="Standard Name">Standard Name</option>
                                    <option value="Status">Status</option>
                                </select>
                          </div>
                        </div>

                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="condition">Condition</label>
                                <select class="form-control" id="condition" name="condition" aria-label="condition" style="width:100%;">
                                    <option value="contains">contains</option>
                                    <option value="start with">start with</option>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-4">
                            <div class="form-group">
                                <label for="keyword">Keyword</label>
                                    <input type="text" class="form-control" id="keyword" name="keyword" placeholder="Enter keyword" aria-label="keyword" style="width:100%;">
                            </div>
                        </div>                  



                <div class="col-md-12" style="padding-top: 2rem;">
                <button type="submit" class="btn btn-primary submitBtn" onclick="submitFilterForm()">Submit</button>
                </form>
                </div>
               </div>

            </div>

            <!-- Modal Footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<!--Filter Modal End-->

调用AJAX函数提交按钮

    function submitFilterForm(){
    var form = $("#filter_form");
    var column_name = $("#column_name").val();
    var condition = $('#condition').val();
    var keyword = $('#keyword').val();
    if(keyword == '' ){
        alert('Please enter keyword.');
        $('#keyword').focus();
        return false;
    }else{
        $.ajax({
            type:'POST',
            url:'submit_filterform.php',
            data:form.serialize(),
            beforeSend: function() {
                $('.submitBtn').attr("disabled","disabled");
                $('.modal-body').css('opacity', '.5');
            },
            success:function(data){
                alert(data.message);
                if(JSON.stringify(data) == 'filtered'){
                    $('#keyword').val('');
                    $('.submitBtn').removeAttr("disabled");
                    $('.modal-body').css('opacity', '');
                    //$("#jsGrid").val(data);
                    $("#jsGrid").jsGrid({
                       onDataLoaded: function(args) {
                          // data is loaded, so do whatever you want, here you have also access to loaded data with args.data
                          alert('Hi I have loaded again');
                       }
                    });
                    //$('.modal').hide();
                    //$("#jsGrid").jsGrid("loadData");
                }else{
                    //$('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>');
                    alert('Some error occurred while filtering data, please refresh page and try again.');
                }
            }
        });
    }
}

PHP后端查询

    switch($_SERVER["REQUEST_METHOD"]) {

    case "POST":
        $result = sqlsrv_query($con,"SELECT * FROM report_standard WHERE ".$_POST['column_name']." LIKE '%".$_POST['keyword']."%'");
        //$params = array($_POST['column_name'],$_POST['keyword']);
        if ($result == false) {

            echo "no data";
            exit();
        }
        else {
            while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
                $data[] = $row;
            }

            $finalArr['data'] = $data;
            $finalArr['itemsCount'] = count($data);
            $finalArr['message'] = 'filtered';
            echo json_encode($finalArr);
        }
        break;

}

1 个答案:

答案 0 :(得分:0)

我终于能够得到解决方案了。请在下面找到更改。

  

调用AJAX函数提交按钮

function submitFilterForm() {
    var form = $("#filter_form");
    var column_name = $("#column_name").val();
    var condition = $('#condition').val();
    var keyword = $('#keyword').val();
    if (keyword == '') {
        alert('Please enter keyword.');
        $('#keyword').focus();
        return false;
    } else {
        $("#filterModal").modal("hide");

        //Simply load the jsGrid again on the same id
        $("#jsGrid").jsGrid({
            height: "400px",
            width: "100%",
            editing: true,
            inserting: true,
            filtering: false,
            paging: true,
            autoload: true,
            pageSize: 15,
            pageButtonCount: 5,
            rowNum: 10,
            controller: {

                loadData: function(filter) {
                    return $.ajax({
                        type: "POST",
                        url: "../submit_filterform.php",
                        data: form.serialize(),
                        dataType: "json"
                    }).then(function(result) {
                        console.log(result);
                        return result.data;
                    });

                },
                fields: [
                    //Fields as required based on the data
                ]
            }
        });
    }
}