1

我在我的应用程序中使用 jquery 数据表,并使用服务器端处理进行分页并将数据导出为 CSV。

      $(function  (){
            $('#dataTable-my tfoot th').each( function (index) {
                var title = $('#dataTable-my thead th').eq( $(this).index() ).text();
                  $(this).html( '<input type="text" placeholder="Filter" class="form-control input-sm ng-pristine ng-valid" size="10"/>' );
            });
            var table = $('#dataTable-my').DataTable( {
                "jQueryUI": true,
                "dom": 'T<"clear">lfrtip',
                "sPaginationType": "full_numbers",
                "bServerSide": true,
                "sAjaxSource": HOST_URL+"/students/",
                "sServerMethod": "GET",
                "bFilter": true,
                "oSearch": {"bRegex":true, "bSmart": false},
                "oTableTools": {
                    "aButtons": [
                        {
                            "sExtends": "ajax",
                            "sButtonText":"Save as CSV",
                            "sToolTip": "Save as CSV",
                            "sButtonClass": "my_button_class",
                            "fnClick": function () {
                                var ajaxUrl=HOST_URL+"/students/?export=true";
                                var searchKeyword=table.search( this.value);
                                if (searchKeyword){
                                    ajaxUrl=ajaxUrl.concat("&sSearch="+searchKeyword);
                                }
                                var iframe = document.createElement('iframe');
                                iframe.style.height = "0px";
                                iframe.style.width = "0px";
                                iframe.src = ajaxUrl;
                                document.body.appendChild( iframe );
                            }
                        }
                    ]
                },
                initComplete: function ()
                {
                    var r = $('#dataTable-my tfoot tr');
                    r.find('th').each(function(){
                        $(this).css('padding', 8);
                    });
                    $('#dataTable-my thead').append(r);
                    $('#search_0').css('text-align', 'center');
                },
                "bAutoWidth": false,
                "aoColumns": [
                    { "mData": "name" },
                    { "mData": "age" },
                    { "mData": "location" }
                ]
            })
            $('#dataTable-my').DataTable();
            table.columns().eq( 0 ).each( function ( colIdx ) {
                $( 'input', table.column( colIdx ).footer() ).on( 'keyup change', function () {
                    table
                        .column( colIdx )
                        .search( this.value )
                        .draw();
                } );
            } );
        });

上面的代码覆盖了导出功能,并将全局搜索中的值作为参数传递。现在,由于我的数据表启用了多列过滤,我还需要将在列内键入的关键字作为请求参数传递。

如何获取在方法内的列过滤器中键入的值fnClick,我可以在进行 ajax 调用时传递请求参数。

我尝试使用,table.row(0).data()但我得到的是行值而不是列值。

请让我知道我哪里出错了。

问候, Pradeep

4

1 回答 1

0

I used the below code to fetch the column values and form the url.

                         "fnClick": function () {
                                var ajaxUrl=HOST_URL+"/students/?export=true";
                                var searchKeyword=table.search( this.value);
                                if (searchKeyword){
                                    ajaxUrl=ajaxUrl.concat("&sSearch="+searchKeyword);
                                }
                                //append the search keywords from the individual columns..
                                for (var index = 0; index < 8; index++ ) {
                                    ajaxUrl = ajaxUrl.concat("&sSearch_"+index+"=" + (table.column( index ).search( this.value ) || ''));
                                }

                                var iframe = document.createElement('iframe');
                                iframe.style.height = "0px";
                                iframe.style.width = "0px";
                                iframe.src = ajaxUrl;
                                document.body.appendChild( iframe );
                            }

The rest of the code remains the same. In specific the below code can be used to fetch the column details:

table.column( index ).search( this.value )
于 2014-12-17T07:59:24.240 回答