Фильтрация данных не работает с использованием javascript - PullRequest
0 голосов
/ 09 марта 2020

В моей 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"
        };
...