简体   繁体   English

将自定义搜索字段添加到 DataTable 对象

[英]Adding custom search fields to a DataTable object

I need to add fields that I created on a page to a DataTable object.我需要将我在页面上创建的字段添加到 DataTable 对象。 Even though they aren't typical parameters (order, search, pagination, etc.), they can get save and loaded along with the rest of the DataTables' object state.即使它们不是典型的参数(顺序、搜索、分页等),它们也可以与 DataTable 的其余对象状态一起保存和加载。

JavaScript JavaScript

$(document).ready(function () {
    var table = $('#stackoverflow-datatable').DataTable({
        "processing": true,
        "stateSave": true,
        "stateSaveCallback": function (settings, data) {
            $.ajax({
                "url": "/api/save_state",
                "data": data,
                "dataType": "json",
                "success": function (response) {}
            });
        },
        "stateLoadCallback": function (settings) {
            var o;
            $.ajax({
                "url": "/api/load_state",
                "async": false,
                "dataType": "json",
                "success": function (json) {
                    o = json;
                }
            });

            return o;
        }
    });
});

And then, the JavaScript that I thought would add the fields to the object is the following.然后,我认为会将字段添加到对象的 JavaScript 如下。

<script type="text/javascript">
    $.fn.dataTableExt.afnFiltering.push(
        function (settings, data) {
            var dateStart = parseDateValue($("#dateStart").val());
            var dateEnd = parseDateValue($("#dateEnd").val());
            var evalDate = parseDateValue(data[9]);

            return evalDate >= dateStart && evalDate <= dateEnd;
        }
    );

    function getDate(element) {
        return $.datepicker.parseDate('mm/dd/yy', element.value);
    }

    // Convert mm/dd/yyyy into numeric (ex. 08/12/2010 -> 20100812)
    function parseDateValue(rawDate) {
        var dateArray = rawDate.split("/");

        return dateArray[2] + dateArray[0] + dateArray[1];
    }
</script>

HTML HTML

<input type="text" id="dateStart" class="datepicker" name="dateStart">
<input type="text" id="dateEnd" class="datepicker" name="dateEnd">

<table id="stackoverflow-datatable">
    <thead>
    <th>ID</th>
    <th>Name</th>
    <th>Date</th>
    </thead>

To implement a custom range search, use:要实现自定义范围搜索,请使用:

$.fn.dataTable.ext.search.push

To save and load custom state parameters, use:要保存和加载自定义状态参数,请使用:

stateSaveParams: function (settings, data) {}
stateLoadParams: function (settings, data) {}

You must attach event listeners to your datepickers, so that the table is redrawn every time the start or ending dates are changed, but you must also fill in the input fields when the table state is restored.您必须将事件侦听器附加到您的日期选择器,以便在每次更改开始或结束日期时重新​​绘制表格,但您还必须在恢复表格状态时填写输入字段。 This can be tricky because there's a cross-dependency between the pickers and the table, so I've introduced a custom event to make sure the datepickers state is restored after their creation.这可能很棘手,因为选择器和表之间存在交叉依赖性,所以我引入了一个自定义事件来确保日期选择器状态在创建后恢复。

I've created a working example based on your code, using a static table:我使用静态表根据您的代码创建了一个工作示例:

JSFIDDLE JSFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM