I am using jquery datatables. There I am using check box for each row inside the table. Now I want when a user click select all link which is outside of table, will select all records in the table. For that I am using this function.
function checkAll(formname, checktoggle)
{
var checkboxes = new Array();
checkboxes = document[formname].getElementsByTagName('input');
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = checktoggle;
}
}
}
Here select all link is
<a onclick="javascript:checkAll('frm_charges', true);" href="javascript:void(0);">Select All</a>
"frm_charges" is the name and id of the form.
This is the code for check box, which I am using inside the table.
<input type="checkbox" value="742" name="charges[]" class="charges" style="opacity: 0; position: absolute; z-index: -1; visibility: hidden;">
Now my problem is I have pagination it is selecting rows from first page but not all page.
Try This:
$(function () {
var oTable = $('#datatable').dataTable();
$('#selectall').click(function () {
var checkall =$('.main_table').find(':checkbox').attr('checked','checked');
$.uniform.update(checkall);
});
});
$(function () {
var oTable = $('#datatable').dataTable();
$('#deselectall').click(function () {
//alert('hi');
var checkall =$('.main_table').find(':checkbox').attr('checked',false);
$.uniform.update(checkall);
});
});
So the problem is your javascript is only getting the checkboxes on the screen. What you need to do is get the checkboxes that are in the original table data. In the following example I get all of the table data, loop through it marking the checkboxes and redraw the data table:
// var oTable - reference to your datatable object
var checkboxColumn = 14; // column number that has the checkboxes in it
function checkAll(checktoggle) {
// get all datatable data
var data = oTable.fnGetData();
// loop through all data
for (var i in data) {
// convert the input into jQuery object
var row = $('<div>' + data[i][checkboxColumn] + '</div>');
// Check the boxes as needed
row.children('input').attr('checked', (checktoggle) ? 'checked' : false);
// update the data in datatables
oTable.fnUpdate(row.html(), parseInt(i, 10), checktoggle, false, false);
}
// redraw the datatable
oTable.fnDraw();
}
You can refer below links and that will give clear functionality idea how to implement
http://datatables.net/examples/api/form.html
Related
I have a dropdown that is filled by a database and everything works well. However I want to pass a parameter to php based on the value of the dropdown which I can do. If I force the var to have a particular number it gets the corresponding item in the database. I'm having a problem to get the value of a dropdown. I've tried all the suggestions here in the forum and nothing works in this particular area of my code. I have it working on another piece of my code but not on this particular one and I don't know why. Here is my code:
<select id="servicos" onChange="return selectServ();">
<option value="item" class="itemoption">Serviço</option>
This is the code that is not working:
function selectServ() {
var e = document.getElementById("servicos");
var idserv = e.options[e.selectedIndex].value;
$.getJSON("http://ib.esy.es/gestao/_php/servicos_threadingpreco.php", { serv: idserv }, null).then(function(data) {
console.log(data);
var tr = data
for (var i = 0; i < data.length; i++) {
var tr = $('<tr/>');
// Indexing into data.report for each td element
$(tr).append("<td>" + data[i].preco + "</td>");
$('.table1').append(tr);
}
});
}
If I put
var idserv = "1"
It is working, however this:
var e = document.getElementById("servicos");
var idserv = e.options[e.selectedIndex].value;
Is not getting a value. The console log gives:
selectdynamicpreco.html:76 Uncaught TypeError: $(...).value is not a function
You should consider using jQuery to get the value of the dropdown:
$('#dropdown').val() will give you the selected value of the drop down element. Use this to get the selected options text.
$("#dropdown option:selected").text();
Should get you the text value of the dropdown.
This is working on another piece of the code
<script>
function selectCat(){
$('#servicos').change(function() {
$('#demo').text($(this).find(":selected").text());
});
//for textbox use $('#txtEntry2').val($(this).find(":selected").text());
var e = document.getElementById("categoria");
var servSelected = e.options[e.selectedIndex].value;
var url = "";
var items="";
if(servSelected === "1"){
url = "http://ib.esy.es/gestao/_php/servicos_threading.php";
}
if(servSelected === "2"){
url = "http://ib.esy.es/gestao/_php/servicos_sobrancelhas.php";
}
if(servSelected === "3"){
url = "http://ib.esy.es/gestao/_php/servicos_manicure.php";
}
$.getJSON(url,function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.ID+"'>"+item.servico+"</option>";
});
$("#servicos").html(items);
});
};
</script>
How can I add a dynamic row to the existing records in jQuery. I am getting some records in php. At the bottom there is an 'Add More Rows', which clones the last row. There are some existing calculations happening in the rows on a trigger like changing the selection will do some calculations, this newly created dynamic rows should adapt those calculations. Here in this case when I click Add New Rows, its adding the complete table. I just need the last row to append. Also the new created row is not taking any calculations. Anyone to guide me plsss? I have uploaded the script to the fiddle.
Fiddle
This is the script iam using to create dynamic rows:
$(window).load(function () {
jQuery(document).ready(function ($) {
$("#add_more").on('click', function (e) {
e.preventDefault();
var clone = $(".clone_row").eq(0).clone();
$("#cart1").append(clone);
});
$("#submit").on('click', function (e) {
e.preventDefault();
alert($("#cart1").serialize());
})
})
});
div can't be placed inside tbody.
Use following js
$("#add_more").on('click', function (e) {
e.preventDefault();
var clone = $("#table tbody tr:last").clone();
$("#table tbody").append(clone);
});
UPDATE use delegated event for dynamically generated element like following.
$('#cart1').on('change', '.currency-selec', function (event, processing) {
console.log("A row changed", processing, event);
var row = $(this).closest('tr');
var table = row.closest('table');
var currency = $(this).val();
if (processing) {
// Calculate me
calculateRowTotals($(row));
} else {
// Calculate other if not processing yet
table.find("tr").each(function () {
$(this).find("select").val(currency).trigger("change", 1);
});
}
});
$('#cart1').on('change', '#table :input', function () {
var $tr = $(this).closest('tr'),
$totInr = $tr.find('[name="total_inr[]"]'),
unitprice = +$tr.find('[name="unitprice[]"]').val() || 0,
qty = +$tr.find('[name="item_quantity_sup[]"]').val() || 0,
$currency = $tr.find('[name="currency_change[]"] option:selected'),
currency = $currency.val(),
inr = $('#inrvalue').val();
var total = unitprice * qty;
$tr.find('[name="total[]"]').val(total);
})
UPDATED FIDDLE
Yes. Azim is right.
And if you want to clear values in new row. You can try this.
var clone = $("#table tr:last").clone();
$("#table").append(clone).find('tr:last input').val('');
Using jQuery load html forms dynamically using append function. Here the following code load the page content dynamically based on number times of values on while loop.
Here I have a struggle on load the content with different values.its working with single value of 0 or 1 on var load_with_value=0; but not on both simultaneously i.e. increment the load_with_value++ for again load the page content of HTML forms.
$(document).ready(function(e) {
$("<DIV>").load("<?php echo $url; ?>", function() //url for loading page
{
var n = $('.item').length + 1; //load the html page content
var i = 1; //iteration for number of times load the content
var count = 2; //check the condition
var load_with_value = 0; //load the page content with different values for display different values on html form
while(i<count) { //loop starts
$("#product").append($(this).html());
i++;
load_with_value++;
}
});
});
First of all let's do some proper code formatting and get rid of the incorrect comments:
$(document).ready(function(e) {
$("<DIV>").load("<?php echo $url; ?>", function() {
var n = $('.item').length + 1;
var i = 1;
var count = 2;
var load_with_value = 0;
while(i<count) {
$("#product").append($(this).html());
i++;
load_with_value++;
}
});
});
Now let's take it apart:
If you want to use a temporary element to store the loaded data you need to assign it to a variable, so instead of
$("<DIV>").load("<?php echo $url; ?>", function() {
do
var tempObject = $("<div/>").load("<?php echo $url; ?>", function() {
Afterwards you can append the temporary element to an existing one with $('#someExistingElement').append(tempObject).
If you want to load the content into an existing element you should use it's ID, class or other selector to do this - not $("<div>").. If you want to load it to all div elements (please don't) then it should be $("div").
Next var n = $('.item').length + 1; makes no sense. It is never used in the code.
While cycle in this case is unnecessary. Don't use while cycles if you don't have to. You can use:
for(var i=0; i<count; i++){
//code
}
What is var load_with_value = 0; used for? I can only see you incrementing it with load_with_value++; but you don't use it anywhere..
Finally if you want to load different content based on the incremented variable it should be done outside of the .load function.. For example
$(document).ready(function(){
for(var i=0; i<5; i++){
$('#container-' + i).load('/somecontent-' + i + '.html');
}
});
This loads the content /somecontent-0.html to /somecontent-4.html into container elements with IDs container-0 to container-4 respectively.
I am getting the data from ajax and filling the select with it. Now How can make default selections in this.
Here are my ajax
var productLists =[];
$.when(http_get('admin/offer/data/sync')).then(function(response){
//returns a products array
$.each(response.products, function(i, item){
var product = {};
product['id'] = item.id;
product['text'] = item.product_name;
productLists[i] = product;
});
});
$('#sel_product').select2({
data: productLists
});
To get seleted values I can do another ajax list
Or I can print <otpion value="" selected="selected"> in the html from php.
So here how do i make default selection with select2?
You need to move the call to .select2() into the callback, so it will be initialized after the data is retrieved.
var productLists = [];
$.when(http_get('admin/offer/data/sync')).then(function(response){
//returns a products array
$.each(response.products, function(i, item){
var product = {};
product['id'] = item.id;
product['text'] = item.product_name;
productLists[i] = product;
});
$('#sel_product').select2({
data: productLists
});
});
I am trying to make autosuggest in Jquery,ajax and json to search cities when user register to website.
So far I am able to get results from database.And i appended to list.but now i need to select data using up down and enter keys.
Key down event is adding class to first city. But I want to loop through all results using key up and down and add value to city textbox if user hits enter. I limit data by 5 in php so 5 results are coming in list item.
Here is my code:
$('#city').keyup(function (event) {
var input_query = $(this).val();
$.post("get_city.php", {
"query": input_query
}, function (data) {
$('#cityres').html("");
$.each(data, function (i, item) {
$('#cityresults').append("<li>" + item.city + "</li>");
});
}, "json");
//below code is for key event
var key = gtKeycode(event);
if (key == 40) {
// I am not sure i need to do this way
$('li').first().addClass('SelectedCity');
}
});
function gtKeycode(e) {
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
return code;
}
i think i have now the solution for your problem hope this will help you..!
I made a php file that echo out json encode just list this:
//PHP "action.php?action=show"
e.g $option[] = array(
'option0'=>".Choose an option",
'option1'=>'somepage1',
'option2'=>'somepage2',
'option3'=>'somepage3');
echo json_encode(array('options'=>$option));
I made up html that will be the handler of the output, then will append
<select class="myoptions">
</select> | <span class="optcap"></span>
JS
function selectedOption()
{
var myoptions = $(".myoptions");
$.ajax({
type:'GET',
url:'action.php?action=show',
dataType:'JSON',
success:function(data){
if(data.s==1){
myoptions.empty();
$.each(data.options, function(x,val){
myoptions.append("<option class='option' value='"+val.option0+"'>"+val.option0+"</option>"
+"<option class='option' value='"+val.option1+"'>"+val.option1+"</option>"
+"<option class='option' value='"+val.option2+"'>"+val.option2+"</option>"
+"<option class='option' value='"+val.option3+"'>"+val.option3+"</option>");
});
}
}
});
}
$(document).ready(function(){
selectedOption();
$(".myoptions").keyup(function(){
var option = [];
$("option.option:selected").each(function(x){
option[x] = $(this).val();
});
$(".optcap").html("["+option+"]");
});
});