TableSorter jQuery Plugin - Enable Select Element in Disabled Header Column - tablesorter

I am using the TableSorter jQuery plugin. I'm trying to place a select element as one of my column headers. I've disabled that particular column for sorting by doing this:
$('#rankings').tablesorter(headers: {5: {sorter:false}});
But when I click on the select element in the column header the options don't populate. It's as if there is something "blocking" me from clicking on it.
Is there something in the TableSorter code that will disable clicking on the element if it is disabled?
I was even experimenting on their example page, and you can't even seem to highlight the text in the column headers for those column headers that are disabled.
Is there any way to place a select in a disabled column header using TableSorter? Thanks for your help!

There is an undocumented option cancelSelection which can be set to false (missing docs post), then you'll need to use the onRenderHeader to disable selection on the other headers (if you want) (demo):
$('table').tablesorter({
// make header text selectable
cancelSelection: false,
headers: {
0: {
sorter: false
}
},
// prevent text selections (optional)
onRenderHeader: function (index) {
if (index > 0) {
$(this)
.addClass('no-select')
.attr('unselectable', 'on')
.bind('selectstart', false);
}
}
});

Related

How to auto-select single search returns with select2

The UI designer wants the select2 live search to complete when the 'matcher' function reduces the option set to 1 value, w/o having to press the enter key etc.
Is there a select2 setting for this?
You need to catch "results:all" event and trigger "result:select" - it will select currently highlighted item.
var select2 = new Select2({});
select2.on("results:all", function (params) {
if (params.data.results.length === 1) {
select2.trigger('results:select');
}
});

jquery tablesorter: sorting filter select field that is using extracted text

I'm using the JQuery tablesorter plugin and I"m trying to sort a filter select list (alphabetically).
The column has full names of people with their titles and I only want to sort by last name. In addition I want the select field to filter those names and display the full name alphabetically in the drop down.
So far, I have the data parsed by textExtraction and the sort works. I set filter_useParsedData to true, but it is only showing the extracted text (last name) in the select field.
$('table').tablesorter({
theme: 'blue',
textExtraction: {
0: function(node, table, cellIndex){ return $(node).find("lname").text(); }},
widgets: ['columns', 'filter', 'stickyHeaders', 'zebra'],
widgetOptions : {
filter_useParsedData : true, }
});
Here is a demo of the table http://jsfiddle.net/UwKge/.
Anyway this can work?

Select2 doesn't show selected value

Select2 loads all items from my list successful, the issue I found when try to select a specific value when page loads. Example:
:: put select2 in a specific html element, no value is selected even all items are loaded.
$('#my_id').select2();
:: When the page is loaded I'm trying to show a specific item selected, but doesn't work as expected, because even selected, the select2 doesn't show it.
$('#my_id').val('3'); //select the right option, but doesn't render it on page loads.
How to make a selected option to pop up when pages loads?
Thanks in advance.
#UPDATED
:: How I load all select2 items (sorry, its jade, not pure HTML):
label(for='category') Category
span.required *
select(id='category', style='width:230px', name='category')
option(value='') - Select -
each cat in categories
option(value='#{cat.id}') #{cat.description}
P.S.: All items from my list are loaded.
:: How I initialize the select2:
Just put the following line code on my javascript and it does successful:
$('#category').select2();
:: How I'm trying to select a specific value:
First attempt:
$('#category').select2(
{
initSelection: function(element, callback) {
callback($('#field-category').val());
}
}
);
Second attempt:
$('#category').val($('#field-category').val());
P.S.: #field-category has a value its a hidden input field and works OK.
You need to use the initSelection option to set the initial value.
If you are using a pre-defined select element to create the select2, you can use the following method
$('select').select2().select2('val','3')
Demo: Fiddle
add a trigger change after setting val:
$('#my_id').val('3').trigger('change');
A very simple way to tackle this problem is :
//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
//Step2: Now reinitialize your select box
//This will work, if you haven't initialized selectbox
$('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();
This may help:
$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed
You can find more on details here:
Thanks
Per here initSelection is deprecated in Select2 4.0 and later.
Using Select2 4.0.0 this worked for me:
$('#my_id').select2({val:3});
HT: #Kokizzu
Here is how to make val in select2 just select the corresponding element.
For some reason, select2 doesn't provide the function to look up selections by id.
init:
$("#thing").select2({data:sources, initSelection: function(item, callback) {
// despite select2 having already read the whole sources list when you
// do .val(n) you have to explicitly tell it how to find that item again.
var to_be_selected = null;
$.each(sources, function(index, thing) {
if (thing.id == item.val()) {
to_be_selected = thing;
return;
}
})
callback(to_be_selected);
}})
normal code
// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -
$(".select").select2({
data: data_names
});
data_names.forEach(function(name) {
if (name.selected) {
$(".select").select2('val', name.id);
}
});
I had a multiple select2 box with multiple selections.
Step 1 was to put my string of school_ids into an array of integers corresponding to the id of each school, and remove a leading zero. I had to do this in a separate script tag.
<script>
var school_ids = <%= raw JSON.parse(#search.school_ids).map{|x| x.to_i} - [0]%>
</script>
Step two was to create the select box, then set the values, then trigger like so:
<script>
$(document).ready(function() {
$('.select2-multiple').select2({
placeholder: "Hit Enter After Selection",
width: 'resolve'
});
$('.select2-multiple').val(school_ids);
$('.select2-multiple').trigger('change');
</script>
trigger just select2 to set the value
$('#my_id').val('3').trigger("change.select2");
and this will trigger select2 with dropdown
$('#my_id').val('3').trigger("change");
I meet with the same problem, this works for me:
Using Select2 > 4.0.0
$('#select_id').val('3').trigger('change');
var option=$(this);
if(option.val()==data.StudentCourses.CourseId)
{
option.setAttribute('Selected');
}
});
i have initilized select2 because i just want few options selected from them.
If you are using select2 v4.0.0 or above, you can check select2 documentation
// Initialize select2 for all select tags
$('select').select2();
// Initialize select2 for a specific id
$('#my-id').select2();
// Initialize select2 for a class
$('.my-class').select2();
// Update the displayed value after changing the selected option.
$('#my-id').trigger('change');
$('.classname').each(function() {
$(this).siblings().find('.select2selection__rendered').text($(this).text())
})
in my case I were dealing with a class then I used this way to set showing content at select2
<option selected value="your_value">your_text</option>
u need to put this at each select box's first option

Using sortable with Tag-it jqueryui

I've been using the tag-it plugin from https://github.com/aehlke/tag-it (demo - http://aehlke.github.com/tag-it/examples.html).
In this code there is an option to display the inputted tags in another input, textarea etc. First option -
$('#singleFieldTags').tagit({
availableTags: sampleTags,
singleField: true,
singleFieldNode: $('#mySingleField')
});
Here the id - #singleFieldTags is the inputting field which is a list like <ul and id - #mySingleField displays the 'list-ordered' tags with commas between each.
All the tags that are added and removed in the #singleFieldTags appear in the #mySingleField. Since there is no built-it sortable function with tag-it, adding a sortable() to change the order of tags in #singleFieldTags, does not change the order of tags in #mySingleField.
The second option is a plain with only #singleFieldTags as follows :-
$('#singleFieldTags').tagit({
availableTags: sampleTags,
});
Although in tag-it.js there is a , the value does not appear in the mysql table after submitting the php form as the above list of tags is placed between <li></li>.
How is it possible to make the tags sortable and ensure that the same arrangement of tags in the list field <ul to be displayed in the <textarea as in First Option? Or How can the second option of sorting tags within a single field <input work and enabling it to be submitted by a form?
EDIT: There is a similar plugin like Tag-it called tagit here: http://webspirited.com/tagit/ . This plugin has sortable with an input box meaning if the tags were interchanged, and when submitted on form it would appear in the order of the sort. However, the disadvantage being that it has custom themeroller themes these are not similar and cannot even be linked to the ones at jQuery UI (jqueryui.com).
But on the other hand, the tag-it plugin (not tagit), can be loaded with these themes but does not provide the sortable function.
Here's a solution that uses the tag-it plugin, because I understand that your missing functionality is explained in your quote "...adding a sortable() to change the order of tags in #singleFieldTags, does not change the order of tags in #mySingleField".
In order to have "#mySingleField" reflect the new sort order, I'm adding a handler to the stop event of sortable():
$('#singleFieldTags').sortable({
stop: function(event,ui) {
$('#mySingleField').val(
$(".tagit-label",$(this))
.clone()
.text(function(index,text){ return (index == 0) ? text : "," + text; })
.text()
);
}
});
and
$('#singleFieldTags2').siblings(".tagit").sortable({
stop: function(event,ui) {
$('#singleFieldTags2').val(
$(".tagit-label",$(this))
.clone()
.text(function(index,text){ return (index == 0) ? text : "," + text; })
.text()
);
console.log( $('#singleFieldTags2').val() ); // just for reference
}
});
Here is a jsfiddle that demonstrates the functionality
(added functionality for single input field)

jqgrid autocomplete reload grid on selection of option

I am writing a piece of code, where i have a jqgrid and a filter with a textbox on the top, i have used the jquery ui autocomplete functionality, everything works fine, but i need to customize the functionality now i.e on autocomplete results when i select a option it should filter the grid with the result selected. Following is the example given below:
// i am doing a search on the textbox with auto complete feature
grid.jqGrid('setColProp', 'Name',
{
searchoptions: {
sopt:['cn'],
dataInit: function(elem) {
$(elem).autocomplete({
source:getUniqueNames('Name'),
delay:0,
minLength:0,
matchContains: true,
autoFill:true,
select:function(event,ui){
grid.setGridParam('postData', ui.item.value);
//grid.jqGrid('clearGridData');
grid.trigger('reloadGrid');
return false;
// need to write the code which will trigger the reload here on selection
}
});
}
}
});
On select function i need the selected option to be loaded in the jqgrid, how can i do it. Please suggest solution.
If you have implemented server side sorting and paging for your jqgrid then this should be easy, you simply need to reload the jqgrid.
To reload the grid see this, use this line of code
$("#grid1").trigger("reloadGrid", [{current:true}]);
this will reload the jqgrid, where you can pass in the paramters using postData: and get the filtered data.

Resources