Button 当datatable为空时,如何禁用datatable导出按钮(或防止单击事件)?

Button 当datatable为空时,如何禁用datatable导出按钮(或防止单击事件)?,button,datatables,datatable-buttons,Button,Datatables,Datatable Buttons,当网格中有空行时,我需要禁用导出按钮(Excel、PDF)。 如何处理数据表导出按钮单击操作? var buttonCommon = { exportOptions: { format: { body: function(data, column, row, node) { return data; } } } }; var dataTableObj = { "process

当网格中有空行时,我需要禁用导出按钮(Excel、PDF)。 如何处理数据表导出按钮单击操作?

 var buttonCommon = {
    exportOptions: {
      format: {
        body: function(data, column, row, node) {
          return data;
        }
      }
    }
  };

     var dataTableObj = {
      "processing": true,
      "destroy": true,
      "scrollX": true,
      "columns": [{
        "data": "CollegeName",
        "width":"30%"

      }, {
        "data": "AffiliatedTo",
         "width":"15%"

      }, {
        "data": "TPOName",
        "width":"20%"

      }, {
        "data": "Phone",

      }, {
        "data": "Website",

        "bSortable": false
      }],
      dom: 'lBfrtip',
      buttons: [
      $.extend( true, {}, buttonCommon, {
                extend: 'excelHtml5',
                  title: 'Colleges',

            } ),


        {
          extend: 'pdf',
          title: 'Colleges'
        }

      ],

      fnRowCallback: function(nRow, aData, iDisplayIndex) {
         //Some code
        return nRow;
      }


    };


      var dataTbl = $('#tblColleges').DataTable(dataTableObj);
我已经完成了网格初始化,如下所示。 我不知道如何处理datatable导出按钮(PDF、Excel)。 你能帮我修一下吗

更新:

  • 我还有一个问题,当用户单击Excel或PDF按钮时,网格列的宽度正在塌陷
  • 我甚至需要用户单击Excel或PDF按钮,网格列宽度不应更改。我们如何才能做到这一点?

     var buttonCommon = {
        exportOptions: {
          format: {
            body: function(data, column, row, node) {
              return data;
            }
          }
        }
      };
    
         var dataTableObj = {
          "processing": true,
          "destroy": true,
          "scrollX": true,
          "columns": [{
            "data": "CollegeName",
            "width":"30%"
    
          }, {
            "data": "AffiliatedTo",
             "width":"15%"
    
          }, {
            "data": "TPOName",
            "width":"20%"
    
          }, {
            "data": "Phone",
    
          }, {
            "data": "Website",
    
            "bSortable": false
          }],
          dom: 'lBfrtip',
          buttons: [
          $.extend( true, {}, buttonCommon, {
                    extend: 'excelHtml5',
                      title: 'Colleges',
    
                } ),
    
    
            {
              extend: 'pdf',
              title: 'Colleges'
            }
    
          ],
    
          fnRowCallback: function(nRow, aData, iDisplayIndex) {
             //Some code
            return nRow;
          }
    
    
        };
    
    
          var dataTbl = $('#tblColleges').DataTable(dataTableObj);
    
  • 将数据导出到excel后,列不会在excel中自动调整

    如何使列(将数据表数据导出到excel后)自动调整?

     var buttonCommon = {
        exportOptions: {
          format: {
            body: function(data, column, row, node) {
              return data;
            }
          }
        }
      };
    
         var dataTableObj = {
          "processing": true,
          "destroy": true,
          "scrollX": true,
          "columns": [{
            "data": "CollegeName",
            "width":"30%"
    
          }, {
            "data": "AffiliatedTo",
             "width":"15%"
    
          }, {
            "data": "TPOName",
            "width":"20%"
    
          }, {
            "data": "Phone",
    
          }, {
            "data": "Website",
    
            "bSortable": false
          }],
          dom: 'lBfrtip',
          buttons: [
          $.extend( true, {}, buttonCommon, {
                    extend: 'excelHtml5',
                      title: 'Colleges',
    
                } ),
    
    
            {
              extend: 'pdf',
              title: 'Colleges'
            }
    
          ],
    
          fnRowCallback: function(nRow, aData, iDisplayIndex) {
             //Some code
            return nRow;
          }
    
    
        };
    
    
          var dataTbl = $('#tblColleges').DataTable(dataTableObj);
    

  • 我不知道下面的解决方案有多合理,但它解决了我的问题

    我需要禁用导出按钮(Excel,PDF),当网格 空行。我们如何处理datatable导出按钮单击 行动

    这里我已经完成了action属性

           buttons: [{
          extend: 'excelHtml5',
          title: 'Colleges',
          action: function(e, dt, button, config) {
            if (this.data().length > 0) {
    
              $.fn.dataTable.ext.buttons.excelHtml5.action(e, dt, button, config);
              $scope.hasAlert = 0;
              $scope.$apply();
            } else {
              $scope.hasAlert = 2;
              $scope.alertMsg = __APP_MESSAGE__.GridEmptyMsg;
              $scope.$apply();
            }
          }
    
        }, {
          extend: 'pdf',
          title: 'Colleges',
          action: function(e, dt, button, config) {
            if (this.data().length > 0) {
    
              $.fn.dataTable.ext.buttons.pdfHtml5.action(e, dt, button, config);
              $scope.hasAlert = 0;
              $scope.$apply();
            } else {
              $scope.hasAlert = 2;
              $scope.alertMsg = __APP_MESSAGE__.GridEmptyMsg;
              $scope.$apply();
            }
          }
        },
    
      ]
    

    这个解决方案对我来说很有效,把它放在fnDrawCallback函数中

    $(tableName).dataTable({
    
     "fnDrawCallback":function () {
                       var table = $(tableName).DataTable();
                        if (table.data().length === 0)
                table.buttons('.buttons-html5').disable();
                     else
                table.buttons('.buttons-html5').enable();
    
    
                } 
    });