i'm trying to make my input Search field Searching for 2 values in 2 distinct columns at the same time in JS. i have started with 1 value Search with this code :
$("#boo").click(function(){
var table = $('#table').DataTable();
table.search("boo").draw();
});
i have try column().search() but I couldn't get it to work properly:
$("#boo").click(function(){
var table = $('#table').DataTable();
table.column([2,3]).search("boo", "New").draw();
});
I'm trying to filter into the table eg the gender and type, can you help me on that?
Try using .columns instead of .column:
$("#boo").click(function(){
var table = $('#table').DataTable();
table.columns([2,3]).search("boo", "New").draw();
});
See DataTables documentation here.
The function column().search() will search a specific column whereas columns().search() allows you to search multiple columns as defined in your array.
EDIT
i just added a line on your exemple :
$("#boo").click(function(){
var table = $('#table').DataTable();
table.search("boo").draw();
table.columns([2,3]).filter("boo", "New").draw();
});
Just search on 2 columns:
$("#boo").click(function(){
var table = $('#table').DataTable();
table.column(2).search("male");
table.column(3).search("asia").draw();
});
The .draw() function does not do anything you do not tell it to. In simple terms, you can run many different events then call them all with .draw(). Like Zim84 suggested, you can search in the 2 different columns but on the first one, you wouldn't do .draw() BUT you would call .draw() on your second search.
So.. yes, doing something like
table.column(2).search("male");
table.column(3).search("asia").draw();
would work fine.
Related
I'm trying to get the table data from some columns and I've tried a couple of variations I could think of, but none worked:
function updateAllTasks() {
var table = document.getElementById("dtable");
var [, ...tr] = table.querySelectorAll("tr");
var tableData = [...tr].map(r => {
var td = r.querySelectorAll("td");
console.log([...td].map(c => c[0].innerHTML, c[1].innerHTML, c[8].querySelectorAll('input[name="rowcheckbox"]')[0].value)); //This is the one giving me error ReferenceError: c is not defined
})
console.log(JSON.stringify(tableData));
}
Thanks for your help!
As mentioned by Andy, following data structures logic. The best approach for this case will be to call the data inside of an Array, and after that, build the table with the data.
This will provide more flexibility the moment you process the information.
Reference
Javascript Array
Arrays in Apps Script
I've a table set up with Tabulator with unknown data ( pulled from a file) so i can't make a column definitions setup for it except for general setup that works with any kind of data.
now I'm in situation that i need to add certain code for each row under column code which is included with the existing table.
i looked for away to add code something like SX0001 on the first row and just add 1 each row so the second row would be look like SX0002
I've checked this link http://tabulator.info/docs/4.1/update#alter-replace i saw some functions that works on rows but not columns .
solution i'm trying to achieve:-
the link includes under updateData function that i need to have index with default value (Id)
giving that I'm not sure what type of date it might have or be included with the table i've added a new column using this code
table.addColumn({title:"id", field: "id" , formatter:"rownum",width:40, align:"center"} , true)
now I don't know how to get the Length of the Column to loop through each one of the column and run function like this
var no = 1000;
var str = "SX";
var x = str + no ;
table.updateData([{id:1, code:x}]);
var no = no +1;
is there's anyway to do this ?
You can retrieve the data stored in the table using the getData function.
var data = table.getData();
This will return an array containing the data objects for each row in the table.
Then to get your lenght just do data.length
If you want to count the table data the getDataCount exactly for this, which returns a count of rows in the table:
var count = table.getDataCount();
But actually your current approach will result in the table being redrawn a load of times as you update the row data.
Mutators
Instead you should look at using a Mutator, these allow you to alter data as it enters the table, so in this case you could do something like this:
//global variable to keep track of row id
var rowCount = 0;
//custom mutator
var idMutator = function(value, data, type, params, component){
//increment rowCount and assign as column value
return rowCount++;
}
//in your column definition set this mutator
{title:"ID", field:"id", mutatorData:idMutator}
By using this approach the value is set as the data is loaded into the table, nothing needs to be redrawn and the whole proceess is much more efficient
I know how to set column names by using:
var table = $('#'+String(response.chartID[i])).DataTable({
stateSave: true,
aoColumnDefs: aryJSONColTable,
processing: true,
serverSide: true,
bDestroy: true,
"scrollX": true,}))
But is it possible to set oaColumnDef inside DataTable object?
For example on specific callbacks, I can set oaColumnDef to another set of names.
The closest I had is inside any callback, I've tried
$(this).dataTable().fnSettings().aoColumns
to get column as objects
But is it possible to set it again?
I've also tried
$(this).dataTable().aoColumnDefs = AnotherArray
However, it seems that this doesn't work.
Can anyone point me to the right direction?
Thanks
Assuming the variable table declared above
if you want to update particular column
table.fnUpdate("newChangedValue", row-index , column-index);
if you want to update complete row
oTable.fnUpdate( ['column1Value', 'column2Value', 'column3Value', ... ], rowIndex );
to add new record
table.fnAddData( [ column1Value, column2Value, column3Value.... ]);
to get clicked row index
$("#tblchargeRate tbody").delegate("tr", "click", function() {
var iPos = table.fnGetPosition( this );
});
var aData = table.fnGetData( iPos );
aData[0] is first column element in datatable
Note: take care of index value i.e first row in data table will be at 0 index
You can updating your datatables, by the code you posted you are using a old version.
In the new version you have a column function to get the columns by selectors.
Check the API doc
Consider I have a list called "test"
And it has 5 list items (say a, b, c, d, e)
Now that I apply filter manually on the list items for a particular field
Now the result is 3 list Items.
Now I want to read the fields of these 3 list items through JavaScript & do calculation on it.
I need to access the fields of the currently displayed (after filter is applied manually)
I do not want to filter it in Javascript. But I want to filter some field manually & then I want to read the displayed result. Can anyone help ?
The following jQuery will get all the values for a column for the rows that are displayed. It works on SharePoint 2010. I left the code very verbose and with debugging statements so it's easier to understand. Keep in mind that this code will not run in IE with the console closed unless you remove the console.log and debugger lines.
$(document).ready(function() {
var columnHeading = "Title";
var anchor = $("a").filter(function() {
return $(this).text() === columnHeading; //find the link that matches the text in the columnHeading variable
});
var th = anchor.closest("th"); //Get the parent table header
var columnIndex = th.index(); //Get the index of the table header in the row for use on other rows
var table = th.closest("table");
var i;
debugger;
table.find("tbody").first().find("tr").not(":first").each(function() { //Loop over every row in the table EXCEPT the first row because its the table header
i = $(this).find("td").eq(columnIndex); //find the td element at the index we determined earlier
console.log($(i).text()); //get the text in the td. You may need to parseInt before any mathematical formulas.
});
});
If you need clarification on anything, please let me know.
I recently started working with taffydb. Assuming I have this as my data
db= TAFFY([
{OrderNo:'prod1',range: 3,description:'one two'},
{OrderNo:'prod2',range: 2,description:'one two three'},
{OrderNo:'prod3',range: 2,description:'one three two'},
{OrderNo:'prod4',range: 6,description:'one two four three'},
{OrderNo:'prod5',range: 5,description:'three'},...
if I wanted to write a query to find all records with "one two" and "three" I'd do something like
db({description:{likenocase:"one two"}},{description:{likenocase:"three"}}).get()
this would return products 2 and 4. Unfortunately I can't figure out how to do this with a dynamic query that will have an unknown number of variables to search for. I'm doing this to let the user search for their own supplied words.
Anyone got any ideas?
As a precursor, this will not be the best answer to your problem. But it will work. :)
So the user will have the option of searching a database with an "unknown number of variables". Let's add a maximum amount of variables--perhaps 10?
Now we catch all the user's search variables in an array:
// Create a dynamic array
var userSearchVars = [];
// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
userSearchVars.push( $(this).val());
}
// Note: by default an empty input will return the empty string: ""
Using your code snippet, just query the database with the array:
db(
{description:{likenocase:userSearchVars[0]}},
{description:{likenocase:userSearchVars[1]}},
{description:{likenocase:userSearchVars[2]}},
{description:{likenocase:userSearchVars[3]}},
{description:{likenocase:userSearchVars[4]}},
{description:{likenocase:userSearchVars[5]}},
{description:{likenocase:userSearchVars[6]}},
{description:{likenocase:userSearchVars[7]}},
{description:{likenocase:userSearchVars[8]}},
{description:{likenocase:userSearchVars[9]}}
).get()
Adapting #Jacob-IT's answer so that its dynamic. Used Taffy for the first time tonight and just discovered you can pass an array of objects as the query argument.
// Create a dynamic array
var userSearchVars = [];
// Fill the array from 10 HTML input type=text fields
// You can fill your array however you fancy. This is just one example!
$("#myForm input[type=text]").each(function() {
// This is my edit - push the whole query on to the array.
userSearchVars.push({description:{likenocase: $(this).val() }});
}
// Then pass the whole query array in...
db( userSearchVars ).get()
Tested the above - and it worked for me.
You can do it like this
let items = [];
items.push({description:{likenocase:"one two"}});
items.push({description:{likenocase:"three"}});
db(...items).get()