首页 > 解决方案 > jquery dataTable Ajax调用中未传递参数

问题描述

我编写了一个代码来将动态表显示到 DataTable 中。

 <table id="tag" class="display table table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        @foreach (var item in Model.HeaderModelList.Select((value, i) => new { i, value }))
                        {
                            <th id=@(item.i+1)>
                                @item.value.Category
                                <span class="filterExcel">
                                    <select id="tag@(item.value.PatientTagCategoryId)" name="tag" asp-items="@item.value.HeaderOptions" multiple class="drp-tagMulti-Select">
                                    </select>
                                </span>
                            </th>
                        }
                        <th id="@(Model.HeaderModelList.Count()+1)">Simulation Name
                        </th>
                        <th id="@(Model.HeaderModelList.Count()+2)">
                            Patient Name                          
                        </th>
                    </tr>
                </thead>
            </table>

在更改下拉列表时,我调用了一个调用 reload jquery 的事件。


  $(document).ready(function () {

            var id = "";
            var newval = "";

             $('.drp-tagMulti-Select').on('change', function () {
                var valid = this.id;
                var val = $('#' + valid).val();

                if (valid) {
                    newval = val;
                    id = valid;
                    console.log("id" + id);
                    console.log("newval" + newval);
                    table.ajax.reload();
                }
            });

            var table = $("#tag").DataTable({

                "aLengthMenu": [[10, 25, 50, 75, -1], [10, 25, 50, 75, "All"]],
                "iDisplayLength": 10,
                "serverSide": true,
                "processing": true,
                "stateSave": true,
                "search": true,
                "ajax": {
                    'type': 'POST',
                    'dataType': 'json',
                    'data': {
                        TagId: id,
                        Values: newval
                    },
                    'url': 'GetFilteredPatientTags',
                    'dataSrc': function (d) {
                        var values = [];
                        for (var i = 0; i < d.data.length; i++) {
                            var result = Object.values(d.data[i]);
                            values.push(result);
                        }
                        return values;
                    }
                }
            });

            $(".filterExcel").click(function (e) {
                e.stopPropagation();
            })

            $('.drp-tagMulti-Select').multipleSelect({
                placeholder: 'Select specific course',
                ellipsis: true,
                filter: true,
                filterAcceptOnEnter: true,
                animate: 'fade',
                width: 20
            })

            $('.ms-drop').width('fit-content')

           
        });

现在,每当我更改下拉列表时,都会触发事件,并且 和 的值idnewval在控制台中正确显示

console.log("id" + id);
console.log("newval" + newval);

然后我重新加载数据表,但是ajax 的值idnewval没有正确传递,值作为 null 发送

id如果我将and的初始值更改newval为“a”和“b”

var id = "a";
var newval = "b";

那么通过ajax的值总是“a”和“b”,我需要在ajax中传递控制台中显示的值idnewval

我怎样才能解决这个问题?

标签: javascriptjqueryajaxdatatabledatatables

解决方案


要解决它,您需要将一个函数传递给ajax.data

"ajax": {
  ...
  'data': function(d) {
    d.TagId = id;
    d.Values = newval;
  },
  ...

推荐阅读