所有表的数据表固定标题

时间:2017-12-14 05:48:45

标签: javascript jquery html

我想为我网站中使用的所有数据表制作固定标题。

我有这个选项来添加固定标头。 var table = $('#example')。DataTable({         fixedHeader:true     });

但我怎样才能将它用于所有表格..

我的网站中有超过100个表...

function parseOptions(element) {
  element.data_options = {
    columnDefs: [],
    lengthMenu: [
      [10, 25, 50, 100, 500, 1000],
      [10, 25, 50, 100, 500, 1000]
    ],
    order: [],
    oLanguage: {
      "sProcessing": "<div class='row'><div class='col-sm-12 text-center'><i class=\"fa fa-3x fa-spin fa-refresh text-torques\"></i><h4>Processing</h4></div></div>",
      "sLengthMenu": "Show entries _MENU_"
    }
  };
  if (element.ifSort !== 1) {
    element.data_options["bSort"] = false;
  }
  element.column_list = [];
  element.primary_key = 0;
  if (element.serial === 1) {
    addSerial(element);
  }
  /* If Checkbox is required*/
  if (element.task === 1) {

    addTask(element);
  }
  /* If plus sign is required*/
  if (element.details === 1) {

    addDetails(element);
  }
  /*count columns*/
  element.columns = [];
  for (var i = element.primary_key; i < (element.primary_key + element.default_column_count); i++) {
    element.columns.push(i);
  }
  /*count excluded columns from sorting*/
  element.excluded_columns = [];
  for (var i = 0; i < element.primary_key; i++) {
    element.excluded_columns.push(i);
  }
  for (var i = 0; i < element.default_column_list.length; i++) {
    var data_name = element.default_column_list[i].name.replace(".", '$');
    if (element.replaceUnderscore) {
      var data_name = element.default_column_list[i].name.replace(".", '$').replace(/\(|\)/g, "_");
    }
    var x = {
      "name": element.default_column_list[i].name.replace(".", '$'),
      "title": element.default_column_list[i].title,
      "data": data_name,
      "className": typeof element.default_column_list[i].class !== "undefined" ? element.default_column_list[i].class : ''
    };
    element.column_list.push(x);
    if (typeof element.default_column_list[i].order_by !== "undefined") {
      element.data_options['order'].push([i, element.default_column_list[i].order_by]);
    }
  }

  if (element.action === 1) {
    addActions(element);
  }
  var dom = '';
  if (element.column_selection && element.export) {
    dom += '<"row form-group"<"col-sm-6"<"DTTT_extras">><"col-sm-6 text-right"B>>';
    addExport(element);
  } else if (!element.column_selection && !element.export) {
    dom += '';
  } else if (element.export) {
    dom += '<"row form-group"<"col-sm-12"B>>';
    addExport(element);
  } else {
    dom += '<"row form-group"<"col-sm-12"<"DTTT_extras">>>';
  }
  if (element.length_menu && element.search) {
    dom += '<"row"<"col-sm-3" l><"col-sm-4"<"actions">><"col-sm-5"f>>';
  } else if (!element.length_menu && !element.search) {
    dom += '';
  } else if (element.length_menu) {
    dom += '<"row"<"col-sm-3" l><"col-sm-9">>';
  } else {
    dom += '<"row"<"col-sm-7"><"col-sm-5"f>>';
  }
  dom += 'rt';
  if (element.info && element.paging) {
    dom += '<"row"<"col-sm-6"i><"col-sm-6"p>>';
  } else if (!element.info && !element.paging) {
    dom += '';
    element.data_options['iDisplayLength'] = element.defaultLength;
  } else if (element.info) {
    dom += '<"row"<"col-sm-12"i>>';
    element.data_options['iDisplayLength'] = element.defaultLength;
  } else {
    dom += '<"row"<"col-sm-6"p>>';
  }
  element.data_options['dom'] = dom;
  if (element.realtime) {
    if (element.ifRealtimeExtraProperty === 1) {
      element.data_options['ajax'] = {
        "url": element.url,
        "data": function(d) {
          if (typeof element.realtimeExtraProperty.key === "undefined") {
            $.each(element.realtimeExtraProperty, function(k, v) {
              d[v.key] = v.value;
            });
          } else {
            d[element.realtimeExtraProperty.key] = element.realtimeExtraProperty.value;
          }
          if (typeof window[element.realtimeCallback] === 'function') {
            window[element.realtimeCallback](d);
          }
        },
      };
    } else {
      element.data_options['ajax'] = {
        "url": element.url
      };
    }
    element.data_options['ajax']["dataSrc"] = function(json) {
      //                            for (var i = 0, ien = json.length; i < ien; i++) {
      //                                json[i][0] = '<a href="/message/' + json[i][0] + '>View message</a>';
      //                            }
      var returnVal = null;
      if (typeof json.data === "object") {
        if (json.success !== true) {
          switch (json.data.action) {
            case "login":
              if ($("#system-modal").length > 0) {
                $("#system-modal .modal-dialog").html(json.customData);
                $('#status-messages').html(json.screen_message);
                $("#system-modal").modal({
                  backdrop: 'static',
                  keyboard: false,
                  show: true
                });
                initCheckboxRadio();
                setScrMsgTimeOut();
              } else {
                window.location.href = json.data.url;
              }
              returnVal = [];
              break;
            default:
              returnVal = json.data;
          }
        }
      } else {
        if (typeof json.screen_message === "string") {
          $('#status-messages').html(json.screen_message);
          setScrMsgTimeOut();
        }
        returnVal = json.data;
      }
      if (typeof window[element.getDataCallback] === 'function') {
        returnVal = window[element.getDataCallback](returnVal);
      }
      return returnVal;
    };
    element.data_options['serverSide'] = true;
    element.data_options['processing'] = true;
  }
  element.data_options['stateSave'] = true;
  element.data_options['stateDuration'] = -1;
  if (element.data_options['order'].length <= 0) {
    element.data_options['order'].push([element.primary_key, element.order]);
  }
  element.data_options['columns'] = element.column_list;
  element.data_options['columnDefs'].push({
    "orderable": false,
    "searchable": false,
    data: null,
    targets: element.excluded_columns
  });
  element.data_options['drawCallback'] = function(settings) {
    if (element.task === 1) {
      $('.row-selected').checkboxradio();
    }
    if (element.details === 1) {
      $.each(window[element.id + '_details'], function(i, d) {
        $('#' + element.id + ' .show-details[data-id="' + d + '"]').trigger('click');
      });
    }
    var api = this.api();
    if (element.onDrawCallback && typeof element.onDrawCallback !== 'undefined') {
      try {
        window[element.onDrawCallback](api);
      } catch (err) {
        console.log(element.onDrawCallback + " is not accessible. Stack Trace" + err);
      }
    }
  };
  //
  console.log(element.data_options);

  window[element.id] = $('#' + element.id).DataTable(element.data_options); //fixed header true option
  addSelectPicker(element);
}

我如何在这里添加。

window[element.id] = $('#' + element.id).DataTable(element.data_options)

任何人都可以建议我,我该怎么做?

0 个答案:

没有答案