Property 'dataTableExt' does not exist on type 'JQuery<HTMLElement>' – when trying to use jQuery DataTables with TypeScript

huangapple go评论64阅读模式
英文:

Property 'dataTableExt' does not exist on type 'JQuery<HTMLElement>' - when trying to use jQuery DataTables with TypeScript

问题

我已经准备好一个简单的 jsFiddle 示例,展示了一个带有可点击行和复选框过滤的jQuery DataTable:

Property 'dataTableExt' does not exist on type 'JQuery<HTMLElement>' – when trying to use jQuery DataTables with TypeScript

当我尝试将我的代码转换为TypeScript时(这里是一个不太工作的 StackBlitz 示例),然后在VS Code中显示了2个错误:

属性 'dataTableExt' 不存在于类型 'JQuery' 上(对于 $.fn.dataTableExt.afnFiltering.push 行)

没有匹配此调用的重载(对于 renderAvgData 调用)

我已经在GitHub和DataTable论坛上搜索过,但还没有找到解决方案。

以下是我的工作Javascript代码的副本(与 jsFiddle 中的代码相同):

'use strict';

function renderOpenElo(data, typeStr, row, meta) {
  return typeStr === 'display' ? '<SPAN CLASS="xlarge">&rtri;</SPAN> ' + data : data;
}

function renderAvgData(data) {
  return '<TABLE WIDTH=100%>' +
    '<TR><TH>Average score: ' + data.avg_score + '</TH></TR>' +
    '<TR><TH>Average time: ' + data.avg_time + '</TH></TR></TABLE>';
}

jQuery(function() {
  var topTable = $('#topTable').DataTable({
    ajax: 'https://wordsbyfarber.com/ru/top-5',
    pageLength: 5,
    order: [
      [0, 'desc']
    ],
    columns: [{
        data: 'elo',
        searchable: false,
        className: 'details-control',
        render: renderOpenElo
      },
      {
        data: 'uid',
        className: 'dt-right'
      }
    ]
  });

  $('#topTable tbody').on('click', 'td.details-control', function() {
    var span = $(this).find('span').first();
    var tr = $(this).closest('tr');
    var row = topTable.row(tr);

    if (row.child.isShown()) {
      row.child.hide();
      span.html('&rtri;');
    } else {
      row.child(renderAvgData(row.data())).show();
      span.html('&dtri;');
    }
  });

  $('#below,#middle,#above').click(function() {
    topTable.draw();
  });

  $.fn.dataTableExt.afnFiltering.push(
    function(oSettings, aData, iDataIndex, row) {
      if ('topTable' == oSettings.nTable.id) {
        console.log(row);

        if (row.uid < 10000) {
          return $('#below').is(':checked');
        }

        if (10000 < row.uid && row.uid < 20000) {
          return $('#middle').is(':checked');
        }

        return $('#above').is(':checked');
      }

      return true;
    }
  );
});

如果您需要进一步的帮助,请告诉我。

英文:

I have prepared a simple jsFiddle demo, which shows a jQuery DataTable with clickable rows and checkbox filtering:

Property 'dataTableExt' does not exist on type 'JQuery<HTMLElement>' – when trying to use jQuery DataTables with TypeScript

When I try to convert my code to TypeScript (here a not really working StackBlitz demo), then I have 2 errors displayed in VS Code:

> Property 'dataTableExt' does not exist on type 'JQuery<HTMLElement>' (for the $.fn.dataTableExt.afnFiltering.push line)

> No overload matches this call (for the renderAvgData call)

I have searched around, on GitHub and at the DataTable forum, but hasn't been able to find a resolution for that.

Below is the copy of my working Javascript code (same as in the jsFiddle):

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

&#39;use strict;&#39;
function renderOpenElo(data, typeStr, row, meta) {
return typeStr === &#39;display&#39; ? &#39;&lt;SPAN CLASS=&quot;xlarge&quot;&gt;&amp;rtri;&lt;/SPAN&gt; &#39; + data : data;
}
function renderAvgData(data) {
return &#39;&lt;TABLE WIDTH=100%&gt;&#39; +
&#39;&lt;TR&gt;&lt;TH&gt;Average score: &#39; + data.avg_score + &#39;&lt;/TH&gt;&lt;/TR&gt;&#39; +
&#39;&lt;TR&gt;&lt;TH&gt;Average time: &#39; + data.avg_time + &#39;&lt;/TH&gt;&lt;/TR&gt;&lt;/TABLE&gt;&#39;;
}
jQuery(function() {
var topTable = $(&#39;#topTable&#39;).DataTable({
ajax: &#39;https://wordsbyfarber.com/ru/top-5&#39;,
pageLength: 5,
order: [
[0, &#39;desc&#39;]
],
columns: [{
data: &#39;elo&#39;,
searchable: false,
className: &#39;details-control&#39;,
render: renderOpenElo
},
{
data: &#39;uid&#39;,
className: &#39;dt-right&#39;
}
]
});
$(&#39;#topTable tbody&#39;).on(&#39;click&#39;, &#39;td.details-control&#39;, function() {
var span = $(this).find(&#39;span&#39;).first();
var tr = $(this).closest(&#39;tr&#39;);
var row = topTable.row(tr);
if (row.child.isShown()) {
row.child.hide();
span.html(&#39;&amp;rtri;&#39;);
} else {
row.child(renderAvgData(row.data())).show();
span.html(&#39;&amp;dtri;&#39;);
}
});
$(&#39;#below,#middle,#above&#39;).click(function() {
topTable.draw();
});
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex, row) {
if (&#39;topTable&#39; == oSettings.nTable.id) {
console.log(row);
if (row.uid &lt; 10000) {
return $(&#39;#below&#39;).is(&#39;:checked&#39;);
}
if (10000 &lt; row.uid &amp;&amp; row.uid &lt; 20000) {
return $(&#39;#middle&#39;).is(&#39;:checked&#39;);
}
return $(&#39;#above&#39;).is(&#39;:checked&#39;);
}
return true;
}
);
});

<!-- language: lang-css -->

td.details-control {
cursor: pointer;
}
span.xlarge {
font-size: x-large;
}

<!-- language: lang-html -->

&lt;link type=&quot;text/css&quot; rel=&quot;stylesheet&quot; href=&quot;//cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css&quot;&gt;
&lt;script src=&quot;//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;//cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js&quot;&gt;&lt;/script&gt;
&lt;p align=&quot;center&quot;&gt;Show user ids:
&lt;label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;below&quot; checked&gt; Below 10k &lt;/label&gt;
&lt;label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;middle&quot; checked&gt; 10k - 20k &lt;/label&gt;
&lt;label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;above&quot; checked&gt; Above 20k &lt;/label&gt;
&lt;/p&gt;
&lt;table id=&quot;topTable&quot; class=&quot;stripe&quot; width=100%&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Elo&lt;/th&gt;
&lt;th&gt;User id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案1

得分: 0

插件作者非常友好地回答了我在DataTables论坛中的两个问题:

  1. 应该使用$.fn.dataTable.ext.search进行过滤
  2. 在DataTables版本1.13.5中,.show()错误消息已经解决
英文:

The plugin author has been very nice to answer both my questions in the DataTables forum:

  1. The $.fn.dataTable.ext.search should be used for filtering
  2. The .show() error message has been resolved in DataTables version 1.13.5

huangapple
  • 本文由 发表于 2023年6月29日 19:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定