Tablesorter не работает со столом - PullRequest
0 голосов
/ 28 марта 2020

Таблица Это мои коды cs html и js. Я попробовал плагин tableorter на других столах (просто нормальные таблицы с thead и tbody), и он работал. Я думаю, проблема в том, что внутри тегов tbody нет тела, потому что я добавляю тело с помощью JS и webapi. У вас есть идея, как я могу это исправить и отсортировать эту таблицу? Я новичок, поэтому буду признателен за любую помощь или совет.

<head>
    <script src="lib/jquery/dist/jquery.js" asp-append-version="true"></script>
    <script src="js/jquery.tablesorter.min.js"></script>
    <link rel="stylesheet" type="text/css" href="~/css/theme.default.css">
</head>
    <h3>Branch Index</h3>
    <div id="branchIndex">
        <table class="table tablesorter mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--2dp customTable" id="branchIndexTablee">
            <thead>
                <tr>
                    <th>Branch Name</th>
                    <th>Open Now</th>
                    <th>Number Of Assets</th>
                    <th>Number Of Patrons</th>
                </tr>
            </thead>
            <tbody id="branchess">
            </tbody>
        </table>
    </div>

    <script>
        $(document).ready(function () {
            $('table').tablesorter({ sortList: [[0, 0]] });
        });
    </script>

js

const uri = 'api/BranchApi';
let branchess = [];

function getItems() {
    fetch(uri)
        .then(response => response.json())
        .then(data => _displayItems(data))
        .catch(error => console.error('Unable to get items.', error));
}

function _displayItems(data) {
    var table = jQuery(".tablesorter");
    table.tablesorter();


  const tBody = document.getElementById('branchess');
    tBody.innerHTML = '';

    data.forEach(item => {

    let tr = tBody.insertRow();
    let td1 = tr.insertCell(0);

    var a = document.createElement('a');
    var name = document.createTextNode(item.branchName);
    a.appendChild(name);
    a.title = item.name;
    a.href = "Branch/Detail/" + item.id;
    td1.appendChild(a);

    let td2 = tr.insertCell(1);
    let time = document.createTextNode(item.isOpen);
    td2.append(time);

    let td3 = tr.insertCell(2);
    let telephoneN = document.createTextNode(item.numberOfAssets);
    td3.appendChild(telephoneN);

    let td4 = tr.insertCell(3);
    let address = document.createTextNode(item.numberOfPatrons);
    td4.appendChild(address);

    });
    branchess = data;
}

1 Ответ

2 голосов
/ 28 марта 2020

Опечатка: идентификатор был назначен в неправильном месте:

СЕЙЧАС:

<tbody >
   <tr>
      <td id="branchess"></td>
   </tr>
</tbody>

Ожидается:

<tbody id="branchess">
   <tr>
      <td ></td>
   </tr>
</tbody>

<!DOCTYPE html>
<html lang="en">

<head>
   <script src="https://code.jquery.com/jquery-3.4.1.min.js"
      integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js"></script>
   <link rel="stylesheet" type="text/css" href="~/css/theme.default.css">
</head>
<h3>Branch Index</h3>
<div id="branchIndex">
   <table
      class="table tablesorter mdl-data-table mdl-js-data-table mdl-data-table--selectable mdl-shadow--2dp customTable"
      id="branchIndexTablee">
      <thead>
         <tr>
            <th>Branch Name</th>
            <th>Open Now</th>
            <th>Number Of Assets</th>
            <th>Number Of Patrons</th>
         </tr>
      </thead>
      <tbody id="branchess">
         <tr>
            <td></td>
         </tr>
      </tbody>
   </table>
</div>

<script>
   $(document).ready(function () {
      _displayItems([{
            id: 1,
            branchName: "Random",
            isOpen: true,
            numberOfAssets: 10,
            numberOfPatrons: 11
         },
         {
            id: 2,
            branchName: "Lost",
            isOpen: true,
            numberOfAssets: 10,
            numberOfPatrons: 11
         },
         {
            id: 3,
            branchName: "Found",
            isOpen: false,
            numberOfAssets: 10,
            numberOfPatrons: 11
         },
         {
            id: 4,
            branchName: "OK",
            isOpen: true,
            numberOfAssets: 10,
            numberOfPatrons: 11
         },

      ])
      $('table').tablesorter({
         sortList: [
            [0, 0]
         ]
      });
   });

   function _displayItems(data) {
      


      const tBody = document.getElementById('branchess');
      tBody.innerHTML = '';

      data.forEach(item => {

         let tr = tBody.insertRow();
         let td1 = tr.insertCell(0);

         var a = document.createElement('a');
         var name = document.createTextNode(item.branchName);
         a.appendChild(name);
         a.title = item.name;
         a.href = "Branch/Detail/" + item.id;
         td1.appendChild(a);

         let td2 = tr.insertCell(1);
         let time = document.createTextNode(item.isOpen ? 'OPEN' :  'CLOSED');
         td2.append(time);

         let td3 = tr.insertCell(2);
         let telephoneN = document.createTextNode(item.numberOfAssets);
         td3.appendChild(telephoneN);

         let td4 = tr.insertCell(3);
         let address = document.createTextNode(item.numberOfPatrons);
         td4.appendChild(address);

      });
      // branchess = data;
      var table = jQuery(".tablesorter");
      table.tablesorter();
   }
</script>

</html>
...