【问题标题】:How to avoid ajax error while typing a date value in search textbox?在搜索文本框中输入日期值时如何避免 ajax 错误?
【发布时间】:2019-08-08 16:33:11
【问题描述】:

我有一个日期搜索功能,可以立即输入完整的日期输入,例如:('10/30/2019'),但如果我输入它,例如:('10/'),它会抛出一个 ajax错误(附截图)。

我的 bal.js 文件:

$(document).ready(function() {
  $("table[role='bal_datatable']").each(function() {
    var table = $(this).DataTable({
      "order": [
        [7, "desc"]
      ],
      columnDefs: [{
          "searchable": false,
          "targets": 9
        },
        {
          "searchable": false,
          "targets": 10
        },
        {
          "orderable": false,
          "targets": 9
        },
        {
          "orderable": false,
          "targets": 10
        },
        {
          "className": "abc",
          "targets": [5]
        }
      ],
      autoWidth: false,
      pageLength: 50,
      processing: true,
      serverSide: true,
      ajax: $(this).data('url')
    });

    // Setup - add a text input to each footer cell
    $('#balances tfoot th').each(function(i) {
      var title = $(this).text();
      $(this).html('<input type="text" placeholder="' + title + '" />');


      $('input', this).on('keyup change', function() {
        if (table.column(i).search() !== this.value) {
          table
            .column(i)
            .search(this.value)
            .draw();
        }
      });

    });

  });
})

我的 bal_datatable.rb :

def bal_method1(balances)
  if(@tabular.search7 > '00/00/0000' )
    balances.where("balances.updated_at > (:search) and balances.updated_at < (date (:search) + interval '1 day')", {search: "#{@tabular.search7}"})
  else
    balances.where("1 = 1")
  end
end

请帮忙。

【问题讨论】:

    标签: javascript ruby-on-rails datatables


    【解决方案1】:
    if( this.value.match(/(\d{2}\/\d{2}\/\d{4})/))
                { table.column(i).search( this.value ).draw(); }
                else if( this.value == '')
                { table.column(i).search( this.value ).draw(); }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      相关资源
      最近更新 更多