Search code examples
javascriptjavamysqlwebjqgrid

How does jqGrid dynamically populate the list of options based on status data?


I am currently using jQgrid to draw a list and data is being retrieved through Ajax. I'm getting the list back normally, and there's nothing wrong with it.

My problem is that I have to dynamically populate the list of options based on the status value I'm getting.

A place to call data:

$(function(){

    search_provider();


    // grid resize
    $(window).on('resize.jqGrid', function() {
        $("#requestList").jqGrid('setGridWidth', $(".grid-cover").width());
    })


});

function search_provider() {


    var queryData = $("#searchList").serialize();

    $.ajax({
        url : "/v1/point/admin/provider/game_provider_list",
        type : "GET",
        dataType : "json",
        data: queryData,
        success : function(result) {
            $("#resultLength").text(result.jqgrid_data.length);
            if(result.jqgrid_data.length == 0){
                noData();
            }else{
            $('#grid-cover').show();
            $('#no-data').hide();
            setRequestList(result.jqgrid_data)
            }
        }
    })  
}

a place to be filled dynamically:

{
            name : 'approval_status',
            index : 'approval_status',
            align : 'center',
            editable : true,
            edittype : 'select',
            formatter : 'select',
            editoptions : {
                value : "0:Unauthorized;1:Approval;2:Hold;3:Denial of approval;4:Reclamation",
                dataEvents : [{
                    type : 'change',
                    fn : function(e) {
                        ...
                    }
                }]
            }
        }

It is now showing all the lists.

When the value of approval_status is '0' return "0:Unauthorized;1:Approval;2:Hold;3:Denial of approval"

When the value of approval_status is '1' return "1:Approval;4:Reclamation"

When the value of approval_status is '2' return "1:Approval;2:Hold;3:Denial of approval"

When the value of approval_status is '3' return "1:Approval;2:Hold;3:Denial of approval"

I want to be changed as above. How can you solve this problem?


Solution

  • Specify the same name of the data variable that is imported from the DB and register the conditional statement through the status value to obtain the DB data that matches the criteria. And draw a JQgrid to fit the data.

    MyBatis.xml

    select id="list" parameterType="hashmap" resultType="hashmap">
                <choose>
                    <when test='approval_status == "0"'>
                                                SELECT
            seq_no AS col1, 
            nick_name AS col2,
            ...
            FROM DB_DB
                    <when test='approval_status == "1"'>
                                                                    SELECT
             seq_no AS col1, 
             ............
             </choose>
    

    JQgrid.js

    function setRequestList(jqgrid_data,status){
        var title = [];
        if(status == '0'){
            title = ['No', 'nick',... ];
        }else if(status == '1'){
            title = ['No', 'name', ... ];
        }
        var colmodel = [];
        $("#requestList").jqGrid("GridUnload");
        jQuery("#requestList").jqGrid({
            data : jqgrid_data,
            datatype : "local",
            height : 'auto',
            colNames : title,
            colModel : [{
                name : 'col1',
                index : 'seq',
                align : 'center',
                sortable : false
            }
            ...