如何从具有data-id属性的表格行中获取数据?

时间:2018-10-09 10:24:20

标签: javascript jquery

我有html表:

<table data-role="table" data-mode="columntoggle:none" class="ui-responsive ui-table">
    <thead>
        <tr></tr>
    </thead>
    <tbody>
        <tr>
            <td style="vertical-align: inherit;text-align:center;">
                <label for="">...</label>
            </td>
            <td style="text-align:center;">
                <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
                    <input type="text" id="propFieldName" value="..">
                    </div>
                </td>
            </tr>
                <tr data-id="">
                <td style="vertical-align: inherit;text-align:center;">
                    <label for="">numerItem</label>
                </td>
                <td style="text-align:center;">
                    <div class="ui-select">
                        <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
                            <span>001</span>
                            <select>
                                <option value="">001</option>
                                <option value="">002</option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
            <tr data-id="5075">
                <td style="vertical-align: inherit;text-align:center;">
                    <label for="">diametr</label>
                </td>
                <td style="text-align:center;">
                    <div class="ui-select">
                        <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
                            <span>6</span>
                            <select>
                                <option value="">3</option>
                                <option value="">4</option>
                                <option value="">6</option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
            <tr data-id="5076">
                <td style="vertical-align: inherit;text-align:center;">
                    <label for="">multi</label>
                </td>
                <td style="text-align:center;">
                    <div class="ui-select">
                        <div id="select-49-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
                            <span>no</span>
                            <select>
                                <option value="">yes</option>
                                <option value="">no</option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table> 

单击某些按钮时,我需要获取数据表中具有data-id属性和span元素内的文本的所有行。

例如,根据上表,单击后的期望结果是:

[{ "id":"5075", "value":6 },{ "id":"5076", "value":"no" }]

我的问题是如何从具有data-id属性和值(即value不为空)的表中仅获取行,以及如何从这些行的跨度中获取文本?

4 个答案:

答案 0 :(得分:4)

您可以使用Has Attribute Selector [name]TR的属性与:not()关联,以排除空值。

然后.map()获得本机数组。

var arr = $('tr[data-id]:not([data-id=""])').map(function() {
  return {
    id: $(this).data('id'),
    value: $(this).find('span').text()
  };
}).get();

var arr = $('tr[data-id]:not([data-id=""])').map(function() {
  return {
    id: $(this).data('id'),
    value: $(this).find('span').text()
  };
}).get();

console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-role="table" data-mode="columntoggle:none" class="ui-responsive ui-table">
  <thead>
    <tr></tr>
  </thead>
  <tbody>
    <tr>
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">...</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
          <input type="text" id="propFieldName" value="..">
        </div>
      </td>
    </tr>
    <tr data-id="">
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">numerItem</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-select">
          <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
            <span>001</span>
            <select>
              <option value="">001</option>
              <option value="">002</option>
            </select>
          </div>
        </div>
      </td>
    </tr>
    <tr data-id="5075">
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">diametr</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-select">
          <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
            <span>6</span>
            <select>
              <option value="">3</option>
              <option value="">4</option>
              <option value="">6</option>
            </select>
          </div>
        </div>
      </td>
    </tr>
    <tr data-id="5076">
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">multi</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-select">
          <div id="select-49-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
            <span>no</span>
            <select>
              <option value="">yes</option>
              <option value="">no</option>
            </select>
          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:0)

function btnClick() {
  const data = [];
  const $rows = $("#table tr[data-id]");
  for (let i = 0; i < $rows.length; i++) {
    let obj = {};
    const $row = $($rows[i]);
    obj["id"] = $row.data("id");
    obj["value"] = $row.find('span').text();
    data.push(obj);
  }
  
  console.log(data)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-role="table" data-mode="columntoggle:none" class="ui-responsive ui-table" id="table">
  <thead>
    <tr></tr>
  </thead>
  <tbody>
    <tr>
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">...</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
          <input type="text" id="propFieldName" value="..">
        </div>
      </td>
    </tr>
    <tr data-id="1">
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">numerItem</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-select">
          <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
            <span>001</span>
            <select>
              <option value="">001</option>
              <option value="">002</option>
            </select>
          </div>
        </div>
      </td>
    </tr>
    <tr data-id="5075">
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">diametr</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-select">
          <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
            <span>6</span>
            <select>
              <option value="">3</option>
              <option value="">4</option>
              <option value="">6</option>
            </select>
          </div>
        </div>
      </td>
    </tr>
    <tr data-id="5076">
      <td style="vertical-align: inherit;text-align:center;">
        <label for="">multi</label>
      </td>
      <td style="text-align:center;">
        <div class="ui-select">
          <div id="select-49-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
            <span>no</span>
            <select>
              <option value="">yes</option>
              <option value="">no</option>
            </select>
          </div>
        </div>
      </td>
    </tr>
  </tbody>
</table>
<button onclick="btnClick()">get data</button>

希望这会对您有所帮助:)

答案 2 :(得分:0)

var result = [];
$('.ui-table tr').each(function(){
    if(typeof $(this).data('id') != undefined && $(this).data('id') != '' && $(this).find('span').text().trim() != ''){
        var obj = {}
        obj.id = $(this).data('id');
        obj.value = $(this).find('span').text();
        result.push(obj); 
    }
});

答案 3 :(得分:0)

$('#getData' ).click(function() {


    $("#myTable tr[data-id]").each(function(){
    var idval=$(this).attr('data-id');
    var mydata=[];
    if(idval!='' && idval!=undefined)
    {
    var cat =  $(this).children('td:nth-child(2)').children('div:nth-child(1)').children('div:first-child').children('span').text();
    mydata.push({ "id":idval, "value":cat});
    }
    
    console.log(mydata);
          
      });
   
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table data-role="table" id="myTable" data-mode="columntoggle:none" class="ui-responsive ui-table">
    <thead>
        <tr></tr>
    </thead>
    <tbody>
        <tr>
            <td style="vertical-align: inherit;text-align:center;">
                <label for="">...</label>
            </td>
            <td style="text-align:center;">
                <div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
                    <input type="text" id="propFieldName" value="..">
                    </div>
                </td>
            </tr>
                <tr data-id="">
                <td style="vertical-align: inherit;text-align:center;">
                    <label for="">numerItem</label>
                </td>
                <td style="text-align:center;">
                    <div class="ui-select">
                        <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
                            <span>001</span>
                            <select>
                                <option value="">001</option>
                                <option value="">002</option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
            <tr data-id="5075">
                <td style="vertical-align: inherit;text-align:center;">
                    <label for="">diametr</label>
                </td>
                <td style="text-align:center;">
                    <div class="ui-select">
                        <div id="select-48-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
                            <span>6</span>
                            <select>
                                <option value="">3</option>
                                <option value="">4</option>
                                <option value="">6</option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
            <tr data-id="5076">
                <td style="vertical-align: inherit;text-align:center;">
                    <label for="">multi</label>
                </td>
                <td style="text-align:center;">
                    <div class="ui-select">
                        <div id="select-49-button" class="ui-btn ui-icon-carat-d ui-btn-icon-right ui-corner-all ui-shadow">
                            <span>no</span>
                            <select>
                                <option value="">yes</option>
                                <option value="">no</option>
                            </select>
                        </div>
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
    
     <input type="button"  value="Get Data"  id="getData" />