从具有有效日期作为值的元素中选择属性

时间:2018-01-26 21:26:23

标签: javascript jquery datatables

Javascript / jQuery选择器问题(请参阅下面的codepen链接)。当你导出到Excel时,结构(小提琴中的HTML)实际上是由jQuery Datatables创建的Excel XML,但是codepen的工作原理完全相同。

基本上,如果需要,我可以更深入地了解最终目标是什么,但目前我正在尝试获取每个列的列字母(第1行的r属性,也就是Excel标题行)日期作为标题。因此xmlhttpRequest应为validDateColumns

我设置了codepen,因此它已经注销了正确的东西 - 我可以使用[D,E,F,G,H]的正确选择器语句的一些帮助。 TIA!

https://codepen.io/murphydan/pen/wpVYPm?editors=1111

2 个答案:

答案 0 :(得分:1)

要实现此目的,您可以先filter() c个节点仅检索包含有效日期的节点。然后,您可以使用map()从这些节点创建r属性的数组。像这样:

var validDateColumns = $('c').filter(function() {
  return moment($(this).text().trim(), "MM/DD/YY", true).isValid()
}).map(function() {
   return $(this).attr('r');
}).get();

console.log(validDateColumns);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
<sheetData xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <row r="1" xmlns="">
    <c r="A1" s="2" t="inlineStr">
      <is>
        <t>Cell</t>
      </is>
    </c>
    <c r="B1" s="2" t="inlineStr">
      <is>
        <t>LIDS WIP</t>
      </is>
    </c>
    <c r="C1" s="2" t="inlineStr">
      <is>
        <t>Late</t>
      </is>
    </c>
    <c r="D1" s="2" t="inlineStr">
      <is>
        <t>01/25/18</t>
      </is>
    </c>
    <c r="E1" s="2" t="inlineStr">
      <is>
        <t>01/26/18</t>
      </is>
    </c>
    <c r="F1" s="2" t="inlineStr">
      <is>
        <t>01/29/18</t>
      </is>
    </c>
    <c r="G1" s="2" t="inlineStr">
      <is>
        <t>01/30/18</t>
      </is>
    </c>
    <c r="H1" s="2" t="inlineStr">
      <is>
        <t>01/31/18</t>
      </is>
    </c>
    <c r="I1" s="2" t="inlineStr">
      <is>
        <t>DB</t>
      </is>
    </c>
    <c r="J1" s="2" t="inlineStr">
      <is>
        <t>OS</t>
      </is>
    </c>
    <c r="K1" s="2" t="inlineStr">
      <is>
        <t>Days of Work</t>
      </is>
    </c>
    <c r="L1" s="2" t="inlineStr">
      <is>
        <t>Work in Period</t>
      </is>
    </c>
    <c r="M1" s="2" t="inlineStr">
      <is>
        <t>Avg Pieces per Day</t>
      </is>
    </c>
    <c r="N1" s="2" t="inlineStr">
      <is>
        <t>Cell Capacity</t>
      </is>
    </c>
    <c r="O1" s="2" t="inlineStr">
      <is>
        <t>SAP Window Open To</t>
      </is>
    </c>
    <c r="P1" s="2" t="inlineStr">
      <is>
        <t>Incoming Work</t>
      </is>
    </c>
  </row>
  <row r="2" xmlns="">
    <c r="A2" t="inlineStr">
      <is>
        <t>Acrylic Trophy Hub</t>
      </is>
    </c>
    <c r="B2" t="n">
      <v>0</v>
    </c>
    <c r="C2" t="n">
      <v>0</v>
    </c>
    <c r="D2" t="n">
      <v>0</v>
    </c>
    <c r="E2" s="20" t="n">
      <v>12</v>
    </c>
    <c r="F2" t="n">
      <v>2</v>
    </c>
    <c r="G2" s="20" t="n">
      <v>120</v>
    </c>
    <c r="H2" s="20" t="n">
      <v>312</v>
    </c>
    <c r="I2" t="n">
      <v>26</v>
    </c>
    <c r="J2" t="n">
      <v>0</v>
    </c>
    <c r="K2" t="n">
      <v>0.00</v>
    </c>
    <c r="L2" t="n">
      <v>472</v>
    </c>
    <c r="M2" t="n">
      <v>0.00</v>
    </c>
    <c r="N2" t="n">
      <v>6</v>
    </c>
    <c r="O2" t="inlineStr">
      <is>
        <t>02/02/18</t>
      </is>
    </c>
    <c r="P2" t="n">
      <v>41</v>
    </c>
  </row>
  <row r="3" xmlns="">
    <c r="A3" t="inlineStr">
      <is>
        <t>ADC4</t>
      </is>
    </c>
    <c r="B3" t="n">
      <v>0</v>
    </c>
    <c r="C3" t="n">
      <v>0</v>
    </c>
    <c r="D3" t="n">
      <v>0</v>
    </c>
    <c r="E3" s="20" t="n">
      <v>25</v>
    </c>
    <c r="F3" s="20" t="n">
      <v>18</v>
    </c>
    <c r="G3" t="n">
      <v>3</v>
    </c>
    <c r="H3" s="20" t="n">
      <v>18</v>
    </c>
    <c r="I3" t="n">
      <v>0</v>
    </c>
    <c r="J3" t="n">
      <v>24</v>
    </c>
    <c r="K3" t="n">
      <v>0.34</v>
    </c>
    <c r="L3" t="n">
      <v>88</v>
    </c>
    <c r="M3" t="n">
      <v>257.88</v>
    </c>
    <c r="N3" t="n">
      <v>12</v>
    </c>
    <c r="O3" t="inlineStr">
      <is>
        <t>01/31/18</t>
      </is>
    </c>
    <c r="P3" t="n">
      <v>225</v>
    </c>
  </row>
</sheetData>

答案 1 :(得分:1)

此代码仅适用于第一行并删除r属性值的最后一个字符(&#34; 1&#34;)。我使用了与他的答案中使用的 @RoryMcCrossan 相同的技术,并通过一些修改来纠正它,以得到你想要的结果。

&#13;
&#13;
var validDateCols = $('row[r="1"] > c').filter(function() {
  return moment($(this).text().trim(), "MM/DD/YY", true).isValid()
}).map(function() {
   return $(this).attr('r').slice(0, -1);
}).get();

console.log(validDateCols);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
<sheetData xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <row r="1" xmlns="">
    <c r="A1" s="2" t="inlineStr">
      <is>
        <t>Cell</t>
      </is>
    </c>
    <c r="B1" s="2" t="inlineStr">
      <is>
        <t>LIDS WIP</t>
      </is>
    </c>
    <c r="C1" s="2" t="inlineStr">
      <is>
        <t>Late</t>
      </is>
    </c>
    <c r="D1" s="2" t="inlineStr">
      <is>
        <t>01/25/18</t>
      </is>
    </c>
    <c r="E1" s="2" t="inlineStr">
      <is>
        <t>01/26/18</t>
      </is>
    </c>
    <c r="F1" s="2" t="inlineStr">
      <is>
        <t>01/29/18</t>
      </is>
    </c>
    <c r="G1" s="2" t="inlineStr">
      <is>
        <t>01/30/18</t>
      </is>
    </c>
    <c r="H1" s="2" t="inlineStr">
      <is>
        <t>01/31/18</t>
      </is>
    </c>
    <c r="I1" s="2" t="inlineStr">
      <is>
        <t>DB</t>
      </is>
    </c>
    <c r="J1" s="2" t="inlineStr">
      <is>
        <t>OS</t>
      </is>
    </c>
    <c r="K1" s="2" t="inlineStr">
      <is>
        <t>Days of Work</t>
      </is>
    </c>
    <c r="L1" s="2" t="inlineStr">
      <is>
        <t>Work in Period</t>
      </is>
    </c>
    <c r="M1" s="2" t="inlineStr">
      <is>
        <t>Avg Pieces per Day</t>
      </is>
    </c>
    <c r="N1" s="2" t="inlineStr">
      <is>
        <t>Cell Capacity</t>
      </is>
    </c>
    <c r="O1" s="2" t="inlineStr">
      <is>
        <t>SAP Window Open To</t>
      </is>
    </c>
    <c r="P1" s="2" t="inlineStr">
      <is>
        <t>Incoming Work</t>
      </is>
    </c>
  </row>
  <row r="2" xmlns="">
    <c r="A2" t="inlineStr">
      <is>
        <t>Acrylic Trophy Hub</t>
      </is>
    </c>
    <c r="B2" t="n">
      <v>0</v>
    </c>
    <c r="C2" t="n">
      <v>0</v>
    </c>
    <c r="D2" t="n">
      <v>0</v>
    </c>
    <c r="E2" s="20" t="n">
      <v>12</v>
    </c>
    <c r="F2" t="n">
      <v>2</v>
    </c>
    <c r="G2" s="20" t="n">
      <v>120</v>
    </c>
    <c r="H2" s="20" t="n">
      <v>312</v>
    </c>
    <c r="I2" t="n">
      <v>26</v>
    </c>
    <c r="J2" t="n">
      <v>0</v>
    </c>
    <c r="K2" t="n">
      <v>0.00</v>
    </c>
    <c r="L2" t="n">
      <v>472</v>
    </c>
    <c r="M2" t="n">
      <v>0.00</v>
    </c>
    <c r="N2" t="n">
      <v>6</v>
    </c>
    <c r="O2" t="inlineStr">
      <is>
        <t>02/02/18</t>
      </is>
    </c>
    <c r="P2" t="n">
      <v>41</v>
    </c>
  </row>
  <row r="3" xmlns="">
    <c r="A3" t="inlineStr">
      <is>
        <t>ADC4</t>
      </is>
    </c>
    <c r="B3" t="n">
      <v>0</v>
    </c>
    <c r="C3" t="n">
      <v>0</v>
    </c>
    <c r="D3" t="n">
      <v>0</v>
    </c>
    <c r="E3" s="20" t="n">
      <v>25</v>
    </c>
    <c r="F3" s="20" t="n">
      <v>18</v>
    </c>
    <c r="G3" t="n">
      <v>3</v>
    </c>
    <c r="H3" s="20" t="n">
      <v>18</v>
    </c>
    <c r="I3" t="n">
      <v>0</v>
    </c>
    <c r="J3" t="n">
      <v>24</v>
    </c>
    <c r="K3" t="n">
      <v>0.34</v>
    </c>
    <c r="L3" t="n">
      <v>88</v>
    </c>
    <c r="M3" t="n">
      <v>257.88</v>
    </c>
    <c r="N3" t="n">
      <v>12</v>
    </c>
    <c r="O3" t="inlineStr">
      <is>
        <t>01/31/18</t>
      </is>
    </c>
    <c r="P3" t="n">
      <v>225</v>
    </c>
  </row>
</sheetData>
&#13;
&#13;
&#13;