简体   繁体   English

从ajax调用到jQuery UI可排序列表构建html

[英]Building html from an ajax call to jquery UIs sortable list

Im having a design problem in HTML/JavaScript. 我在HTML / JavaScript中遇到设计问题。

I appended jquery UIs sortable to my web-application: Heres a demo on sortable (cant show my application now): http://jqueryui.com/demos/sortable/default.html 我将可排序的jQuery UI附加到了我的Web应用程序:这是一个可排序的演示(现在不能显示我的应用程序): http : //jqueryui.com/demos/sortable/default.html

Now im populating that drag and drop list in JavaScript with data from an ajax call. 现在,使用来自ajax调用的数据在JavaScript中填充该拖放列表。 The list is changed by users all the time. 该列表始​​终由用户更改。

I try do something like this: 我尝试做这样的事情:

Var htmlData = '<div id=wrapper>'
             +'<div>'
             +data.title
             +'</div>'
             +'<div>'
             +data.description
             +'</div>';

${"#sortable-list"}.html(htmlData);

And so on. 等等。 Some of the divs also have attributes set in variables like 'id="' + data.id + '"' I then try to fit this string htmldata in the sortable-list. 一些div也有在变量中设置的属性,例如'id="' + data.id + '"'然后,我尝试将此字符串htmldata放入sortable-list中。 But it's getting messy pretty quick. 但是很快变得混乱了。 I tried to fit <tables> in it, and <p> with <span> s in. But it's still hard to get the design that I want. 我试图在其中放入<tables><p>加入<span> 。但是,仍然很难获得我想要的设计。

Cant post images due to lack of reputation but here's the design i want (this is just one <li> in the <ul> ): 由于缺乏声誉而无法张贴图片,但这是我想要的设计(这只是<li>中的一个<li> <ul> ):

http://img546.imageshack.us/img546/9179/48361880.gif http://img546.imageshack.us/img546/9179/48361880.gif http://img546.imageshack.us/img546/9179/48361880.gif http://img546.imageshack.us/img546/9179/48361880.gif

So how would you do this? 那么,您将如何做呢? I've been reading about templates like mustache but it don't seems to help me. 我一直在阅读有关胡须之类的模板的信息,但似乎对我没有帮助。 And the way I building the table with a string can't be the best way. 而且我用字符串构建表的方法不是最好的方法。

Any example or info on how to do this is much appreciated 任何有关如何执行此操作的示例或信息都非常感谢

You could do something like this. 你可以做这样的事情。

var items[]

items.push($('<div />', { html: data.title }));
items.push($('<div />', { html: data.description}));

$("#sortable-list").html($('<div />', {
     'id' : 'wrapper',
     'class' : 'yourclass',
     html: items.join('')
  })
);

the items.push you can use in a loop to cycle all data and push items into the array. 您可以在循环中使用items.push循环所有数据并将项目推入数组。 Later join the array items into the html 稍后将数组项加入html

There are many advantages of client side tempting but the main one is that you don't have to mess with stringing HTML together in JavaScript. 客户端诱使有很多优点,但是主要的优点是您不必在JavaScript中将HTML串在一起。 Doing so is not only error prone but it also quickly becomes a maintenance nightmare. 这样做不仅容易出错,而且很快就会成为维护的噩梦。

For example here's how you could tackle your requirements with Underscore.js tempting. 例如,这是通过Underscore.js诱人的方法来满足您的要求的方法。

As you can see the HTML is clearly laid out and will be easy to alter as your requirements change. 如您所见,HTML布局清晰,随着需求的变化将易于更改。

<script type="text/template" id="sortable-entry">
    <% _.each(items, function(item) { %>
        <div>Title: <%= item.title %></div>
        <div>Description: <%= item.description %></div>
        <hr />
    <% }); %>
</script>

<ul id="sortable-list"></ul>​

<script>
    var data = {
        items: [
        { title: 'title1', description: 'description1' },
        { title: 'title2', description: 'description2' },
        { title: 'title3', description: 'description3' },
        { title: 'title4', description: 'description4' },
        { title: 'title5', description: 'description5' }
        ]
    };

    var event_html = _.template($('#sortable-entry').html());
    $(event_html(data)).appendTo($('#sortable-list'));
</script>

Here's a live example of this - http://jsfiddle.net/tj_vantoll/kmXUr/ . 这是一个实时示例-http://jsfiddle.net/tj_vantoll/kmXUr/

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

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