jqGrid 在 keyup 事件上未能正确重新加载或刷新。

huangapple go评论65阅读模式
英文:

jqGrid is not reloading or refreshing properly on keyup event

问题

I needed to create a search page where various types of text, dropdown, date field will be there. i added onkeyup function to every text field so that whenever i put any text or number a grid will appear with subgrid. The issue i am facing is if i give input for the first time then the grid appear with correct data. if i clear that field then the grid is reloading also but if i input again in that field or any other text field then the js is not sending request to the action method with updated post value.

Here is my View Part:

<section class="content">
    <div class="container-fluid">
        <!-- SELECT2 EXAMPLE -->
        <!-- SELECT2 EXAMPLE -->
        <div class="card card-info">
            <div class="card-header">
                <h3 class="card-title">Search Criteria</h3>

                <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-card-widget="collapse">
                        <i class="fas fa-minus"></i>
                    </button>
                </div>
            </div>
            <!-- /.card-header -->
            @using (Html.BeginForm("SearchReport", "LRLiveSearch", FormMethod.Post))
            {
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-12" style="line-height:1.50px;">

                            <div class="row">
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>Deed No</label>
                                        @Html.TextBoxFor(model => model.DeedNo, new { @class = "form-control", onkeyup = "LiveSearch('DN', this.value)" })
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>Deed Identity</label>
                                        @Html.TextBoxFor(model => model.DeedId, new { @class = "form-control", onkeyup = "LiveSearch('DI', this.value)" })

                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>Client Name</label>
                                        @Html.TextBoxFor(model => model.ClientName, new { @class = "form-control", onkeyup = "LiveSearch('CN', this.value)" })
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>Client Contact Number</label>
                                        @Html.TextBoxFor(model => model.ClientPhoneNumber, new { @class = "form-control", onkeyup = "LiveSearch('CP', this.value)" })
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>MR No</label>
                                        @Html.TextBoxFor(model => model.MRNo, new { @class = "form-control", onkeyup = "LiveSearch('MR', this.value)" })
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>Cheque No</label>
                                        @Html.TextBoxFor(model => model.ChequeNo, new { @class = "form-control", onkeyup = "LiveSearch('CQN', this.value)" })
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>Collection Type</label>
                                        @Html.DropDownListFor(model => model.CollectionType, Model.CollectionTypeList, new { @class = "form-control" })
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>From Date</label>
                                        <div class="input-group date" id="FirstDate" data-target-input="nearest">
                                            @Html.TextBoxFor(model => model.FromDate, new { @class = "form-control datetimepicker-input", @data_target = "#FirstDate" })
                                            <div class="input-group-append" data-target="#FirstDate" data-toggle="datetimepicker">
                                                <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label>To Date</label>
                                        <div class="input-group date" id="SecondDate" data-target-input="nearest">
                                            @Html.TextBoxFor(model => model.ToDate, new { @class = "form-control datetimepicker-input", @data_target = "#SecondDate" })
                                            <div class="input-group-append" data-target="#SecondDate" data-toggle="datetimepicker">
                                                <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
    <!-- /.container-fluid -->
    <table class="table table-bordered table-striped table-hover text-nowrap text-sm" id="Demogrid"></table>
    <div class="container-fluid" id="pager"></div>
</section>

here is my JS part where i handled onkeyup event:

function LiveSearch(type, value) {
    var searchType = type;
    var searchValue = value;
    if (value == "" || value == null) {
        $("#Demogrid").jqGrid('setGridParam', { postData: { Type: searchType, SearchValue: searchValue } }).trigger('reloadGrid');
        return true;
    }
    else {
        $("#Demogrid").jqGrid({
            url: '/HRM/LRLiveSearch/LiveSearch',
            datatype: "json",
            mtype: 'Get',
            //table header name
            colNames: ['ProjectName', 'PlotName', 'TotalLandArea', 'DeedId', 'DeedDate', 'RentFeeType', 'RentFee', 'ClientName', 'ClientAddress', 'ClientContactNo', 'EffectiveDate'],
            //colModel takes the data from controller and binds to grid
            colModel: [
                { name: "ProjectName" },
                { name: "PlotName" },
                { name: "TotalLandArea" },
                { name: "DeedId" },
                { name: "DeedDate" },
                { name: "RentFeeType" },
                { name: "RentFee" },
                { name: "ClientName" },
                { name: "ClientAddress" },
                { name: "ClientContactNo" },
                { name: "EffectiveDate" }
            ],
            height: '100%',
            viewrecords: true,
            caption: 'Deed Info',
            emptyrecords: 'No records',
            rowNum: 10,
            pager: jQuery('#pager'),
            rowList: [10, 20, 30, 40],
            jsonReader:
            {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeat

<details>
<summary>英文:</summary>

I needed to create a search page where various types of text, dropdown, date field will be there. i added onkeyup function to every text field so that whenever i put any text or number a grid will appear with subgrid. The issue i am facing is if i give input for the first time then the grid appear with correct data. if i clear that field then the grid is reloading also but if i input again in that field or any other text field then the js is not sending request to the action method with updated post value.

Here is my View Part:

<section class="content">
<div class="container-fluid">
<!-- SELECT2 EXAMPLE -->
<!-- SELECT2 EXAMPLE -->
<div class="card card-info">
<div class="card-header">
<h3 class="card-title">Search Criteria</h3>

            &lt;div class=&quot;card-tools&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-tool&quot; data-card-widget=&quot;collapse&quot;&gt;
&lt;i class=&quot;fas fa-minus&quot;&gt;&lt;/i&gt;
&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- /.card-header --&gt;
@using (Html.BeginForm(&quot;SearchReport&quot;, &quot;LRLiveSearch&quot;, FormMethod.Post))
{
&lt;div class=&quot;card-body&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-12&quot; style=&quot;line-height:1.50px;&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;Deed No&lt;/label&gt;
@Html.TextBoxFor(model =&gt; model.DeedNo, new { @class = &quot;form-control&quot;, onkeyup = &quot;LiveSearch(&#39;DN&#39;, this.value)&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;Deed Identity&lt;/label&gt;
@Html.TextBoxFor(model =&gt; model.DeedId, new { @class = &quot;form-control&quot;, onkeyup = &quot;LiveSearch(&#39;DI&#39;, this.value)&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;Client Name&lt;/label&gt;
@Html.TextBoxFor(model =&gt; model.ClientName, new { @class = &quot;form-control&quot;, onkeyup = &quot;LiveSearch(&#39;CN&#39;, this.value)&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;Client Contact Number&lt;/label&gt;
@Html.TextBoxFor(model =&gt; model.ClientPhoneNumber, new { @class = &quot;form-control&quot;, onkeyup = &quot;LiveSearch(&#39;CP&#39;, this.value)&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;MR No&lt;/label&gt;
@Html.TextBoxFor(model =&gt; model.MRNo, new { @class = &quot;form-control&quot;, onkeyup = &quot;LiveSearch(&#39;MR&#39;, this.value)&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;Cheque No&lt;/label&gt;
@Html.TextBoxFor(model =&gt; model.ChequeNo, new { @class = &quot;form-control&quot;, onkeyup = &quot;LiveSearch(&#39;CQN&#39;, this.value)&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;Collection Type&lt;/label&gt;
@Html.DropDownListFor(model =&gt; model.CollectionType, Model.CollectionTypeList, new { @class = &quot;form-control&quot; })
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;From Date&lt;/label&gt;
&lt;div class=&quot;input-group date&quot; id=&quot;FirstDate&quot; data-target-input=&quot;nearest&quot;&gt;
@Html.TextBoxFor(model =&gt; model.FromDate, new { @class = &quot;form-control datetimepicker-input&quot;, @data_target = &quot;#FirstDate&quot; })
&lt;div class=&quot;input-group-append&quot; data-target=&quot;#FirstDate&quot; data-toggle=&quot;datetimepicker&quot;&gt;
&lt;div class=&quot;input-group-text&quot;&gt;&lt;i class=&quot;fa fa-calendar&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-2&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label&gt;To Date&lt;/label&gt;
&lt;div class=&quot;input-group date&quot; id=&quot;SecondDate&quot; data-target-input=&quot;nearest&quot;&gt;
@Html.TextBoxFor(model =&gt; model.ToDate, new { @class = &quot;form-control datetimepicker-input&quot;, @data_target = &quot;#SecondDate&quot; })
&lt;div class=&quot;input-group-append&quot; data-target=&quot;#SecondDate&quot; data-toggle=&quot;datetimepicker&quot;&gt;
&lt;div class=&quot;input-group-text&quot;&gt;&lt;i class=&quot;fa fa-calendar&quot;&gt;&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
@*&lt;div class=&quot;col-md-1&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;center&gt;
&lt;button id=&quot;btnLRSearch&quot; formaction=&quot;SearchList&quot; class=&quot;btn btn-md btn-primary&quot; style=&quot;margin-top:.80rem&quot; type=&quot;button&quot;&gt;Search&lt;/button&gt;
&lt;/center&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-1&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;center&gt;
&lt;button id=&quot;btnPrint&quot; formaction=&quot;SearchReport&quot; class=&quot;btn btn-md btn-warning&quot; style=&quot;margin-top:.80rem&quot; type=&quot;submit&quot;&gt;Report&lt;/button&gt;
&lt;/center&gt;
&lt;/div&gt;
&lt;/div&gt;*@
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
}
&lt;/div&gt;
&lt;/div&gt;
&lt;!-- /.container-fluid --&gt;
&lt;table class=&quot;table table-bordered table-striped table-hover text-nowrap text-sm&quot; id=&quot;Demogrid&quot;&gt;&lt;/table&gt;
&lt;div class=&quot;container-fluid&quot; id=&quot;pager&quot;&gt;&lt;/div&gt;

</section>


here is my JS part where i handled onkeyup event:

function LiveSearch(type, value) {
var searchType = type;
var searchValue = value;
if (value == "" || value == null) {
$("#Demogrid").jqGrid('setGridParam', { postData: { Type: searchType, SearchValue: searchValue } }).trigger('reloadGrid');
return true;
}
else {
$("#Demogrid").jqGrid({
url: '/HRM/LRLiveSearch/LiveSearch',
datatype: "json",
mtype: 'Get',
//table header name
colNames: ['ProjectName', 'PlotName', 'TotalLandArea', 'DeedId', 'DeedDate', 'RentFeeType', 'RentFee', 'ClientName', 'ClientAddress', 'ClientContactNo', 'EffectiveDate'],
//colModel takes the data from controller and binds to grid
colModel: [
{ name: "ProjectName" },
{ name: "PlotName" },
{ name: "TotalLandArea" },
{ name: "DeedId" },
{ name: "DeedDate" },
{ name: "RentFeeType" },
{ name: "RentFee" },
{ name: "ClientName" },
{ name: "ClientAddress" },
{ name: "ClientContactNo" },
{ name: "EffectiveDate" }
],
height: '100%',
viewrecords: true,
caption: 'Deed Info',
emptyrecords: 'No records',
rowNum: 10,
pager: jQuery('#pager'),
rowList: [10, 20, 30, 40],
jsonReader:
{
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
Id: "0"
},
postData: {
Type: searchType,
SearchValue: searchValue
},
autowidth: true,
subGrid: true,
subGridRowExpanded: function (subgridId, rowId) {
// create the subgrid HTML here
var subgridTableId = subgridId + "_t";
$("#" + subgridId).html("<table id='" + subgridTableId + "'></table>");
// get the data for the parent row
var parentRowData = $("#Demogrid").jqGrid("getRowData", rowId);
// retrieve the primary key value of the parent row
var parentId = parentRowData.DeedId;
$("#" + subgridTableId).jqGrid({
// configure the subgrid
url: '/HRM/LRLiveSearch/LiveSearch',
datatype: "json",
mtype: 'Get',
//table header name
colNames: ['CollectionId', 'CollectionDate', 'MRIdentity', 'MRNo', 'MRDate', 'MRType', 'CollectionType', 'ChequeNo', 'BankName'],
//colModel takes the data from controller and binds to grid
colModel: [
{ name: "CollectionId", hidden: true },
{ name: "CollectionDate" },
{ name: "MRIdentity" },
{ name: "MRNo" },
{ name: "MRDate" },
{ name: "MRType" },
{ name: "CollectionType" },
{ name: "ChequeNo" },
{ name: "BankName" }
],
height: '100%',

                        viewrecords: true,
caption: &#39;Collection Details&#39;,
emptyrecords: &#39;No records&#39;,
rowNum: 10,
jsonReader: {
repeatitems: false
},
postData: {
Type: searchType,
SearchValue: searchValue,
DeedId: parentId
},
autowidth: true
});
},
}).navGrid(&#39;#pager&#39;,
{
edit: true,
add: true,
del: true,
search: true,
refresh: true,
closeAfterSearch: true
});
}
}

and it is my action method:

public JsonResult LiveSearch(string sord, int page, int rows, string searchString, string Type, string SearchValue, string DeedId)
{
if(SearchValue == "")
{
SearchValue = "$";
}
var query = iLRSearchRepository.LiveSearchFilter(Type, SearchValue);
if (DeedId == null)
{
//#2 Setting Paging
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
//#3 Linq Query to Get Data

            var Results = query.Select(
x =&gt; new
{
ProjectName = x.ProjectName,
PlotName = x.PlotName,
TotalLandArea = x.TotalLandArea,
DeedId = x.DeedId,
DeedDate = x.DeedDate.ToString(&quot;dd/MM/yyyy&quot;),
RentFeeType = x.RentFeeType,
RentFee = x.RentFee,
ClientName = x.ClientName,
ClientAddress = x.ClientsAddress,
ClientContactNo = x.ClientsContactNo,
EffectiveDate = x.ActivateDate.ToString(&quot;dd/MM/yyyy&quot;)
}).Distinct();
//#4 Get Total Row Count  
int totalRecords = Results.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
//#5 Setting Sorting  
if (sord.ToUpper() == &quot;DESC&quot;)
{
Results = Results.OrderByDescending(s =&gt; s.DeedId);
Results = Results.Skip(pageIndex * pageSize).Take(pageSize);
}
else
{
Results = Results.OrderBy(s =&gt; s.DeedId);
Results = Results.Skip(pageIndex * pageSize).Take(pageSize);
}
//#6 Setting Search  
if (!string.IsNullOrEmpty(searchString))
{
Results = Results.Where(m =&gt; m.DeedId == searchString);
}
//#7 Sending Json Object to View.  
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = Results
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
else
{
var Results = query.Where(x =&gt; x.DeedId == DeedId).Select(
x =&gt; new
{
CollectionId = x.CollectionId,
CollectionDate = x.CollectionDate?.ToString(&quot;dd/MM/yyyy&quot;),
MRIdentity = x.MRIdentity,
MRNo = x.MRNo,
MRDate = x.MRDate?.ToString(&quot;dd/MM/yyyy&quot;),
MRType = x.MRType,
CollectionType = x.CollectionType,
ChequeNo = x.ChequeNo,
BankName = x.BankName,
});
var jsonData = new
{
rows = Results
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
//model.landRent_View_SearchInfos = query;
//return View(&quot;_LandRentInfoList&quot;, model);
}

</details>
# 答案1
**得分**: 0
使用您的代码,每次进行搜索时都尝试创建jqGrid,这显然是不允许的,因为网格已经创建。
如果值为空或为null:
```javascript
if (value == "" || value == null) {
$("#Demogrid").jqGrid('setGridParam', { postData: { Type: searchType, SearchValue: searchValue } }).trigger('reloadGrid');
return true;
}

否则:

else {
    $("#Demogrid").jqGrid({}...)
}

为了解决这个问题,首先创建网格,然后在值更改时重新加载:

$("#Demogrid").jqGrid({
    url: '/HRM/LRLiveSearch/LiveSearch',
    datatype: "json",
    mtype: 'Get',
    //表头名称
    // ...
});

function LiveSearch(type, value) {
    var searchType = type;
    var searchValue = value;
    $("#Demogrid").jqGrid('setGridParam', { postData: { Type: searchType, SearchValue: searchValue } }).trigger('reloadGrid');
    return true;
}

希望这有所帮助。最初,您可以设置值(postData),以便网格返回空数据,或者使用空的本地数据创建网格(datatype: local),然后将数据类型更改为json。

英文:

With your code you every time try create jqGrid when you do a search, which of course is not allowed since the grid is already created.

    if (value == &quot;&quot; || value == null) {
$(&quot;#Demogrid&quot;).jqGrid(&#39;setGridParam&#39;, { postData: { Type: searchType, SearchValue: searchValue } }).trigger(&#39;reloadGrid&#39;);
return true;
}
else {
$(&quot;#Demogrid&quot;).jqGrid({}...)
}

To solve the problem first create the grid and the do a reload when the value changes

            $(&quot;#Demogrid&quot;).jqGrid({
url: &#39;/HRM/LRLiveSearch/LiveSearch&#39;,
datatype: &quot;json&quot;,
mtype: &#39;Get&#39;,
//table header name
...
});
function LiveSearch(type, value) {
var searchType = type;
var searchValue = value;
$(&quot;#Demogrid&quot;).jqGrid(&#39;setGridParam&#39;, { postData: { Type: searchType, SearchValue: searchValue } }).trigger(&#39;reloadGrid&#39;);
return true;
}   

Hope this helps.

Initially you can set values (postData) so that the grid return empty data or create the grid with empty local data (datatype : local) and then change the datatype to json.

huangapple
  • 本文由 发表于 2023年1月9日 13:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定