Skip to content

Commit

Permalink
Add checkboxes to osom_table (#5)
Browse files Browse the repository at this point in the history
* Add checkboxes to osom_table
  • Loading branch information
rauxalach authored Aug 15, 2016
1 parent 71d2e57 commit 69ef3b3
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/assets/javascripts/osom-tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
order_table(this);
});

$(document).on('click', '.osom-table table th.mark > input[type="checkbox"]', mark_an_item_as_checked);

$(document).on('click', '.osom-table table td.mark > input[type="checkbox"]', function() {
return save_checkboxes($(this).closest('.osom-table'));
});

/* Load async tables */
$(document).ready(function() {
update_table_urls_from_query_string();
Expand Down Expand Up @@ -69,6 +75,16 @@
});
}

function mark_an_item_as_checked(){
$(this).closest('table').find('td.mark > input[type="checkbox"]').each((function(_this) {
return function(i, box) {
box.checked = _this.checked;
return true;
};
})(this));
return store_checked_ids_in_data_attribute($(this).closest('.osom-table'));
}

// Public Functions
// ==========================================================================

Expand All @@ -87,6 +103,7 @@
cache: false,
success: function(new_content) {
table_load_success(container, new_content, url)
load_checkbox_state_for_page(container);
},
complete: function() {
container.removeClass('loading');
Expand Down Expand Up @@ -143,6 +160,32 @@
}
}

function store_checked_ids_in_data_attribute(container) {
var checked_ids;
checked_ids = (container.data('checked') || '').split(',');
container.find('td.mark input[type="checkbox"]').each(function() {
var item_id;
item_id = $(this).data('item-id').toString();
if ($.inArray(item_id, checked_ids) === -1) {
if (this.checked) {
return checked_ids.push(item_id);
}
} else if (!this.checked) {
return checked_ids.splice($.inArray(item_id, checked_ids), 1);
}
});
return container.data('checked', checked_ids.join(',').replace(/^,/, ''));
};

function load_checkbox_state_for_page(container) {
var checked_ids;
checked_ids = (container.data('checked') || '').split(',');
return container.find('td.mark input[type="checkbox"]').each(function() {
this.checked = $.inArray($(this).data('item-id').toString(), checked_ids) !== -1;
return true;
});
};

// URL Functions
// ==========================================================================

Expand Down

0 comments on commit 69ef3b3

Please sign in to comment.