简体   繁体   English

将有序列表提交到服务器

[英]Submitting an ordered list to a server

To teach myself Javascript, I'm trying to make a web page that gives users a list of items (eg foods), asks them to sort these foods from favorite to least favorite, and submit the data when they're done. 为了自学Javascript,我正在尝试制作一个向用户提供项目(例如食物)列表的网页,要求他们将这些食物从最喜欢到最不喜欢排序,并在完成后提交数据。 Using jQuery sortables seems like a good way to do this. 使用jQuery sortables似乎是一种很好的方法。 However, I'm not sure how the data submission should happen. 但是,我不确定如何提交数据。

Here's what I'm thinking. 这就是我的想法。 Each of these food items would be in a div like this: 这些食物中的每一个都将在这样的div中:

<div id="sortable">
    <div id="1" class="foods">Pizza</div>
    <div id="2" class="foods">Sushi</div>
    <div id="3" class="foods">Taco</div>
</div>

When the user clicks a "submit" button, I want the order of these items to be determined, and for this ordering to be sent back to the server (by the way, I'm using Django on the server side). 当用户单击“提交”按钮时,我希望确定这些项目的顺序,并将此顺序发送回服务器(顺便说一句,我在服务器端使用Django)。 It seems I can determine the order of the items with a function like this: 看来我可以使用以下功能确定项目的顺序:

function getOrder()
{
    var foods = $(".foods");
    var ids = [];

    for(var x=0; x<foods.length; x++)
    {
        ids.push(foods[x].id);
    }
    return ids;
}

However, I'm stuck on a couple of things: 但是,我在以下几点上遇到了麻烦:

  • Where in my code would I call this function? 我在代码中的哪个位置调用该函数? I'm thinking it would be an onclick action when the user presses the submit button, but I'm not sure where the data the function returns would get passed. 我想当用户按下“提交”按钮时这将是一个onclick动作,但是我不确定函数返回的数据将传递到哪里。
  • What format would be the most appropriate for sending this ordering to the server (eg JSON)? 哪种格式最适合将此订单发送到服务器(例如JSON)?

(I know this is a really basic question, but I have never made a web page with JavaScript before, so this area of programming is all new to me.) (我知道这是一个非常基本的问题,但是我以前从未使用JavaScript制作过网页,因此这部分编程对我来说是全新的。)

A more semantically relevant way of doing a list is by using an actual <ul> element. 列表在语义上更相关的方式是使用实际的<ul>元素。

So if you had something like this: 因此,如果您有这样的事情:

<ul id='foods'>
<li id='food_1'>Pizza</li>
<li id='food_2'>Sushi</li>
<li id='food_3'>Taco</li>
</ul>
<a href="javascript:saveFoods();">Save Order</a>

The following jQuery code would be appropiate: 以下jQuery代码将是适当的:

function saveFoods(id) {
    var data = $("#foods").sortable('serialize');
    var action = "/django/view/";
    $.post(action, data, function(json, status) {
        if(status == 'success' && json.status == 'success') {
            alert('Saved order of the foods!');
        } else {
            alert('Unable to complete the request.');
        }
    }, "json");
}

According to the jQuery docs on sortable , when you use serialize the elements of a sortable it requires their IDs to be in a setname_number format. 根据sortable的jQuery文档,当您使用序列化sortable的元素时,它要求其ID为setname_number格式。 So by having your list as food_1 , food_2 , etc. jQuery recognizes that the ID of Pizza is 1 and its set is food. 因此,通过将您的列表显示为food_1food_2等,jQuery识别出Pizza的ID为1,且其集合为food。 The data variable in saveFoods will then contain something like food[]=1&food[]=2&food[]=3 that you can process in your Django app. 然后, saveFoodsdata变量将包含诸如food[]=1&food[]=2&food[]=3 ,您可以在Django应用中对其进行处理。

Yes, the norm is some form of user action, so a button click is a good choice. 是的,规范是某种形式的用户操作,因此单击按钮是一个不错的选择。 You will write a routine that calls the ordering routine and sends it to the server. 您将编写一个例程,该例程调用订购例程并将其发送到服务器。

JSON is a good option, as it is lightweight. JSON是一个不错的选择,因为它是轻量级的。 If you want to play a bit more, you can head to XML, but I see very little reason to do this other than to learn, as XML adds unnecessary weight in this instance. 如果您想多玩点游戏,可以使用XML,但是除了学习之外,我几乎没有其他理由这样做,因为XML在这种情况下会增加不必要的负担。

Scriptaculous库提供可排序的列表,并提供可以发布回服务器的排序索引。

It'd probably be easier to put hidden fields inside the list of items. 将隐藏的字段放在项目列表中可能会更容易。 When the form is submitted, the same order will be send to the server in the post or get. 提交表单后,相同的订单将以邮寄或获取的方式发送到服务器。

Example: 例:

<div id="sortable">
    <div id="1" class="foods"><input type="hidden" name="sortable[]" value="1" />Pizza</div>
    <div id="2" class="foods"><input type="hidden" name="sortable[]" value="2" />Sushi</div>
    <div id="3" class="foods"><input type="hidden" name="sortable[]" value="3" />Taco</div>
</div>

The post will then have an array in it, like: 然后,该帖子中将包含一个数组,例如:

array(
    0 => 1,
    1 => 3,
    2 => 2
)

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

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