【问题标题】:I have a duplicate row problem in jquery datatable我在 jquery 数据表中有重复的行问题
【发布时间】:2020-06-23 12:06:20
【问题描述】:

我在 jquery 数据表中有重复行问题。通过 ajax 调用获取 Jquery 数据表时,某些行显示不止一页:

我使用了这个查询.....我怎样才能修复这个错误,我可以使用 unique() 方法吗?

var data = (from h in db.TwitterTrends
                        select new
                        {
                            h.Id,
                            h.Name,
                            h.TweetVolume,
                            Countries = h.TwitterTrendCountryRelations.Select(Country => Country.Country.Name).Distinct(),
                            h.LanguageCode,
                            Categories = h.TwitterTrendCategoryRelations.Select(Category => Category.Category.Id).Distinct(),
                            h.Url,
                            h.IsPublished,
                            CategoryName = h.TwitterTrendCategoryRelations.Select(Category => Category.Category.Name),
                            TrendDate = h.TwitterTrendCountryRelations.OrderByDescending(b => b.TrendDate).FirstOrDefault().TrendDate,
                            HashtagStory = h.HashtagStory
                        });
 data = query.OrderByDescending(h => h.TrendDate).Skip(skip).Take(pageSize)
//jquery datatable
  var RequestVerficationToken = $('input[name = "__RequestVerificationToken"]').val();
        $("#Twitter-List").dataTable({
            "bSort": false,
            "processing": true, // for show progress bar
            "serverSide": true, // for process server side
            "filter": true, // this is for disable filter (search box)
            "orderMulti": false, // for disable multiple column at once
            "pageLength": 10,
            "oSearch": { "sSearch": Searchvalue },
            "drawCallback": function (settings) {
                $(".select2").select2({
                    placeholder: "Please select",
                    width: 175,
                });
            },
            "ajax": {
                "url": "@Url.Action("LoadDataTable")",
                "datatype": "json",
                "type": "POST",
                "data": { __RequestVerificationToken: RequestVerficationToken }
            },
            "columns":
                [
                    { "data": "Name" },
                    { "data": "TweetVolume" },
                    { "data": "Countries" },
                    { "data": "LanguageCode" },
                    {
                        "render": function (row, data, type, full, meta) {
                            var Options = "";
                            $(CategoryOptions).each(function (i, item) {"...."}

【问题讨论】:

  • 您需要分享您的代码并用更多细节解释问题。
  • 另外,分享您的 API 代码,您可以在其中加载数据。 90% 的 API 返回重复数据。
  • 我已经分享了我的代码@ChetanRanpariya ,,,
  • @Alex-TinLe 这个代码我用过
  • 如何计算 API 中的“skip”参数?你能分享那个动作的所有代码吗?

标签: c# jquery ajax model-view-controller datatable


【解决方案1】:

从您共享的代码中,跳过计算错误

var start = Request.Form.GetValues("start")[0].ToString();
int skip = start != null ? Convert.ToInt32(start) : 0;

应该是

int skip = (start != null ? Convert.ToInt32(start) : 0) * pageSize;

因为,当您的 pageindex 为 0 时,您需要跳过 0 行。当您的 pageindex 为 1 时,您需要跳过 20 行(假设您的页面大小为 20)。

【讨论】:

  • 这是错误的方式,因为第一次跳过会加倍,第二次会跳过 10 条记录,第二次会跳过 100 条记录,第三次会跳过 200 条记录
  • 我在使用 .Distinct() 时发现了问题,查询中的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 2012-01-14
相关资源
最近更新 更多