在DTETIT中使用JS实施单个列搜索

发布于 2025-01-27 13:49:35 字数 1305 浏览 1 评论 0原文

在我的数据表中,我想实现此页面中显示的单个列搜索:

​>过滤器参数。因此,我正在尝试使用datatable.options.options参数dtedit来实现单个列搜索。

dtedit中,我可以将JS代码与以下代码合并:

jsc <- `Some JS code`

dtedit(input,
       output,
       name = "input_table",
       thedata = design,
       ...,
       datatable.options = list(initComplete = JS(jsc))
)

我的JS技能不存在,并且我需要帮助弄清楚如何在我的dtedit中使用以下JS代码

$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#example tfoot th').each( function () {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );
 
    // DataTable
    var table = $('#example').DataTable({
        initComplete: function () {
            // Apply the search
            this.api().columns().every( function () {
                var that = this;
 
                $( 'input', this.footer() ).on( 'keyup change clear', function () {
                    if ( that.search() !== this.value ) {
                        that
                            .search( this.value )
                            .draw();
                    }
                } );
            } );
        }
    });
 
} );

In my datatable, I would like to implement individual column searching shown in this page:

https://datatables.net/examples/api/multi_filter.html

I am using DTedit R package, an extension to DT, and it doesn't have the convenient filter argument. So, I am trying to use the datatable.options argument inside dtedit to implement individual column searching.

In dtedit, I can incorporate JS code with the following code:

jsc <- `Some JS code`

dtedit(input,
       output,
       name = "input_table",
       thedata = design,
       ...,
       datatable.options = list(initComplete = JS(jsc))
)

My JS skills are non existent and I need help on figuring out how to use the following JS code in my dtedit

$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#example tfoot th').each( function () {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );
 
    // DataTable
    var table = $('#example').DataTable({
        initComplete: function () {
            // Apply the search
            this.api().columns().every( function () {
                var that = this;
 
                $( 'input', this.footer() ).on( 'keyup change clear', function () {
                    if ( that.search() !== this.value ) {
                        that
                            .search( this.value )
                            .draw();
                    }
                } );
            } );
        }
    });
 
} );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2025-02-03 13:49:35

这是我将如何处理dt软件包:

initComplete <- "
function(settings) {
  var table = settings.oInstance.api();
  var ncol = table.columns().count();
  var append = function (i) {
    var column = table.column(i);
    var colname = column.header().textContent;
    var $input = $(
      '<input type=\\\"text\\\" placeholder=\\\"Search ' + colname + '\\\" />'
    );
    $(column.footer()).append($input);
    $input.on('keyup change clear', function () {
      if (column.search() !== this.value) {
        column.search(this.value).draw();
      }
    });
  };
  for (var i = 1; i < ncol; i++) {
    append(i);
  }
}
"

dat <- iris
sketch <- htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      lapply(c("", names(dat)), th)
    )
  ),
  tfoot(
    tr(
      lapply(rep("", 1+ncol(dat)), th)
    )
  )
))

datatable(
  dat,
  container = sketch,
  options = list(
    initComplete = JS(initComplete),
    columnDefs = list(
      list(targets = "_all", className = "dt-center")
    )
  )
)

”在此处输入图像说明“

我不熟悉dtedit。但是我看了一下文档,看来它不接受容器选项。不幸的是,不可能将页脚添加到使用JavaScript的DataTable中。

Here is how I would do with the DT package:

initComplete <- "
function(settings) {
  var table = settings.oInstance.api();
  var ncol = table.columns().count();
  var append = function (i) {
    var column = table.column(i);
    var colname = column.header().textContent;
    var $input = $(
      '<input type=\\\"text\\\" placeholder=\\\"Search ' + colname + '\\\" />'
    );
    $(column.footer()).append($input);
    $input.on('keyup change clear', function () {
      if (column.search() !== this.value) {
        column.search(this.value).draw();
      }
    });
  };
  for (var i = 1; i < ncol; i++) {
    append(i);
  }
}
"

dat <- iris
sketch <- htmltools::withTags(table(
  class = 'display',
  thead(
    tr(
      lapply(c("", names(dat)), th)
    )
  ),
  tfoot(
    tr(
      lapply(rep("", 1+ncol(dat)), th)
    )
  )
))

datatable(
  dat,
  container = sketch,
  options = list(
    initComplete = JS(initComplete),
    columnDefs = list(
      list(targets = "_all", className = "dt-center")
    )
  )
)

enter image description here

I am not familiar with DTedit. But I took a look at the doc and it seems that it doesn't accept the container option. And unfortunately, it's not possible to add a footer to a datatable with JavaScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文