public
Description: A relational database using <table> tags and jQuery
Homepage:
Clone URL: git://github.com/nkallen/jquery-database.git
jquery-database / jquery.db.js
100644 50 lines (50 sloc) 1.352 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
(function($) {
  $.extend(jQuery.expr[":"], {
    eq : function(a, i, m) {
      if (!isNaN(Number(m[3]))) {
        return jQuery(a).text() == m[3];
      } else
        return jQuery(a).text() == jQuery(a).siblings(m[3]).text();
    }
  });
  $.fn.where = function(condition) {
    return this.find('tr:has(' + condition + ')').filter('tr');
  };
  $.fn.and = function(condition) {
    return this.filter(':has(' + condition + ')');
  };
  $.fn.select = function(attributes) {
    return this
      .map(function() {
        return $(this).find(attributes)
          .map(function() { return $(this).text() });
      });
  };
  $.fn.join = function(other) {
    var left = this, right = $(other);
    var table = $('<table>')
    this
      .find('tr').each(function() {
        var left_row = $(this);
        $(other).find('tr').each(function() {
          $(this)
            .clone()
              .find('td')
              .each(function() {
                $(this).addClass(right.attr('class'));
              })
              .end()
            .append(
              left_row
                .clone()
                .find('td')
                .each(function() {
                  $(this).addClass(left.attr('class'));
                })
            )
            .appendTo(table);
        });
      });
    return table;
  };
})(jQuery);