使用JavaScript无法进行数据表过滤

时间:2020-03-09 09:29:21

标签: javascript jquery datatable filtering

我的html中有下表:

<table id="mutationlogs" class="table table-bordered">
    <thead>
    <th>Sequence Number</th>
    <th>Date time</th>
    <th>INSS</th>
    <th>Mutation</th>
    <th>Old Value</th>
    <th>NewValue</th>
    </thead>
    <tbody></tbody>
</table>

我希望能够过滤列。 我添加了以下内容:

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/fixedheader/3.1.6/js/dataTables.fixedHeader.min.js"></script>

我添加了以下代码:

<script type="text/javascript">
    $(document).ready(function () {
        var datatable = $("#mutationlogs").DataTable({
            "ordering": false,
            "searching": true,
            "info": true,
            "processing": true,
            "serverSide": true,
            "pagingType": "simple_numbers",
            "sDom": "<'row'<'col-sm-12'tr>><'row'<'col-sm-12'p>>",
            "ajax": {
                "url": "@Url.Action("GetMutationsLogs", "Logs")",
                "type": "POST"
            },
            "columns": [
                { "name": "@Constants.MutationLogColumnNames.SequenceNumber" },
                { "name": "@Constants.MutationLogColumnNames.DateTime" },
                { "name": "@Constants.MutationLogColumnNames.INSS" },
                { "name": "@Constants.MutationLogColumnNames.MutationAction" },
                { "name": "@Constants.MutationLogColumnNames.OldValue" },
                { "name": "@Constants.MutationLogColumnNames.NewValue" },
            ]
        });

        $('#mutationlogs thead tr').clone(true).appendTo('#mutationlogs thead');
        $('#mutationlogs thead tr:eq(1) th').each(function (i) {
            var title = $(this).text();
            $(this).html('<input type="text" placeholder="Search ' + title + '" />');

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


</script>

但是,当我在用于过滤的字段之一中键入一个值时,我的表不会被过滤。 在调试时,我确实获得了进行过滤的代码。但这似乎没有过滤条件。

我忘记了什么?

这是我的服务器端代码:

DataTable dt = new DataTable();
        Workbook wb;

        string[] files = Directory.GetFiles(@"c:\Logs\", "mutations*.xls");

        var mutations = new List<Mutation>();

        foreach (var file in files)
        {
            var sequencenumber = file.Split('_')[2].Split('.')[0];

            wb = new Workbook(file);
            dt = wb.Worksheets[0].Cells.ExportDataTable(1, 0, wb.Worksheets[0].Cells.MaxDataRow + 1, wb.Worksheets[0].Cells.MaxDataColumn + 1);

            foreach (DataRow dataRow in dt.Rows)
            {
                if (dataRow[0].ToString() != "")
                {
                    Mutation mutation = new Mutation();
                    mutation.SequenceNumber = sequencenumber;
                    ...

                    bool exists = mutations.Any(m => m.SequenceNumber == mutation.SequenceNumber && m.INSS == mutation.INSS && m.OldValue == mutation.OldValue && m.NewValue == mutation.NewValue && m.MutationAction == mutation.MutationAction);

                    if (!exists)
                    { mutations.Add(mutation); }
                }
            }

            dt = wb.Worksheets[1].Cells.ExportDataTable(1, 0, wb.Worksheets[0].Cells.MaxDataRow + 1, wb.Worksheets[0].Cells.MaxDataColumn + 1);
            foreach (DataRow dataRow in dt.Rows)
            {
                if (dataRow[0].ToString() != "")
                {
                    Mutation mutation = new Mutation();
                    ...
                    mutation.Timestamp = String.Format("{0:dd/MM/yyyy HH:mm:ss}", dataRow[2]);
                    mutation.OldValue = dataRow[3].ToString();
                    mutation.NewValue = dataRow[4].ToString();

                    bool exists = mutations.Any(m => m.SequenceNumber == mutation.SequenceNumber && m.INSS == mutation.INSS && m.OldValue == mutation.OldValue && m.NewValue == mutation.NewValue && m.MutationAction == mutation.MutationAction);

                    if (!exists)
                    { mutations.Add(mutation); }
                }
            }

        };

        var count = mutations.Count();
        var result = mutations.Distinct().OrderByDescending(t => t.SequenceNumber).ThenBy(t => t.INSS)
            .Skip(request.Start)
            .Take(request.Length)
            .ToList();

        return new ContentResult
        {
            Content = JsonConvert.SerializeObject(GetJson(result, request, count)),
            ContentType = "application/json"
        };

0 个答案:

没有答案