How to restore jeditable functionality? - javascript

I try to figure out, how to restore Jeditable fields after removing cells from table?
I have table where are
rows with one cell (colspan)
rows with many cells (rows with ID-s)
Rows with ID have a cell which contains some text and editable span-element (with Jeditable). Now when I click on button, I want to remove from rows with ID all cells and replace it with cell which contains only the span-element (which should remain editable).
Problem is: I can't restore editability for those recreated spans.
I tried different approaches, most simple should running $( '.editable' ).editable('enable'), but I can't figure out, why it does not working.
One of my efforts is like this:
$(document).ready(function(){
$("#remove").click(function() {
$("table#original tr").each( function( index, row ) {
if ( $( row ).attr( 'id' ) ) {
var editabaleField = $( row ).children( 'td' ).children( 'span' ).text();
$( row ).children( 'td' ).remove();
$("<td colspan='3'><span class='editable'>" + editabaleField + "</span></td>").appendTo( row );
$( '.editable' ).editable('enable');
}
});
});
$('.editable').editable('echo.php', {
type : 'text',
tooltip : 'Just click...'
});
});
Made a Fiddle too, hope it helps to better understand my problem.

Problem
The issue seems to be that the editable object is being destroyed and when you attempt to re-enable it, it doesn't keep the same options so it fails.
Solution:
Abstract the options to a variable (for easier changes going forward)
var editableOptions = {
type : 'text',
tooltip : 'Just click...'
};
var editableURL = 'echo.php';
Update the .editable() call to use these new variables:
$(document).ready(function(){
$("#remove").click(function() {
$("table#original tr").each( function( index, row ) {
if ( $( row ).attr( 'id' ) ) {
var editabaleField = $( row ).children( 'td' ).children( 'span' ).text();
$( row ).children( 'td' ).remove();
$("<td colspan='3'><span class='editable'>" + editabaleField + "</span></td>").appendTo( row );
$( '.editable' ).editable(editableURL, editableOptions)
}
});
});
$('.editable').editable(editableURL, editableOptions);
});
Working JSFiddle

You don't need to recreate editable field in new td cell. In fact you should not remove it in the first place. Proper way of handling this from perspective of performance and optimization is to create new td and move already initialized editable element to it. In this case you don't have to worry about reinitizing it one more time.
So your code will become:
$(document).ready(function () {
$("#remove").click(function () {
$("table#original tr").each(function (index, row) {
if (row.id) {
var editableField = $(row).find('.editable');
var newTd = $("<td colspan='3'></td>").append(editableField);
$(row).empty().append(newTd);
}
});
});
$('.editable').editable('echo.php', {
type: 'text',
tooltip: 'Just click...'
});
});
Demo: http://jsfiddle.net/jxtnd1g4/
Or a little shorter variation for the same thing:
$("#remove").click(function () {
$("table#original tr[id]").html(function() {
var editable = $(this).find('.editable');
return $('<td colspan="3"></td>').append(editable);
});
});

Related

Bootstrap Select with callback function

I have 2 Bootstrap Select dropdowns, one is a list of countries, and another is a list of states. The country list is static and is populated on page load. The list of states is only populated via the Bootstrap Change event and loads states based upon the country.
I need to be able to populate both these values at the same time. So the problem is I'm trying to set the state value when its undefined ,or hasn't loaded its options yet. I've tried doing a callback function within the ajax call, but that doesn't appear to work. I think another problem is that there is the initial change binding that is goofing it up.
I've written a fiddle for my foundation on where I'm experiencing the problem.
https://jsfiddle.net/jeffbeagley/DTcHh/37843/
The user is presented with a blank form that allows them to select the values, but the user can also load a saved form from the database ( hence the function labled as "select_data" ).
$(document).ready(function() {
$( "#country" ).on( 'changed.bs.select', function(e) {
change_country($( this ).val());
});
$( "#load" ).click(function() {
select_data();
});
});
This function is called anytime the user selects a country, the actual ajax call goes out to the database and returns all the appropriate states. I tried passing the state into this function and selecting it that way, but setting the country first would still fire the country change event and override it.
function change_country(country_id) {
$.ajax({
type: 'GET',
cache: false,
url: '/echo/jsonp/',
success: function(response) {
$( "#state" ).empty();
if(country_id == 1) {
$( "#state" ).append( "<option value=1>Oklahoma</option" );
$( "#state" ).append( "<option value=2>Missouri</option" );
} else {
$( "#state" ).append( "<option value=1>Ontario</option" );
$( "#state" ).append( "<option value=2>Quebec</option" );
}
$( "#state" ).selectpicker( "refresh" );
}
});
}
This is the function that grabs the saved form from the database and populates the values. With the way that its set, the expected outcome would be to select the country Canada, and the state Quebec
function select_data() {
var country_id = 2
var state_id = 2
$( "#country" ).selectpicker('val', country_id);
$( "#state" ).selectpicker('val', state_id);
}
Thanks for any help!
Might've figured it out.. sometimes you just need to strip out your code and simplify it to find an answer..
I replaced the code to populate the states outside of the event being called from the country being changed. Then I declared a callback function within this new function on success. I then moved the logic to select the country into the populate_state function ( this may not be appropriate )
You can see the updated fiddle here,
https://jsfiddle.net/jeffbeagley/DTcHh/37849/
So now the function select_data is as follows
function select_data() {
var country_id = 2
var state_id = 2
populate_states(country_id, function() {
$( "#state" ).selectpicker('val', state_id);
});
}
Edit ----
So with this, I ran into a call stack issue as it was still applying the original change event. So when the function select_data() is called, I unbind any events attached to the country selector, and rebind within the callback.
function select_data() {
var country_id = 2
var state_id = 2
$( "#country" ).off( 'changed.bs.select');
$( "#country" ).selectpicker('val', country_id);
populate_states(country_id, function() {
$( "#state" ).selectpicker('val', state_id);
$( "#country" ).on( 'changed.bs.select', function(e) {
change_country(e,$( this ).val());
});
});
}

Datatables fnGetData returns different object from table row

I have this function
var table = $(tableId);
var datatable = table.dataTable();
table.on('click', 'i.pointerCursor', function() {
var $this = $(this),
tr = $(this).closest( 'tr' ).get(0);
if ( datatable.fnIsOpen(tr) ) {
$this.removeClass( 'fa-minus-square-o' ).addClass( 'fa-plus-square-o' );
datatable.fnClose( tr );
} else {
$this.removeClass( 'fa-plus-square-o' ).addClass( 'fa-minus-square-o' );
console.log(tr);
console.log(datatable.fnGetData(tr));
datatable.fnOpen( tr, self.dataTableFormatRow( datatable,tr,$this[0].attributes[0].textContent), 'details' );
}
});
I am logging out the tr and the datatable.fnGetData(tr), because I would need the row as an object. The tr returns the actual row, but when I pass this tr to the datatable.fnGetData(), it always returns a totally different object. And this object seems always the same, even if I click on a different row.
I can't figure it out, why this is. How could I fix this? Or should I replace the code?

Column filter is not working with row grouping

When I integrate jQuery DataTables column filter and row grouping, jQuery DataTables column filter is not working.
I tried the demo but it seems in the demo column filter also does not work.
SOLUTION
Plug-ins Row Grouping along with Column Filtering are no longer being developed, I would not recommend using them. Use DataTables options and API methods to perform row grouping and individual column searching as shown in Row grouping example and Individual column searching example.
// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
var title = $('#example thead th').eq( $(this).index() ).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
// DataTable
var table = $('#example').DataTable({
"order": [[2, 'asc']],
"drawCallback": function (settings){
var api = this.api();
// Zero-based index of the column for row grouping
var col_name = 2;
// If ordered by column containing names
if (api.order()[0][0] === col_name) {
var rows = api.rows({ page: 'current' }).nodes();
var group_last = null;
api.column(col_name, { page: 'current' }).data().each(function (name, index){
var group = name;
if (group_last !== group) {
$(rows).eq(index).before(
'<tr class="group"><td colspan="6">' + group + '</td></tr>'
);
group_last = group;
}
});
}
}
});
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
DEMO
See this jsFiddle for code and demonstration.

Passed data on selected row from dialog window to main page

$(document).ready(function(){
$('#1, #2').click(function(){
window.clickedbtnid = $(this).attr('id');
$( "#table_dialog_1" ).dialog();
});
$( "#table_dialog_1" ).find('td').click(function(){ $('#'+window.clickedbtnid).parent().prev().find('input').val($(this).id);
$( "#table_dialog_1" ).dialog('close');
})
});
$('#input_'+id).attr('value', $(this).html());
this was closed to what i want but it only pass based on clicked td's text of table on dialog area because what I need is the first td table of dialog window to pass on input tag, and second td and third on link tag element. See my last table in fiddle below, it should look like the table on the main page when dialog window is closed.
see this FIDDLE
I hope this is what you are looking for, I refactored the code a bit, added class clickMe to button, it makes id unnecessary.
the JS code
var clickedButton;
$(document).ready(function(){
$('.clickMe').click(function(){
clickedButton = this;
$( "#table_dialog_1" ).dialog();
});
$( '#table_dialog_1 tr').click(function(){
var tds = $(this).children();
$(clickedButton).parent().prev().find('input').val(tds.eq(0).html());
$(clickedButton).next('a').text(tds.eq(2).html()+','+tds.eq(1).html());
$( "#table_dialog_1" ).dialog('close');
});
});

jQuery mobile nested-list plugin bug going back

I'm using the 'nested-list' plugin for jQuery Mobile, this one:
The problem is that when you use more than one level the plugins fails going back. For example, in the fiddle I have created I can go to 'Test 1.2.1' without problem, If I going back 1 level it works fine and I go to 'Test 1.2', but then if I tried to go up one level more (it was 'Test1') it goes up 2 levels (to 'Test').
I have checked the plugin code but I can't find the problem and I have left a message in the Git forum with no answer. Maybe someone could help me here.
Thanks in advance!
Fiddle
Looking at the plugin code, it is only designed for one level deep nesting. This is because the developer chose to remove created subpages each time you click on a parent LI. So when you get to the second level of depth, its parent has been removed from the DOM and you have to click the back button twice to get to the original page.
I have made some changes to the plugin code that should solve this problem:
In _attachBindings, I have commented out the line that removes previously created subpages:
_attachBindings: function() {
this._on({
"click": "_handleSubpageClick"
});
this._on( "body", {
"pagechange": function(){
if ( this.opening === true ) {
this.open = true;
this.opening = false;
} else if ( this.open === true ) {
//Don't remove the old LI
//this.newPage.remove();
this.open = false;
}
}
});
},...
Then in _handleSubpageClick, I check if the subpage already exists in the DOM (via data attribute added when creating the page). If not, we go through the existing code that creates the subpage, and then in the end I store the created subpage id in a data attribute on the parent LI. If it does exist we just navigate to that page.
_handleSubpageClick: function( event ) {
if( $(event.target).closest( "li" ).children( "ul" ).length == 0 ) {
return;
}
this.opening = true;
//see if we already created the subpage
var $li = $(event.target).closest( "li" );
var pid = $li.data("nextpageid");
if (pid && pid.length > 0){
this.pageID = pid;
} else {
this.newPage = $( this.options.page ).uniqueId();
this.nestedList = $( event.target ).children( "ul" )
.clone().attr( "data-" + $.mobile.ns + "role", "listview" )
.css( "display", "block" );
this.pageName = (
$( event.target.childNodes[0] ).text().replace(/^\s+|\s+$/g, '').length > 0 )?
$( event.target.childNodes[0] ).text() : $( event.target.childNodes[1] ).text();
this.pageID = this.newPage.attr( "id" );
// Build new page
this.newPage.append(
$( this.options.header ).find( "h1" ).text( this.pageName ).end()
).append(
$( this.options.content )
).find( "div.ui-content" ).append( this.nestedList );
$( "body" ).append( this.newPage );
//save subpage id as data attribute of the LI
$li.data("nextpageid", this.pageID);
}
$( "body" ).pagecontainer( "change", "#" + this.pageID );
}...
Here is your updated FIDDLE
I removed the external link to the plugin and instead copied all the code into the javascript pane and made the edits. You should be able to copy that code directly and use as the updated plugin. (Of course I did this quickly and have not rigorously tested it, so make sure it works for you).

Categories

Resources