【问题标题】:Parameter Not Passing in jquery dataTable Ajax Calljquery dataTable Ajax调用中未传递参数
【发布时间】:2021-09-29 13:26:21
【问题描述】:

我已经编写了一个代码来将动态表显示到 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);

然后我重新加载数据表,但 idnewval 的值未正确传递到 ajax 中,该值作为 null 发送

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

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

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

我该如何解决这个问题?

【问题讨论】:

    标签: javascript jquery ajax datatable datatables


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      • 2016-02-18
      • 2011-06-30
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多