English 中文(简体)
职业经历
原标题:Sorting Div s With PrototypeJS

我正在寻求某种帮助,把四分五裂与PrototypeJS混为一谈。 这就是我迄今为止所做的事情:

document.observe( dom:loaded ,function(){
    $$( .sortcol ).invoke( observe ,  click , function() { 
        if (this.hasClassName( desc )) {
            var desc = false;
            this.removeClassName( desc );
        } else {
            var desc = true;
            this.addClassName( desc );
        }
        var colname = this.className;
        var contentid = this.up(2).id;
        sortColumn(contentid,colname,desc);
    });
});

function sortColumn(contentid,colname,desc) {
    $$( # +contentid).select( . +colname).sort(function(a,b){ 
        if (desc) {
            return (a.text.toLowerCase() >= b.text.toLowerCase() ) ? -1 : 1; 
        } else {
            return (a.text.toLowerCase() < b.text.toLowerCase() ) ? -1 : 1; 
        }
    });
} 

实例数据:

<div id="contentbox_Users" class="userList">
    <div class="userListHeader">
        <div class="userListHeaderCell col1">First Name</div>
        <div class="userListHeaderCell col2">Last Name</div>
    </div>
    <div id="contentbox_People">
        <div class="userListRow">
            <div class="userListCell col1">John</div>
            <div class="userListCell col2">Smith</div>
        </div>
        <div class="userListRow">
            <div class="userListCell col1">Bob</div>
            <div class="userListCell col2">Ray</div>
        </div>
        <div class="userListRow">
            <div class="userListCell col1">Fred</div>
            <div class="userListCell col2">Jones</div>
        </div>
    </div>
</div>

在被点击时,基本上没有任何“col”类,我想用被点击的栏目加以分类。 第一个问题是,在有多个班级时,我必须能够正确掌握班级名称。 这些班级均如col1、col2等。 我如何看待正确的类别?

第二件是改变分类法,以便统一一栏数据(每栏数据由另一栏目汇总),并得出结果,取代现有数据。

This needs to be done in prototypejs and I can t change the code to tables.

事先得到帮助。

最佳回答

在您提问的第一部分,如果一栏名称本身归属于rel>>:data-*,则会变得容易得多,但你说不能改变超文本。 有可能从最喜欢的阶层中挑选......

var colname = this.className.match(/cold+/).first()

但是,如果我们假设每行各列一栏,就没有必要这样做。 如果使用表格,这将是一个更安全的假设。

var colnumber = this.up().childElements().indexOf(this);

贵国问题的第二部分是轻松的,即rows而不是cells

页: 1 仅凭任何append <>>> 或insert<>> /em>,其中一项内容的行动首先要将其从母体中删除,因此,只是再次向他们致敬,并且他们要服从正确的命令。 无需更换,我看到一些图书馆令人信服地将这些内容转换为超文本,从而使之重新插入!

The following has been test

document.observe( dom:loaded ,function() {
    $$( .userListHeaderCell ).invoke( observe ,  click , function() {
        this.toggleClassName( desc );
        var colnumber = this.up().childElements().indexOf(this);
        var content = this.up(2); // use the element directly instead of it s ID
        sortColumn(content, colnumber, this.hasClassName( desc ));
    });
});

function sortColumn(content, colnumber, desc) {
    content.select( .userListRow ).sort(function(a,b){
        var atext = a.down(colnumber).innerHTML.stripTags().toLowerCase();
        var btext = b.down(colnumber).innerHTML.stripTags().toLowerCase();
        return atext.localeCompare(btext) * (desc ? -1 : 1);
    }).each(Element.prototype.appendChild, content);
}
问题回答

在我看来,这似乎与你正在制作表格数据一样。 因此,为什么不使用表格? 一旦你使用一张表格,就有许多字面。 http://www.millstream.com.au/upload/code/tablekit/“rel=“nofollow”>。





相关问题
Javascript drop down menu widget

I have a menu consisting of an <ul> in a Web CMS. I want several menu items to have sub-items that are displayed in a dropdown list. These sub-items are <ul>s as well. This is basically ...

highlight div or p after loading

Hello i need to highlight a div after my page is loading, i don t know if i should use a partial or something like that, the partial also has to have 2 variables disponible. Code to highlight after ...

Turning a DIV into a click-and-drag viewport

Does somebody know an unobtrusive, Prototype or no framework based way to turn a DIV with big content (e.g. a map) into a clickable and draggable "map" container with fixed dimensions, very much like ...

bug when prototype and jQuery are both loaded

I have function in Javascript which works fine using prototype. The function is used to dynamicaly change a select field based on the selection of another field. var sizes_286 = new Array(); ...

beginner javascript question

This should be easy, but since I m new to programming, specially in javascript, I cannot figure it out. Let s say I have the following javascript code: var InjectClientValue = Class.create(); ...

How to use Scriptaculous effects on error messages

I want to use some Scriptaculous effects on error messages. <% form_for(@page) do |f| %> <%= f.label :name %> <%= f.text_field :name %> <%= f.error_message_on "name" ...

Scrolling to the bottom of a div

As part of an ajax chat room (using prototype framework) I am working on I scroll the user to the bottom each time a message is added using the below js. However if the user has scrolled up and the ...

Compare text with innerHTML IE7 problem

I can t find a work around for the innerHTML bug in IE7. I need to look at the contents of dynamicly generated HTML and change it if the text is "-1". I m using the prototype js gallery but couldn t ...