Disable The First Previous Button On Dynamic Page Numbers
Solution 1:
You should add the prev/next buttons to your HTML as hidden, and then reference them by ID instead of class (as you will only have one of each).
Keep track of the current page number in a global variable.
Show the "prev" button only when the page number is greater than 0. For the "next" button you could apply this trick:
Instead of loading 10 records, load one more, but not for display: if it exists, then the "next" button should be visible.
Here is a snippet with those changes (all changes are in HTML and top part of the script):
var currentPageNo = 0; // Keep track of currently displayed page
$('#next-btn').click(function(){ // Give buttons an ID (include them in HTML as hidden)userList(currentPageNo+10);
});
$('#prev-btn').click(function(){
userList(currentPageNo-10);
});
functionuserList(pageNo) {
var resType="userList";
createTable(resType,pageNo);
}
functioncreateTable(resType, pageNo) {
// Update global variable
currentPageNo = pageNo;
// Set visibility of the "prev" button:
$('#prev-btn').toggle(pageNo > 0);
// Ask one record more than needed, to determine if there are more records after this page:
$.getJSON("https://api.randomuser.me/?results=11&start="+pageNo, function(data) {
$('#datatable tr:has(td)').remove();
// Check if there's an extra record which we do not display, // but determines that there is a next page
$('#next-btn').toggle(data.results.length > 10);
// Slice results, so 11th record is not included:
data.results.slice(0, 10).forEach(function (record, i) { // add second argument for numbering recordsvar json = JSON.stringify(record);
$('#datatable').append(
$('<tr>').append(
$('<td>').append(
$('<input>').attr('type', 'checkbox')
.addClass('selectRow')
.val(json),
(i+1+pageNo) // display row number
),
$('<td>').append(
$('<a>').attr('href', record.picture.thumbnail)
.addClass('imgurl')
.attr('target', '_blank')
.text(record.name.first)
),
$('<td>').append(record.dob)
)
);
});
// Show the prev and/or buttons
}).fail(function(error) {
console.log("**********AJAX ERROR: " + error);
});
}
var savedData = []; // The objects as array, so to have an order.functionsaveData(){
var errors = [];
// Add selected to map
$('input.selectRow:checked').each(function(count) {
// Get the JSON that is stored as value for the checkboxvar obj = JSON.parse($(this).val());
// See if this URL was already collected (that's easy with Set)if (savedData.find(record => record.picture.thumbnail === obj.picture.thumbnail)) {
errors.push(obj.name.first);
} else {
// Append it
savedData.push(obj);
}
});
refreshDisplay();
if (errors.length) {
alert('The following were already selected:\n' + errors.join('\n'));
}
}
functionrefreshDisplay() {
$('.container').html('');
savedData.forEach(function (obj) {
// Reset container, and append collected data (use jQuery for appending)
$('.container').append(
$('<div>').addClass('parent').append(
$('<label>').addClass('dataLabel').text('Name: '),
obj.name.first + ' ' + obj.name.last,
$('<br>'), // line-break between name & pic
$('<img>').addClass('myLink').attr('src', obj.picture.thumbnail), $('<br>'),
$('<label>').addClass('dataLabel').text('Date of birth: '),
obj.dob, $('<br>'),
$('<label>').addClass('dataLabel').text('Address: '), $('<br>'),
obj.location.street, $('<br>'),
obj.location.city + ' ' + obj.location.postcode, $('<br>'),
obj.location.state, $('<br>'),
$('<button>').addClass('removeMe').text('Delete'),
$('<button>').addClass('top-btn').text('Swap with top'),
$('<button>').addClass('down-btn').text('Swap with down')
)
);
})
// Clear checkboxes:
$('.selectRow').prop('checked', false);
handleEvents();
}
functionlogSavedData(){
// Convert to JSON and log to console. You would instead post it// to some URL, or save it to localStorage.console.log(JSON.stringify(savedData, null, 2));
}
functiongetIndex(elem) {
return $(elem).parent('.parent').index();
}
$(document).on('click', '.removeMe', function() {
// Delete this from the saved Data
savedData.splice(getIndex(this), 1);
// And redisplayrefreshDisplay();
});
/* Swapping the displayed articles in the result list */
$(document).on('click', ".down-btn", function() {
var index = getIndex(this);
// Swap in memory
savedData.splice(index, 2, savedData[index+1], savedData[index]);
// And redisplayrefreshDisplay();
});
$(document).on('click', ".top-btn", function() {
var index = getIndex(this);
// Swap in memory
savedData.splice(index-1, 2, savedData[index], savedData[index-1]);
// And redisplayrefreshDisplay();
});
/* Disable top & down buttons for the first and the last article respectively in the result list */functionhandleEvents() {
$(".top-btn, .down-btn").prop("disabled", false).show();
$(".parent:first").find(".top-btn").prop("disabled", true).hide();
$(".parent:last").find(".down-btn").prop("disabled", true).hide();
}
$(document).ready(function(){
$('#showExtForm-btn').click(function(){
$('#extUser').toggle();
});
$("#extUserForm").submit(function(e){
addExtUser();
returnfalse;
});
});
functionaddExtUser() {
var extObj = {
name: {
title: "mr", // No ladies? :-)first: $("#name").val(),
// Last name ?
},
dob: $("#dob").val(),
picture: {
thumbnail: $("#myImg").val()
},
location: { // maybe also ask for this info?
}
};
savedData.push(extObj);
refreshDisplay(); // Will show some undefined stuff (location...)
}
table, th, td {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 10px;
}
.parent {
height: 25%;
width: 90%;
padding: 1%;
margin-left: 1%;
margin-top: 1%;
border: 1px solid black;
}
.parent:nth-child(odd){
background: skyblue;
}
.parent:nth-child(even){
background: green;
}
label {
float: left;
width: 80px;
}
input {
width: 130px;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttononclick="userList(0)">Load First Page</button><buttonid="next-btn"style="display:none">Next Page</button><buttonid="prev-btn"style="display:none">Previous Page</button><tableid="datatable"><tr><th>Select</th><th>Name</th><th>DOB</th></tr></table><buttononclick="saveData()">Save Selected</button><br /><divclass="container"></div><buttononclick="logSavedData()">Get Saved Data</button><buttonid="showExtForm-btn">Open External Form</button><divid="extUser"style="display:none"><formid="extUserForm"><p><labelfor="name">Name:</label><inputtype="text"id="name"required></p><br /><p><labelfor="myImg">Image:</label><inputtype="url"id="myImg"required></p><br /><p><labelfor="dob">DOB:</label><inputtype="date"id="dob"required></p><br /><button>Submit</button></form></div>
As this URL provides random data, the pages will not show the same data when you revisit them. Also, there is apparently no last page in this random data set. I have guessed the URL parameter for specifying the first record (start
), since the results
parameter is for specifying the page size, not the first record number. Adapt as needed.
Solution 2:
Check previ
value is less or equal to 10 if so hide it. Same reverse logic apply for next button. And on next button you can show prev-btn
.
$(document).on('click', '.prev-btn', function(){
var previ = pageNo - 10;
if(previ < = 10)
$(this).hide();
UserList(previ);
});
Post a Comment for "Disable The First Previous Button On Dynamic Page Numbers"