第二次加载时,Jquery网格无法正常工作

时间:2016-03-25 11:04:36

标签: javascript jquery

当我第二次点击时,jquery网格不起作用。

$(document).ready(function() {
  $('#btnlinechat').click(function() {
    debugger;
    var firstClick = true;
    if (!firstClick) {
      $("#list").trigger("reloadGrid");
    }
    firstClick = false;
    //var getdata = function () {
    //debugger;
    var arr = {};
    var datefromt = document.getElementById('txtfrom').value;
    var datetot = document.getElementById('txtto').value;
    arr.StartDate = datefromt;
    arr.EndDate = datetot;

    $.ajax({
      //mtype: 'GET',
      type: "POST",
      contentType: "application/json",
      //data: "{}",
      data: JSON.stringify(arr),
      url: "buttontest.aspx/GetData",
      dataType: "json",
      success: function(data) {
        data = data.d;
        $("#list").jqGrid({
          datatype: "local",
          colNames: ['Monthly Topup', 'Monthly Payout', 'Balance', 'Monthly Date'],
          colModel: [{
              name: 'monthlytopup',
              index: 'monthlytopup',
              width: 10,
              height: 30,
              editable: false
            }, {
              name: 'monthlypayout',
              index: 'monthlypayout',
              width: 10,
              height: 30,
              editable: false
            }, {
              name: 'balance',
              index: 'balance',
              width: 10,
              height: 30,
              editable: false
            }, {
              name: 'monthlydate',
              index: 'monthlydate',
              width: 10,
              formatter: 'date',
              formatoptions: {
                srcformat: "Y-m-d",
                newformat: 'd/m/y',
                datefmt: 'd/m/y'
              }
            }

          ],
          data: JSON.parse(data),
          rowno: 10,
          loadonce: false,
          rowlist: [5, 10, 20],
          pager: '#pagingGrid',
          viewrecords: true,
          gridview: true,
          sortorder: 'asc',
          rownumbers: true,
          //altrows: true,
          reload: true,
          autowidth: false,
          hoverrows: true,
          height: 300,
          //multiselect: false,
          width: 1345,
          rownumbers: true,
          caption: "DATA",
        });
        debugger;
        $('#list').jqGrid('navGrid', '#pagingGrid', {
          edit: false,
          add: false,
          del: false,
          search: true,
          searchtext: "Search",
          viewrecords: true,
          reload: true,
          //cloneToTop: false,
        });
      },
    });
    //}
  });

});

1 个答案:

答案 0 :(得分:1)

因为您在click处理程序中明确说出来,所以每次点击都被视为第一次点击:

 var firstClick = true;

相反,它应该类似于:

var firstClick = true; // << Outside the click handler

$(document).ready(function() {
  $('#btnlinechat').click(function() {
    debugger;

    if (!firstClick) {
      $("#list").trigger("reloadGrid");
    }
   ...
相关问题