使用搜索诊断上的自定义按钮保存 jqgrid 的搜索过滤器


Save Search Filter Of Jqgrid Using Custom Button On Search Diaglog

>我在jqgrid的搜索对话框中添加了一个按钮,用于将搜索条件保存在数据库中,以供以后使用。单击该按钮时,我需要搜索条件的过滤器属性jqgrid。

https://i.stack.imgur.com/31J5x.png[快照]当用户单击保存按钮(我在搜索对话框中添加的自定义按钮)时,我使用对话框询问过滤器名称,然后将其与过滤器一起保存。

单击"搜索"按钮时,我不想根据搜索条件重新加载网格。

但是我无法将过滤器属性存储在数据库中。

我在重绘事件后使用了,但这不是我添加的最后一个过滤器。

所以请尽快回复我。

提前谢谢。

var grid = jQuery("#list_records").jqGrid({
        url: "getGridData.php?" + window.location.search.substring(1),
        searchurl: "getGridData.php?" + window.location.search.substring(1),
        datatype: 'json',
        mtype: "GET",
        colNames: ["Task Id", "Title", "Priority", "tags", "Created Date", "Last Update Date", "Complete Date"],
        colModel: [
                    { name: 'Task_ID', index: 'Task_ID', width: 200},
                    { name: 'Title', index: 'Title', width: 200 },
                    { name: 'Priority', index: 'Priority', width: 200},
                    { name : 'tags', index : 'tags', width : 200},
                    { name : 'Created_Date', index : 'Created_Date', width : 200},
                    { name : 'Last_Updated', index : 'Last_Updated', width : 200},
                    { name : 'Completed_date', index : 'Completed_date', width : 200}
                ],
        sortname: 'Task_ID',
        viewrecords: true,
        rownumbers: true,
        sortorder: "desc",
        ignoreCase: true,
        pager: '#perpage',
        caption: "Task Results",
        rowNum: 30,
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
        });
grid.jqGrid('navGrid','#perpage', {add:false,edit:false,del:false,search:false,refresh:false}).jqGrid('navButtonAdd','#perpage',{
               caption: "",
               title : "Search",
               id : "filterManipulation",
               buttonicon:"ui-icon-search", 
               onClickButton: function (){
                    //console.log(JSON.stringify(filterForSearch));
                    $("#list_records").setGridParam({
                         postData: { filters: JSON.stringify(filterForSearch)}
                    });
                    $("#list_records").jqGrid('searchGrid', 
                        {multipleSearch:true, overlay:false, searchOnEnter:true, multipleGroup:true, closeOnEscape:true, showQuery:true,recreateFilter: true,
                        afterShowSearch : function(){
                            var element = document.getElementById("fbox_list_records_2").getElementsByTagName("tr")[1].getElementsByTagName("td")[1];
                            var saveAnchor = document.createElement('a');
                            saveAnchor.setAttribute('id', 'fbox_list_save_query');
                            saveAnchor.setAttribute('class', 'fm-button ui-state-default ui-corner-all fm-button-icon-left');
                            saveAnchor.setAttribute('onclick','saveSearch()');
                            saveAnchor.innerHTML = buttonName;
                            var newAnchor = document.createElement('a');
                            newAnchor.setAttribute('id', 'fbox_list_new_filter');
                            newAnchor.setAttribute('class', 'fm-button ui-state-default ui-corner-all fm-button-icon-left');
                            newAnchor.setAttribute('onclick','newFilter()');
                            newAnchor.innerHTML = 'New';
                            element.appendChild(newAnchor);
                            element.appendChild(saveAnchor);                    
                            var tableDataTag = document.createElement("td");
                            tableDataTag.setAttribute('id','tdForFilterNameList');
                            var filterNameList = document.createElement("select");
                            filterNameList.setAttribute('id','filterNameList');
                            filterNameList.setAttribute('onChange','loadFilterAttribute()');
                            var option = new Option("--Select--", "0");
                            filterNameList.appendChild(option);
                            <?php
                                $userId = 0;
                                $conn = new mysqli("localhost", "root", "$$$$", "$$$");
                                if (mysqli_connect_errno()){
                                    echo "Failed to connect to MySQL: " . mysqli_connect_error();
                                }
                                $sql = "select filter_id,filter_name from filters where userID = $userId ";
                                //print $sql;
                                $result = $conn->query($sql);
                                if($result)
                                {
                                    while ($row = $result->fetch_assoc()) {
                            ?>
                                        var optionTag = document.createElement("option");
                                        optionTag.setAttribute("value", "<?php echo $row['filter_id']?>");
                                        var text = document.createTextNode("<?php echo $row['filter_name']?>");
                                        optionTag.appendChild(text);
                                        if(selectedValue == "<?php echo $row['filter_id']?>"){
                                            optionTag.setAttribute("Selected", "true");
                                        }
                                        //option = new Option('<?php echo $row['filter_name']?>', '<?php echo $row['filter_id']?>');
                                        filterNameList.appendChild(optionTag);
                            <?php 
                                    }
                                }
                            ?>
                            tableDataTag.appendChild(filterNameList);
                            var elementForDropDown = document.getElementById("fbox_list_records").getElementsByTagName("table")[0].getElementsByTagName("tr")[0];
                            elementForDropDown.appendChild(tableDataTag);
                        },
                        onSearch: function() {
                            var postData = grid.jqGrid('getGridParam','postData');
                            var $filter = $("#" + $.jgrid.jqID("fbox_" + this.id)),
                            sql = $filter.jqFilter('toSQLString');
                            $("#list_records").setGridParam({url: "getGridData.php?" + window.location.search.substring(1) + "&sqlQuery=" + escape(sql)});
                            $("#list_records").trigger("reloadGrid");
                            loadgridData(postData, sql);    
                            fusionChart(postData, sql); 
                            return false;
                        }, 
                        onReset : function() {
                            var postData = grid.jqGrid('getGridParam','postData');
                            $("#list_records").setGridParam({url: "getGridData.php?" + window.location.search.substring(1)});
                            $("#list_records").trigger("reloadGrid");
                            loadgridData(postData, ''); 
                            fusionChart(postData, '');  
                            return false;
                        },
                        onClose: function(){
                            filterForSearch = "";
                            buttonName = "Save";
                            selectedValue = "";
                        },
                        afterRedraw: function (p) {
                            console.log(p.filter);
                        }
                    });
                }, 
               position:"last"
        });

在上面的代码中,我添加了自定义导航按钮。此代码用于加载用户的搜索过滤器。存储和编辑过滤器的方式。

但是我应该怎么做才能在保存按钮单击的位置获取过滤器。

https://i.stack.imgur.com/sU00x.png

感谢@jtc审查我的问题。我问题的答案已经在我的问题中。

基本上我已经在高级搜索对话框中实现了保存,更新,删除功能。

在搜索对话框中,我列出了特定用户的所有过滤器。

用户可以创建新筛选器,也可以通过选择筛选器来更新和删除筛选器。

因此,稍后当用户再次登录时,他/她对过滤器进行了所有操作,并从过滤器列表中选择特定字段并相应地执行搜索。

因此,如果有人需要代码,请告诉我此功能。

谢谢