Select all rows in django tables2 with pagination - checkbox

How can I select all records of a django_tables2 table using a CheckBoxColumn when the table is split into several pages?
I have used this select all rows in django_tables2 code to select all records from one page and this How to get information from Django_tables2 row? to do something with them.
But how can I select all records across pagination limits?
And also keep the status of a checkbox when I go back and forth on the pages?

I would suggest to add a 'check all' checkbox to your template. Apart from visually checking all rows of the current page (using JavaScript), it being checked should signal the logic in your view that the user intends to check all rows.
Keeping the status of those checkboxes is another story. It can lead to bookkeeping in some very big lists. You could keep them in request.session if they need to be kept for a user in a particular browser session, if they need to be persisted to the database, you can create a model to keep track of the selection of a certain record for a certain user.

Ok, now I came up with this JavaScript/jQuery solution. It addresses two aspects. First, to store and display the selected checkboxes when the sort order of the table is changed, the table is filtered or when you naviagte to another page and return the table again. Secondly to check and store all checkboxes on a table page when the "Select All" button on in the table head is clicked. Here I'm stuck with selecting all rows in a table, not only the ones that are displayed on one table page (with pagination on). The selected checkboxes are stored in the session storage of the browser. There is certainly room for improvement in the functions, I don't like the iteration over all rows of the table a lot, but I couldn't find a better solution yet.
Still I think it would be nice to have this kind of funtionality available in django-tables2. Maybe in some coming version?
The checkbox column is called "selvars" in my tables.py
class dataset_varsTable(tables.Table):
selvars=tables.CheckBoxColumn(accessor='pk',attrs = { "th__input": {"onclick": "toggle(this)"}}, orderable=False)
... (other table columns)
It's the integer primary key of my model
class vars(models.Model):
idvar=models.IntegerField('Var.ID', primary_key=True)
...
And the JavaScript and jQuery functions in the HTML template with the table
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var storage_name="checkedvars"; // name of session storage
// function to store/remove checked varids in session storage
function update_checkedvars(varid, check_status){
var current_checked=[];
if (sessionStorage.getItem(storage_name)){
current_checked=sessionStorage.getItem(storage_name).split(",");
}
if (isNaN(parseInt(varid))==false && check_status==true){
current_checked.push(varid);
// store checked varid in session storage without duplicates
sessionStorage.setItem(storage_name, Array.from(new Set(current_checked)));
} else if (isNaN(parseInt(varid))==false && check_status==false){
// remove unchecked varid from session storage
sessionStorage.setItem(storage_name, current_checked.filter(e => e !== varid));
}
}
// toggle button
function toggle(source) {
checkboxes = document.getElementsByName('selvars');
for(var i in checkboxes){
checkboxes[i].checked = source.checked;
update_checkedvars(checkboxes[i].value, checkboxes[i].checked);
}
}
$(document).ready( function() {
// display checkboxes according to selected varids in session storage
var current_checked=[];
if (sessionStorage.getItem(storage_name)){
current_checked=sessionStorage.getItem(storage_name).split(",");
checkboxes = document.getElementsByName('selvars');
for(var i in checkboxes){
if(current_checked.includes(checkboxes[i].value)){
checkboxes[i].checked=true;
}
}
}
// save/remove checked/unchecked varid in session storage
$(".selvars").click( function(event) {
var varid=event.target.value
var check_status=event.target.checked
update_checkedvars(varid, check_status);
});
});
</script>

Related

Salesforce lightning:treegrid find Id/data of deselected row

I am working with a lightning:treegrid component.
The onrowselection attribute of lightning:treegrid invokes a method on js controller whenever a row is selected/deselected.
I am able to fetch the currently selected rows using the getSelectedRows() method.
But if I deselect a row, I am not able to find its Id or row data (which is deselected) in the js controller.
Aura:
<lightning:treeGrid columns="{!v.gridColumns}"
data="{!v.gridData}"
keyField="idRef"
aura:id="productTree"
expandedRows="{! v.gridExpandedRows }"
onrowselection="{! c.getSelectedRows}"
ontoggle = "{!c.handleToggle}"
selectedRows = "{!v.selectedIds}"
isLoading="{! v.isLoading }"
/>
JS:
getSelectedRows: function(cmp, event, helper) {
//get selected rows
var curRows = event.getParam('selectedRows');
//how to get the row that is deselected
}
Can anyone please help?
There is no standard way on lightning:treegrid component to obtain a list of deselected rows. There are many other limitations on this component which make it practically useless.
I followed these steps to obtain the deselected row:
Create an attribute that stores ids associated with all selected
rows. Let us name this oldSelectedRows.
Then obtain a list of all currently selected rows using
cmp.find("aura_id_of_treegrid").getSelectedRows(). Let us name it
selectedRows.
Find the difference between oldSelectedRows and selectedRows. This fetches you deselected item.

Angular select multiple not setting selected items

Scenario is quite simple. I have edit view which comprises of select element with multiple selection. Select element looks as follows
<select class="selectpicker" multiple
ng-model="UserBeingEdited.UberTypes"
ng-options="uberType.Id as uberType.Name for uberType in UberTypes"></select>
</div>
If I press edit for specific user then showEditModal function is called and edit pop up shows up. User has his own UberTypes objects selected so I pick theirs Ids and assign to UserBeingEdited.UberTypes.
$scope.UserBeingEdited = { UberTypes: []};
$scope.showEditModal = function (user) {
for (var i = 0; i < user.UberTypes.length; i++) {
$scope.UserBeingEdited.UberTypes.push(user.UberTypes[i].Id);
}
$("#editUserModal").modal("show");
};
Unfortunately they are not selected, no matter what. Suprisingly, if I set values to property UserBeingEdited by the time of declaring then it works, items are selected.
$scope.UserBeingEdited = { UberTypes: [1,2]}; //this works
It seems like angular does not refresh selected items within UI.
EDIT:
I have two items: Uber1 and Uber2. Both items are selected but view does not reflect it. If I open drop down menu and select Uber2 it shows Uber1 selected, apparently Uber2 was selected and another click was treated as unselection. The same happens when I click Uber1 first, Uber2 shows as selected.
As above picture depicts, Uber2 is highlighted since I have just clicked it but it shows Uber1 as selected. Both were selected in User's UberTypes but view just simply does not mark them as selected.
http://jsfiddle.net/upusq8ah/7/
I use bootstrap-select.js for select element and presumably this library causes such an error since without including it, it works fine.
Solution
After a while of struggle, I accidentally ran into below workaround. You need to call refresh on selectPicker and this has to be delayed otherwise it will bring no effect.
setTimeout(function () {
$('.selectpicker').selectpicker('refresh');
}, 100);
$("#editUserModal").modal("show");

How to filter the JSON data using AngularJS?

I have three dropdown boxes. I need to filter the data and need to be displayed in the table based on my checkbox selection(either with single checkbox or two checkboxes or three checkboxes).
I have done the following, but if we observe it clearly, I am not able to filter the data properly using AngularJS.
Like:
a. It should work for individual checkbox selection: means if I select any single checkbox either from Name or Description or Field4, then respective matched filtered data should be displayed in the table, otherwise it shouldn't be displayed any data(i.e if it doesn't match our checkbox selection means it won't display any data)
b. It should work for multiple(two) checkbox selection: means if I select any multiple checkboxes like either one from Name and one from Description or one from Description and one from Field4 or one from Field4 and one from Name, then respective matched filtered data should be displayed in the table, otherwise it shouldn't be displayed any data(i.e if it doesn't match our checkbox selection means it won't display any data)
c. It should work for multiple(three) checkbox selection: means if I select the three checkboxes like one from Name and one from Description and one from Field4, then respective matched filtered data should be displayed in the table, otherwise it shouldn't be displayed any data(i.e if it doesn't match our checkbox selection means it won't display any data)
It is working fine for the first time checkbox selection only, means: after loading the above code/app, if we check either any one of the above selections(like whether single checkbox selection or two checkbox selection or three checkbox selection) then it's working fine, later it is not working(means if we uncheck the above any criteria and then if we select any checkbox again then it's not working, for that again we need to refresh the app/code then only it's working).
Example: if I select one from Name, then respective matched data will be displayed. Then again if I uncheck the same and check the some other checkbox like from Description then it's not working. Similarly for all the above criteria. You can observe it clearly.
Please let me know that what I have done wrong here and let me know how to filter it properly. Created Fiddle. Thanks in advance !
The problem is the convoluted filtering logic. Anytime you find yourself nesting lots of if statements, think about reorganizing the branching logic. By breaking it into smaller components, you can make it easier to manage, and if you can avoid using if altogether, then you only have to test one path, instead of several.
Every time a user checks a box, we need to make sure that we only display items that match however many boxes are checked. So we need to know 1) how many boxes are checked, n, and 2) how many items can be found with n matching fields.
We can take advantage of the fact that Boolean variables can be cast to integers (0 = false, true = 1) and use that to count the number of checked boxes, as well as the number of matches found.
We can also put the field names into an array, so that we don't have a lot of repetition in our functions.
Fiddle example here.
var fields = ["name", "description", "field4"];
//for each field, count how many fields the item matches
function getMatches(item, matchesNeeded) {
var foundMatches = 0;
fields.forEach(field => {
foundMatches += item[field] === $scope.pagedItems[field]
});
//make sure found at least one match and found desired minimum
return foundMatches && foundMatches >= matchesNeeded;
}
//count how many boxes are checked
//this will tell us how many different fields we are matching on
function numChecked() {
var count = 0;
fields.forEach(field => {
//this will auto convert falsy to 0.
//truthy values will be 1
count += Boolean($scope.pagedItems[field]);
});
return count;
}
$scope.filterItems = function(item) {
return getMatches(item, numChecked());
};
As #Larry pointed it was more based on logic. I have modified Apress book 'Pro AngularJS' Source Code from GIT for this.
Basic logic will in filter function as below -
$scope.categoryFilterFn = function (product) {
var canHave = false;//dont load by default
var atLeastOneTrue = false;//Check if previously checked any condition
angular.forEach(filterValues, function(selectedValue, key) {
var selectVals = Object.values(selectedValue);
if(selectVals.indexOf(product[key]) !== -1) {//if values exits in product.
canHave = !atLeastOneTrue ? true : canHave;
}else{
canHave = false;
}
atLeastOneTrue = true;
});
return canHave;
}
For working Fiddle.

web2py - Dropdown menu

I'm trying to have a dropdown menu for the user to select the database table. I have defined few tables in db.py and I want the user the to select a particular table from a dropdown menu and insert entries. Right now I use SQLFORM:
def index():
form=SQLFORM(db.selectedtable) #want to change the table name#
if form.process().accepted:
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill out the form'
return dict(form=form)
I need the user to select 'selectedtable' value from a dropdown list that shows all the available tables in the DB. I do not necessarily want to retrieve the table values from DB. I am OK with defining a list with the available tables and the dropdown menu can pull the table names from that list.
So far I only found IS_IN_DB to automatically create a dropdown and PluginDropdown() but that does not serve my purpose. If soemebody can direct me to the proper way of handling this task I'd be really thankful.
Regards.
Update:
After Anthony's suggession I tried the following with , as I'm not that familiar with JS.
{{extend 'layout.html'}}
{{select='NONE'}}
<form>
<select>
{{for item in TOOLS:}}
<option value="{{select=item}}">{{=item}}</option>{{pass}}
</select>
<input type="submit" value="Go!"/>
</form>
<h2>Input form</h2>
{{=form}}
<h2>{{=select}}</h2>
As you might see this doesn't work properly. What I tried to do is to get the user chose value to 'select' variable. But it doesn't work. It always gets the last element in ITEMS (this list is defined in db.py). My next option would be to be call another controller function, passing the user selected value as an argument. Then it can prepare the form with the passed value and send to a view to display
<h2>Input form</h2>
{{=form}}
But I'm not sure how I can assign the user chosen value to an argument and then call another controller function with that arugument value.
If you have any suggestion how I can modify this to get the user chosen value thats very much appreciated. Thank you.
You could create a <select> element listing all the tables, and then load the form associated with the selected table as a web2py component via Ajax. In the view of the main page (e.g., /views/default/index.html):
<script>
jQuery(function() {
jQuery('#table').change(function() {
web2py_component("{{=URL('default', 'form.load')}}" + "/" +
jQuery(this).val(), target='form')
})
})
</script>
{{=SELECT('Select a table', *db.tables, _id='table')}}
<div id="form"></div>
And in a controller (e.g., default.py):
def form():
if request.args(0) in db.tables:
response.generic_patterns = ['load']
return dict(form=SQLFORM(db[request.args(0)]).process())
else:
raise HTTP(404)
Note, db.tables is a list of all the tables defined on the db connection object -- it is used in the SELECT() helper in the view to generate a <select> list of all the tables. The script in the view registers a jQuery event handler that fires whenever a different table is selected from the dropdown. The handler calls the web2py_component() function (which is in /static/js/web2py.js), which loads the form component via Ajax into the div with id="form". It appends the value of the selected table to the URL.
In the controller, the form() function checks for the db table name in request.args(0). It then sets response.generic_patterns so the "generic.load" view will be allowed (by default, generic views are only enabled for local requests). Alternatively, you could define your own "form.load" view, or even use a different extension (e.g., "form.html").
Because the form is loaded as a web2py Ajax component, the form submission will be trapped and submitted via Ajax as well, so it will not result in a full page reload.

SWT/JFace: disabled checkbox in a table column

It is quiet easy to create a Table with a checkbox column, e.g., using the SWT.CHECK flag. But how to make checkboxes in certain table rows not editable while those in other rows remain editable?
I don't know a simple way of doing that.
But I see two possible solutions: There is a JFace Snippet doing a rather extreme hack to emulate natively looking checkboxes in tables with images here.
And then you could put own checkboxes into a plain Table, like this. That way you can control the state of every checkbox on your own.
I'd go with the 2nd solution.
try this:
table.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if( event.detail == SWT.CHECK ) {
event.detail = SWT.NONE;
event.type = SWT.None;
event.doIt = false;
((TableItem)event.item).setChecked(false);
}
}
});
I had faced a similar problem and i was able to solve it in the table using SWT.check.
In the widgetSelected Event of the table you can try the following code:
TableItem[] item = table.getItems();
for(int j=0; j<item.length;j++)
{
TableItem tblItem = item[j];
if (tblItem.getChecked())
{
table.setSelection(j);
if(codition for the checkbox to be non-Editable written here)
{
item[table.getSelectionIndex()].setChecked(false);
}
}
}
In the above code after the table has been filled and when the user tries to check any item in the table the above code should be called. When the checkbox is clicked if the condition meets for the checkBox to be non-editable the checkbox is not selected otherwise it is selected.In this way in a table certain rows can be editable while others will be non-editable according to the required conditon.

Resources