简体   繁体   English

可排序的嵌套ul li如何保存到数据库?

[英]Sortable nested ul li how to save to database?

I need a database that contains a set of pages: their ID, parent ID (0 if no parents) and order. 我需要一个包含一组页面的数据库:它们的ID,父ID(如果没有父,则为0)和顺序。

+------+------------+-----------+-------------+
|  ID  |  parentID  |   order   |    Name     |
+------+------------+-----------+-------------+
|  1   |      0     |     1     |    page 1   |
|  2   |      1     |     1     |    page 2   |
|  3   |      1     |     2     |    page 3   |
|  4   |      0     |     2     |    page 4   |
|  5   |      0     |     3     |    page 5   |
+------+------------+-----------+-------------+

Then I have a sortable nested ul li that works with jQuery such as this 然后我有一个可排序的嵌套ul li ,它可以像这样使用jQuery

<ul id="sortablenested">
  <li>page 1
  <ul>
     <li>page 2</li>
     <li>page 3</li>
  </ul>
  </li>
  <li>page 4
  <ul></ul>
  </li>
  <li>page 5
  <ul></ul>
  </li>
</ul>

Now if it wasn't nested, I would just use an hidden input within each li tag passing an array and then go through the array with a foreach and write the position in the DB but when I move a li from the root ul to a sub ul with the same method it wouldn't nest it but just go through the array ignoring that it's a child. 现在,如果它不是嵌套的,我将在每个li标记中使用一个隐藏的输入传递一个数组,然后使用foreach遍历该数组并在数据库中写入位置,但是当我将li从根ul移到a时sub ul使用相同的方法,它不会嵌套它,而只是通过数组忽略它是一个孩子。

What is the best/right method to submit the order to the database? 将订单提交到数据库的最佳/正确方法是什么?

I would either send whole HTML of the list tree ( innerHtml ) if it's this simple, or write JavaScript function to convert it to JSON, and send that. 如果这很简单,我要么发送列表树的整个HTML( innerHtml ),要么编写JavaScript函数将其转换为JSON并发送。 And parse that on server. 并在服务器上解析它。

See How to serialize DOM node to JSON even if there are circular references? 请参阅即使有循环引用也如何将DOM节点序列化为JSON?

在您的foreach中尝试一下

array('menu' => array(
array( father => 'page 1', link => 'url.php',
child=> array(
array(name=>'page 2', url=>'url.php'),
array(name=>'page 3', url=>'url.php'),)),
array( father => 'page 4', link => 'url.php'),
array( father => 'page 5', link => 'url.php'))
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM