duanpi7578 2018-08-14 13:23
浏览 60
已采纳

使用拖放更新数据顺序

I show some data from the DB on a table ordered by a column in the DB called order:

<tbody>
    <tr id="1" data-order="1">
        <td>1</td>    <!-- id from DB -->
        <td>Test</td> 
        <td>1</td>    <!-- order from DB  --> 
        <td>
            <a>Edit</a>
            <a>Delete</a>
        </td>
    </tr>
    ..
</tbody>

I use jquery-uri for drag and drop http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js

JS code:

jQuery("table tbody").sortable({
    update: function (event, ui) {
        UpdateOrder();
    }
});

function UpdateOrder() {
    jQuery("table tbody tr").each(function () {
        //Do something.
    }
}

It's working and I can drag and drop the elements, But how to save the new order of elements?

Like the WordPress menus drag and drop feature, Where you could organize the pages in the menu using drag-drop.

I could create a form beneath the table:

<form method="POST">
    <input type="submit" name="update_order" value="Save" >
</form>

Then check if that form is submitted:

if( isset($_POST['update_order']) ){
    global $wpdb;
    $wpdb->update('data', array("order" => ) , array('id' => ));
}

But how to gather all the elements order and ids to update them?

How to do that?

  • 写回答

1条回答 默认 最新

  • doufei1893 2018-08-15 10:23
    关注

    Please try this

    $(document).ready(function() {
        jQuery("table tbody").sortable();
        // Save button click event
           $("[name='update_order']").click(function(e){
           e.preventDefault();
           UpdateOrder();
        });
     });
     
     function UpdateOrder() {
        var jsonObj = [];
        var id = '';
        var existing_order = '';
        var updated_order = '';
        var updated_row = {};
        jQuery("table tbody tr").each(function (index, value) {
          if((index+1) != $(this).data('order'))
          { // To check whether the current row order has been changed or not. Only the changed order rows only needs to be updated.
            id = $(this).attr("id");
            existing_order = $(this).data('order');
            updated_order = (index+1);
            updated_row = {}
            updated_row ["id"] = id;
            updated_row ["existing_order"] = existing_order; // I just added for your reference. If you do not need this value, comment this line.
            updated_row ["updated_order"] = updated_order;
            jsonObj.push(updated_row);
          }
        });
        console.log(JSON.stringify(jsonObj));
    //ex: [{"id":"2","existing_order":2,"updated_order":1},{"id":"3","existing_order":3,"updated_order":2},{"id":"1","existing_order":1,"updated_order":3}]
        $('form').append("<input type='hidden' name='updated_rows' value='"+JSON.stringify(jsonObj)+"'>").submit(); // To send the values to server side script (here, PHP). Please do empty validation before you send to server if you need
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <table>
      <tbody>
          <tr id="1" data-order="1">
              <td>1</td>    <!-- id from DB -->
              <td>Test</td>
              <td>1</td>    <!-- order from DB  -->
              <td>
                  <a>Edit</a>
                  <a>Delete</a>
              </td>
          </tr>
          <tr id="2" data-order="2">
              <td>2</td>    <!-- id from DB -->
              <td>Test</td>
              <td>2</td>    <!-- order from DB  -->
              <td>
                  <a>Edit</a>
                  <a>Delete</a>
              </td>
          </tr>
          <tr id="3" data-order="3">
              <td>3</td>    <!-- id from DB -->
              <td>Test</td>
              <td>3</td>    <!-- order from DB  -->
              <td>
                  <a>Edit</a>
                  <a>Delete</a>
              </td>
          </tr>
      </tbody>
    </table>
    <form method="POST">
    <input type="submit" name="update_order" value="Save" >
    </form>

    PHP script

    if( isset($_POST['update_order']) ){
      global $wpdb;
      if(!empty($_POST['updated_rows'])) {
        $updated_rows_json = $_POST['updated_rows'];
        $updated_rows_ary = json_decode($updated_rows_json, true);
        foreach ($updated_rows_ary as $updated_row) {
          $wpdb->update('data', array("order" => $updated_row['updated_order']) , array('id' => $updated_row['id']));
        }
      }
    }
    
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型