【问题标题】:jquery nestable does not updatejquery可嵌套不更新
【发布时间】:2015-09-02 21:19:04
【问题描述】:

我正在使用 jquery nestable 创建一个潜在学生可能感兴趣的学术课程列表。从 mysql 中提取的 16 个课程如下所示:

<ol class="dd-list">

if(mysqli_num_rows($result)) {
    $order = array();
    while($item = mysqli_fetch_assoc($result)) {
        echo '<li class="dd-item" data-id="'.$item['id'].'"><div class="dd-handle">',$item['program'],'</div></li>';
    }

   </ol>
 } else { 
   <p>Sorry!  There are no items in the system.</p>
 }

那里没有问题。我有第二个容器来放置程序:

<div class="dd" id="nestable2">
 <h4>Place programs below in order of preference</h4>
  <ol class="dd-list dd-placeholder">
   <li class="dd-item" data-id="17">
   <div class="dd-handle">Health Sciences Advisor</div>
  </li>
 </ol> 
</div>

原始列表和第二个容器的输出在 textareas 中作为序列化的 json 数据输出,如下所示:

 Serialised Output (per list)
 <textarea id="nestable-output"></textarea>
 <textarea id="nestable2-output" name="nestable2-output"></textarea>

同样没有真正的问题。 textareas 稍后将更改为隐藏输入。除了有时当我将列表项从左列拖动到右侧时,值不会更新。这是jquery.nestable.js的链接

下面是表单中的脚本:

$(document).ready(function()
{
var updateOutput = function(e)
{
    var list   = e.length ? e : $(e.target),
        output = list.data('output');
    if (window.JSON) {
        output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
    } else {
        output.val('JSON browser support required for this application.');
    }
};
// activate Nestable for list 1
$('#nestable').nestable({
    group: 1
})
.on('change', updateOutput);
// activate Nestable for list 2
$('#nestable2').nestable({
    group: 1
})
.on('change', updateOutput);
// output initial serialised data
$('#nestable2').attr('data-id', 'newvalue');
$('#nestable2').data('id', 'newvalue');

updateOutput($('#nestable').data('output', $('#nestable-output')));

updateOutput($('#nestable2').data('output', $('#nestable2-output')));

});

它只是有时会失败。然后,如果我在它更新的第二个容器中重新排序列表,但在我再次重新排序之前不会更新任何新项目。非常感谢任何帮助。

【问题讨论】:

  • No issue there. -> ,$item['program'], 如果我是你,我不会那么肯定。这些逗号肯定会导致一些问题。我保证!
  • 似乎没有。列表已填充,没有抛出错误。但我把它改成了这样: echo '
  • '.$item ['程序'].'
  • ';
  • 看看这个问题:Difference between “,” and “.” in PHP? 并感到惊讶。
  • 明白了。但这不是问题。我的列表已生成。问题似乎与 e.target 和最接近的列表有关。正如我所说,有时当我从一列拖到另一列时它确实有效。在原始列中重新排序也可以,但我不想提交 16 个程序,只提交他们选择的程序。
  • 我知道。我还没有赶上那部分。确保您的代码正确是找到问题的第一步。就是这样
  • 标签: javascript php jquery mysql


    【解决方案1】:

    我找到了答案。问题出在 jquery.nestable.js 文件中,第 475 行左右是这样的:

    $(this).data("nestable-id", new Date().getTime());
    

    在速度更快的机器上,似乎没有足够的时间(以毫秒为单位)让脚本看到发生了变化。替换为:

    $(this).data("nestable-id", Math.round(Math.random() * Math.pow(10, 16)));
    

    它似乎可以生成任何随机数,因此这个答案并不是唯一可以完成的方法。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签