我正在 drupal 中开发一个项目,该项目允许我拥有可视化书签屏幕截图(想想zootool.com)。我将每个屏幕截图放在一个使用 jQuery 进行排序的网格中。这一切都工作正常,我可以将所有图像按我希望的顺序排序。唯一的问题是我无法保存刚刚更改的位置/订单。
我使用“权重”模块为每个屏幕截图分配默认值,这是使用按此“权重”排序的视图输出的。因此,当我使用 jQuery 移动节点时,我需要一种机制来更新/保存所有节点的权重顺序。
目前,我有 jQuery 代码,它输出带有新订单的警报框:
//Draggable boxes
$(function() {
$( ".sortable-nodes" ).sortable({
placeholder: "ui-state-highlight",
opacity: 0.7,
update: function(event, ui) {
var result = $(this).sortable('toArray');
alert(result);
}
});
});
我的问题是:Drupal 是否已经有一个页面可以用来通过 jQuery.post 更新节点,或者我必须制作自己的一个页面来保存节点?另外,这是正确的方法还是有更好的方法在移动可排序框后更新/保存节点位置?
非常感谢!
I'm working on a project in drupal that allows me to have visual bookmark screenshots (think zootool.com). I have each screenshot in a grid that is using jQuery to be sortable. This all works correctly and I can sort all the images into whatever order I wish. The only problem is that I can't save the locations/order that I've just changed them to.
I have used the 'weight' module to assign a default value to each screenshot and this is output using Views sorted by this 'weight'. Therefore I need to have a mechanism to update/save the weight order of all the nodes when I move them with jQuery.
Currently I have the jQuery code that outputs an alert box with the new order:
//Draggable boxes
$(function() {
$( ".sortable-nodes" ).sortable({
placeholder: "ui-state-highlight",
opacity: 0.7,
update: function(event, ui) {
var result = $(this).sortable('toArray');
alert(result);
}
});
});
My question is this: Does Drupal already have a page I can use to update the nodes through jQuery.post or do I have to make my own one that will save the nodes? Also, is this the right way to go or is there a better way to update/save the node positions once the sortable box has been moved?
Many Thanks!
发布评论
评论(2)
好吧,我想我应该用我找到的解决方案来跟进我的问题。它可能会帮助其他人。它很大程度上基于 googletorp 对此处发布的非常相似的问题的回答: Drupal 6/jQuery Ajax 更新字段。
步骤1.0 创建一个新模块。
我创建了一个简单的 updatemynode.info 文件和 updatemynode.module 文件。
1.1 updatemynode/updatemynode.module包含两个函数。
这是模块中创建 URL www.mywebsite.com/update_positions 的第一个函数,该函数调用 php 函数“update_node_position”。 (见下文)
1.2 updatemynode/updatemynode.module 第二个功能是:
为了简洁起见,我删除了任何错误检查,它只是完成了一个功能,仅此而已。基本上,它执行以下操作: A. 搜索 $_POST['theorder'] 变量(其中包含我的应用程序中的 NID 字符串),使用 php 'explode' 将字符串转换为数组并分配给 $neworder。 B. 对于新数组的每个条目,使用值 (NID) 加载该节点。 C. 我正在使用“重量”模块来创建排序。 $node->node_weight = ($key+1); line 将数组 1、2、3、4 等中的位置分配给节点权重。 (从而创建订单)。 D. 保存节点并对数组中的每个条目重复此操作。
2.0 我有一个 main_javascript.js 文件附加到我的所有 drupal 页面。这个 javascript 文件包含以下代码:
这是 jQuery 的关键,它做了一些工作:
2.1 标记为 .sortable-nodes 的对象(在我的例子中是一个 DIV)使用 jQuery 的“sortable”方法进行排序。当我单击并拖动 DIV 时,它将执行以下操作:
2.2 在每个可排序 DIV 之间使用 CSS 类“ui-state-highlight”。
2.3 将不透明度降低到70%。
2.4 一旦位置发生更改,它将运行“更新”事件。
2.5 'update' 事件将创建一个名为 'result' 的变量,其中包含所有可排序对象节点 id 的字符串。 (例如 113,114,115,116,117 等...)
2.6 .ajax 方法运行并被告知将 POST 请求发送到 /update_positions URL(之前在 updatemynode/updatemynode.module 文件中创建)并传递名为 ' 的 $_POST 变量theorder' 包含字符串 'result'。一旦字符串“result”被传递到 URL,模块函数 update_node_position() 就会使用这个字符串并发挥其魔力,如上所述。
3.0 我在页面上的结果是使用视图按节点权重排序的。因此,当页面重新加载时,顺序应该与您使用 AJAX 调用订购的方式保持相同(因为 node_weight 已更新)。
希望这可能对某人有帮助。欢迎任何问题/意见!
Ok, I thought I'd follow up my question with the solution I've found. It may help others. It's based heavily on the answer that googletorp gave to the very similar question posted here: Drupal 6/jQuery Ajax update a field.
Step 1.0 Create a new module.
I created a simple updatemynode.info file and updatemynode.module file.
1.1 updatemynode/updatemynode.module contains two functions.
This is the first function in the module that creates a URL www.mywebsite.com/update_positions which calls the php function 'update_node_position'. (see below)
1.2 updatemynode/updatemynode.module second function is:
For brevity and simplicity I have removed any error checking to it just completes a function and that's it. Basically, it does the following: A. Search for the $_POST['theorder'] variable (which contains a string of NID's in my app), use php 'explode' to convert the string to an array and assign to $neworder. B. For each entry of new array, use the value (NID) to load that Node. C. I'm using the 'Weight' module to create ordering. The $node->node_weight = ($key+1); line assigns the position in the array 1,2,3,4,etc.. to the node wight. (Thus creating an order). D. Save the node and repeat for each entry in array.
2.0 I have a main_javascript.js file that is attached to all of my drupal pages. This javascript file contains the following code:
This is the crux of the jQuery and it does a few bits:
2.1 The object (which is a DIV in my case) labelled .sortable-nodes is made sortable using the jQuery 'sortable' method. When I click and drag the DIV it will do this:
2.2 Use the CSS class 'ui-state-highlight' in between each sortable DIV.
2.3 Reduce the opacity to 70%.
2.4 Once the position has been changed then it will run the 'update' event.
2.5 The 'update' event will create a variable called 'result' that contains a string of all the sortable object node id's . (eg. 113,114,115,116,117,etc... )
2.6 The .ajax method is run and is told to send a POST request to the /update_positions URL (created earlier in the updatemynode/updatemynode.module file) and pass the $_POST variable called 'theorder' which contains the string 'result'. Once the string 'result' is passed to the URL then the module function update_node_position() uses this string and does its magic as explained above.
3.0 My results on the page are ordered by the node weight using views. So, when the page is reloaded the order should stay the same as how you ordered it (because the node_weight has been updated) using the AJAX call.
Hope that maybe helps someone. Any questions/comments welcome!
Drupal 还没有这种功能,除非您使用服务模块
jsonp
的设置时间比您自己实现该功能要花费更长的时间。在我看来,没有更好的方法来做到这一点,您可以找到的任何贡献模块都可能非常臃肿,包含许多您不需要的功能。向自定义菜单项执行发布操作非常简单,我建议您这样做。然后您还可以完全控制代码,并可以根据需要对其进行优化。
Drupal doesn't already have this sort of functionality, unless you'd count using the Services module over
jsonp
which would take longer to setup than implementing the functionality yourself would.In my opinion there isn't any better way to do this, any contributed module you could find to do it would likely be very bloated with lots of functionality you don't need. It's so easy to perform a post to a custom menu item that I'd recommend just doing that. You'll also have full control over the code then and can optimise it as you wish.