0

I have a table as follows:

<table cellspacing="0" cellpadding="0" border="0" class="layui-table">
    <tbody>
    <tr data-index="0" class="">
        <td data-field="sid" data-content="123456">
            <div class="layui-table-cell laytable-cell-1-sid"> 123456 </div>
        </td>
        <td>
            <div></div>
        </td>
    </tr>
    <tr data-index="1" class="">
        <td data-field="sid" data-content="100012">
            <div class="layui-table-cell laytable-cell-1-sid"> 100012 </div>
        </td>
        <td>
            <div></div>
        </td>
    </tr>
    </tbody>
</table>

I can use the following scripts to get tbody but not tr or td

$("document").ready(function(){
    var tb = $('.layui-table-main table:eq(0) tbody');
    console.log(tb);
    var size=tb.find("tr").length;
    console.log(size);
});

What I have been trying to do is to get each td's value. How can I get them?

2
  • why not use the class of the table? like var tb = $('.layui-table:eq(0) tbody'); Commented Feb 1, 2018 at 3:32
  • One thing to mention: the table is generated via jquery.table.rowspan.js. The provided answers are not able to get the right results. Commented Feb 1, 2018 at 5:13

1 Answer 1

2

You can find the list of tr using .find("tr") on the tbody and then loop through the result to get each row. Within that loop, you can find the list of td using .find("td") on each row. See the implementation below.

$("document").ready(function() {
  var tb = $('.layui-table:eq(0) tbody');
  var size = tb.find("tr").length;
  console.log("Number of rows : " + size);
  tb.find("tr").each(function(index, element) {
    var colSize = $(element).find('td').length;
    console.log("  Number of cols in row " + (index + 1) + " : " + colSize);
    $(element).find('td').each(function(index, element) {
      var colVal = $(element).text();
      console.log("    Value in col " + (index + 1) + " : " + colVal.trim());
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table cellspacing="0" cellpadding="0" border="0" class="layui-table">
  <tbody>
    <tr data-index="0" class="">
      <td data-field="sid" data-content="123456">
        <div class="layui-table-cell laytable-cell-1-sid"> 123456 </div>
      </td>
      <td>
        <div></div>
      </td>
    </tr>
    <tr data-index="1" class="">
      <td data-field="sid" data-content="100012">
        <div class="layui-table-cell laytable-cell-1-sid"> 100012 </div>
      </td>
      <td>
        <div></div>
      </td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

6 Comments

your code is ok to understand. But I still got number of rows = 0.
Just run the code snippet and see the result yourself. If this doesn't solve your issue, you have some issue that is not covered in this question.
thanks for the response. I update my original post. the difference is that the table is not static one, but is generated from jquery.
Add the code that generates the table to the question please.
can anyone help me out?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.