【问题标题】:Multiple Objects in an array数组中的多个对象
【发布时间】:2016-11-17 14:34:47
【问题描述】:

我不是很喜欢 jQuery,但我需要学习它,这样我才能意识到我正在做的一些项目。

我使用带有 select-Extension 的 Datatables 来多选表格行。之后我需要在 php.ini 中处理这些行的值。所有这些都像一个魅力,问题是我只能在一个表格中使用一个表。这意味着我只能将具有“table_select”类的第一个表的结果放入 $_POST-Variable 中。

我尝试更改 jQuery-Code 以使用此类迭代所有对象,因此我创建了一个数组并尝试使用 [Array.length] 推送此数组,但我仍然只能获取 Array[0] 中的值。

我做错了什么?

    var table = [];

table[table.length] =  $('.table_select').DataTable( {
    'initComplete': function(){
    var api = this.api();
     api
        .rows()
            .every(function(){
           var data = this.data();
           if(data[1] === '1'){
              api.cells(this.index(), 0).checkboxes.select();
           }
        });
    },
    order: [[2, "asc"]],
    paging:false,
    info:false,
    filter:false,
    language: {
        "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/German.json",
    },
    columns: [
        null,
        { "visible": false},
        null
    ],
    'columnDefs': [{
        'targets': 0,
        'checkboxes': {
            'selectRow': true,
            'selectAll': false
        }
    }],
    select: {
        style: 'multi'
    }
}); 
flen = table.length;

在这种情况下,flen 总是“1”,即使我有 4 个带有“table_select”-Class 的 Tabes。

提前感谢您的帮助!

编辑:

我根据cmets把代码改成:

table =  $('.table_select').DataTable( {
    'initComplete': function(){
    var api = this.api();
     api
        .rows()
            .every(function(){
           var data = this.data();
           if(data[1] === '1'){
              api.cells(this.index(), 0).checkboxes.select();
           }
        });
    },
    order: [[2, "asc"]],
    paging:false,
    info:false,
    filter:false,
    language: {
        "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/German.json",
    },
    columns: [
        null,
        { "visible": false},
        null
    ],
    'columnDefs': [{
        'targets': 0,
        'checkboxes': {
            'selectRow': true,
            'selectAll': false
        }
    }],
    select: {
        style: 'multi'
    }
}); 

alert(table.length);

在这种情况下,警报总是“0”。

Edit2:示例 jsfiddle:jsfiddle.net/tu59s6Ls

【问题讨论】:

    标签: jquery arrays loops


    【解决方案1】:

    让我解释一下你在做什么

    var table = [];  // this is setting length 0 as no value
    
    
    table[table.length] = ... // here you are setting table[0] = ...
    

    这意味着您将整个值设置为索引 0。因此,jQuery 数据表中的任何障碍都将其设置为索引 0,因此表的长度始终为 1

    您需要在表变量中设置整个值而不是索引 0

    table =  ...
    

    【讨论】:

    • 好的,我明白你的意思,我误解了w3schools.com/js/js_arrays.asp 中的“推送数组”的描述。我需要将每个元素(确切:表)与“table_select”类放在一个数组中,所以如果我有 4 个表,我需要有“table[0]”、“table[1]”、“table [2]”、“表 [3]”。我怎样才能做到这一点?
    【解决方案2】:

    在您的代码中,table 变量是数组,并且在数组的第一个位置 (table[0]) 您放置了对DataTable 元素的引用。您可能有 4 个带有 table_select 类的元素,但您将对它们的引用保存在第一项中(而不是变量中)。

    实际上 - table[0].length == 4,但 table.length == 0。

    你可以使用

    table[0].lenght
    

    table = $('.table_select').DataTable
    

    (没有你那里的数组)。

    【讨论】:

    • 我尝试使用table = $('.table_select').DataTable flen 在这种情况下总是“0”
    • 你能提供一个可行的例子吗? (jsfiddle什么的)。 datatable 函数应该返回一个 jquery 对象及其应用的元素(如果你真的有 4 个表,那么长度应该是 4 个)。
    • 如果你运行alert($('.table_select').length)会发生什么?
    • alert($('.table_select').length) 回馈 4 Working jsfiddle: jsfiddle.net/tu59s6Ls 也许是因为我读了$('.table_select').DataTable
    【解决方案3】:

    DataTable 在实例化时不会返回您想要的数据。

    您可以通过 context data()

    访问它

    所以在实例化数据表时,将其保存到一个变量中,然后访问该变量 .context .data() 属性

    编辑

    使用.data() 代替上下文

    $(document).ready(function() {
      let datatable = $('.table_select').DataTable({
        'initComplete': function() {
          var api = this.api();
          api
            .rows()
            .every(function() {
              var data = this.data();
              if (data[1] === '1') {
                api.cells(this.index(), 0).checkboxes.select();
              }
            });
        },
        order: [
          [2, "asc"]
        ],
        paging: false,
        info: false,
        filter: false,
        language: {
          "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/German.json",
        },
        columns: [
          null, {
            "visible": false
          },
          null
        ],
        'columnDefs': [{
          'targets': 0,
          'checkboxes': {
            'selectRow': true,
            'selectAll': false
          }
        }],
        select: {
          style: 'multi'
        }
      });
    
      let table = datatable.data();
    
      alert(table.length);
      alert($('.table_select').length);
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.datatables.net/v/bs/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.12/b-1.2.2/b-colvis-1.2.2/b-flash-1.2.2/b-html5-1.2.2/b-print-1.2.2/fh-3.1.2/se-1.2.0/datatables.min.js"></script>
    <link href="https://cdn.datatables.net/v/bs/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.12/b-1.2.2/b-colvis-1.2.2/b-flash-1.2.2/b-html5-1.2.2/b-print-1.2.2/fh-3.1.2/se-1.2.0/datatables.min.css" rel="stylesheet" />
    <form id="form_login" action="" data-parsley-validate class="form-horizontal form-label-left select_table_form" method="post">
      <table class="table table_select table-hover display" width="100%;" cellspacing="0">
        <thead>
          <tr>
            <th>
            </th>
            <th>
            </th>
            <th>
              Test
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              Test
            </td>
            <td>
              Test
            </td>
            <td>
              Value 1
            </td>
          </tr>
        </tbody>
      </table>
      <table class="table table_select table-hover display" width="100%;" cellspacing="0">
        <thead>
          <tr>
            <th>
            </th>
            <th>
            </th>
            <th>
              Test
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              Test
            </td>
            <td>
              Test
            </td>
            <td>
              Value 1
            </td>
          </tr>
        </tbody>
      </table>
      <table class="table table_select table-hover display" width="100%;" cellspacing="0">
        <thead>
          <tr>
            <th>
            </th>
            <th>
            </th>
            <th>
              Test
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              Test
            </td>
            <td>
              Test
            </td>
            <td>
              Value 1
            </td>
          </tr>
        </tbody>
      </table>
      <table class="table table_select table-hover display" width="100%;" cellspacing="0">
        <thead>
          <tr>
            <th>
            </th>
            <th>
            </th>
            <th>
              Test
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              Test
            </td>
            <td>
              Test
            </td>
            <td>
              Value 1
            </td>
          </tr>
        </tbody>
      </table>
    </form>

    【讨论】:

    • 感谢您的提示。我现在在数组中获得了 4 个对象,但似乎我在此步骤中丢失了相关数据:[jsfiddle.net/tu59s6Ls/1/] 基于此脚本 [gyrocode.com/articles/jquery-datatables-checkboxes/] 我尝试获取所有选定行的值。但是,如果我像在 jsfiddle 中那样转储对象,则似乎不再有数据了。
    • @Daniel 刚刚更新了它。如上所述使用 .data()
    • @GuiServ Just update my jsfiddle jsfiddle.net/tu59s6Ls/2 I get the data from the tables, but i don't get the value '1' when the row is selected, just the inital set values are set on '1'。此外,我只在 jQuery 中得到它们,而我需要在 $_POST-Variable 中使用它们来处理 php 中的结果。提交后,我只从表单中的第一个表中获取选定的行,其他行被忽略。我想我必须用添加的代码迭代每个对象。感谢所有的努力,我希望我表达自己至少可以理解顺便说一句:我怎样才能让 jsfiddle 与提交一起工作?
    猜你喜欢
    • 1970-01-01
    • 2015-02-13
    • 2013-01-27
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多