I am using jquery's dataTables plugin. I am having skills value as header in my table. I want to serch for users with specific skills. For example as shown in figure i want to search for users who have skill php. Then i should get the name of John. For css i should get name of Mona. The snapshot is here:
You can do this by implementing a custom filter for the datatable, as described here.
Before filtering you have to find, the index of the column you want to filter and check afterwards the value in each row.
This could look somewhat like this:
<input type="text" id="skillFilter" />
<table id="example">
<thead>
<tr>
<th>Skills</th>
<th>PHP</th>
<th>CSS</th>
<th>HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<td>Mona</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
script :
$.fn.dataTable.ext.search.push(
function(settings, data) {
if (skillFilterColumnIndex != undefined) {
//Get the data of each row
var data = data[skillFilterColumnIndex] || "";
return data > 0;
} else {
return 1;
}
});
$("#skillFilter").change(function() {
var skill = $("#skillFilter").val().toLowerCase();
//Find the column index with the skill to filter
$.each($("#example thead th"), function(index, element) {
if (index != 0 && element.innerHTML.toLowerCase() == skill) {
skillFilterColumnIndex = index;
}
});
table.fnDraw();
});
Related
I have following razor page in my Blazor server app (Code-1). In this razor page I have two dynamic tables. In the first table the CNC AX names of a CNC machine are listed (AX0...AX7). If the user clickes to the axis name, the axis parameters belonging to this axis shall be listed in the second table.
As result if I click to any axis name, I get always the parameters of the last axis (AX7) in the second table.
The axis parameters are read from the two dimension CNC_AX_MD array. I am sure that the array has the correct values for each axis, because if I write the first table like Code-2 (below) everything is working fine. But the number of axis are sometimes much more then in my code, so I have to shorten the code.
I think I have a problem on my dynamic table structure in table-1 with the for loop. What could be the problem? I have tried the act. commented out "StateHasChanged()" but it didn't help.
Code-1
<table>
#{ for (int j = 0; j < 8; j++)
{
<tr>
<td #onclick='#(e => Select_CNC_AX_par(j))'>#CNC_AX_Names[j]</td>
</tr>
//StateHasChanged();
}
}
</table>
<table>
#{ for (int m = 0; m < arraylenght; m++)
{
<tr>
<td >CNC_AX_parameter[m]</td>
</tr>
}
}
</table>
public void Select_CNC_AX_par(int aksno)
{
for(int k=0; k < arraylenght;k++)
{
CNC_AX_parameter[k] = CNC_AX_MD[k, axno];
}
}
Code-2
<table>
#{ <tr>
<td #onclick='#(e => Select_CNC_AX_par(0))'>#CNC_AX_Names[0]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(1))'>#CNC_AX_Names[1]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(2))'>#CNC_AX_Names[2]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(3))'>#CNC_AX_Names[3]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(4))'>#CNC_AX_Names[4]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(5))'>#CNC_AX_Names[5]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(6))'>#CNC_AX_Names[6]</td>
</tr>
<tr>
<td #onclick='#(e => Select_CNC_AX_par(7))'>#CNC_AX_Names[7]</td>
</tr>
}
</table>
Try to use the following code:
<table>
#{
for (int j = 0; j < 8; j++)
{
var Number = j;
<tr>
<td #onclick='#(e => Select_CNC_AX_par(Number))'>#j</td>
</tr>
//StateHasChanged();
}
}
</table>
Do not use a loop variable directly in a lambda expression, such as i in the preceding for loop example. Otherwise, the same variable is used by all lambda expressions, which results in use of the same value in all lambdas.
For more details,you can try to refer to the official doc.
I'd like for my page to initially load with just a search box, and then when someone begins to search that is when the table appears.
My example table is below:
<script>
$(document).ready(function() {
$('#example').DataTable( {
responsive: true
} );
} );
</script>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th class="none">Branch</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test 1</td>
<td>Test test test</td>
<td>ABC</td>
</tr>
<tr>
<td>Test 2</td>
<td>Test test test</td>
<td>DEF</td>
</tr>
<tr>
<td>Test 3</td>
<td>Test test test</td>
<td>GHI</td>
</tr>
</tbody>
</table>
yes it can be done using ajax call and i suppose you use php.
For example, you can generate a table everytime you press any char, populating the table with the results or using a button to search the keyword you type on the input field.
Example with ajax and php:
var search_string = $('#id_of_the_input_box').val();
$.ajax({
url:"php_file.php",
method:"POST",
data:{action:action},
dataType:"json",
success:function(data){
console.log(data);
}
and the php code using PDO
$search_string = $_POST["string"];
$result = '';
$query = $connection->prepare(" select * from table where name = '%".search_string."' ";
$query->execute();
$query_results->query->fetchAll;
if($query->rowCount() > 0) {
$result = '
<script>
$(document).ready(function() {
$('#example').DataTable( {
responsive: true
});
});
<table id="example" class="display" style="width:100%">
YOUR TABLE ROWS HERE
</table>
';
// foreach loop to populate the table example
foreach ($query_results as $row) {
$result.= '
<tr>
<td>'.$row["table_column_name_1"].'</td>
<td>'.$row["table_column_name_1"].'</td>
<td>'.$row["table_column_name_1"].'</td>
<td>'.$row["table_column_name_1"].'</td>
'
} else {
echo "No results";
}
}
// echo the generated table as ajax response
echo $result;
Hope you have caught the idea with this example.
I want to generate a pdf from below HTML table, and I’m using jsPdf-AutoTable for that. I’m getting a PDF as below image. The table consist of correct number of rows without any data in it. How to get generate the pdf our of this table.?
I'm also using Datatable.js on this table.
HTML Table markup and Javascript given below.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script>
<script src="https://github.com/MrRio/jsPDF/blob/master/dist/jspdf.debug.js"> </script>
<script src="https://github.com/simonbengtsson/jsPDF-AutoTable/blob/master/dist/jspdf.plugin.autotable.src.js"> </script>
// this function generates the pdf using the table
function generate() {
var columns = ["productid", "productname", "unit", "unitprice"];
var data = tableToJson($("#products-table").get(0), columns);
var doc = new jsPDF('p', 'pt');
doc.autoTable(columns, data);
doc.save("table.pdf");
}
// This function will return table data in an Array format
function tableToJson(table, columns) {
var data = [];
// go through cells
for (var i = 1; i < table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j = 0; j < tableRow.cells.length; j++) {
rowData[columns[j]] = tableRow.cells[j].innerHTML;
}
data.push(rowData);
}
return data;
}
<table id="products-table" cellspacing="0" width="100%">
<thead>
<tr>
<th>ProductId</th>
<th>ProductName</th>
<th>Unit</th>
<th>UnitPrice</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
Run Code
It is because you are creating objects from table data. You should create an array from the data.
function tableToJson(table, columns) {
var data = [];
// go through cells
for (var i = 1; i < table.rows.length; i++) {
var tableRow = table.rows[i];
// create an array rather than an object
var rowData = [];
for (var j = 0; j < tableRow.cells.length; j++) {
rowData.push(tableRow.cells[j].innerHTML)
}
data.push(rowData);
}
return data;
}
check working fiddle
https://jsfiddle.net/shakee93/dh8e7gjc/
I am using jquery kendo grid in my project where i used row template to show three column in one row. Below is the code:
<table id="grid" style="width:100%">
<thead style="display:none">
<tr>
<th>
Details
</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td>
</td>
</tr>
</tbody>
</table>
<script id="rowTemplate" type="text/x-kendo-tmpl">
<div>
<span class="name" style="font-size:medium">#: FirstValue #</span>
<span class="name" style="font-size:medium">#: SecondValue #</span>
</div>
<tr>
<td style="width:30%">
#: GetName #
<span class="name" style="font-size:14px; color:green">#: Designation #</span>
<span class="name" style="font-family:Arial; font-size:small">#: Company #</span>
</td>
</tr>
</script>
in the above code i am just passing my model data it's working fine but when i added one div which have value firstName and LastName so it is also repeating with this data but i want to to show separately.How do i show it separately so that it should not repeat with grid.
there is one problem in your html template.
Please replace '#' with 'Javascript:void(0)'.
Error:- #: GetName #
Fix:- #: GetName #
Hope that's work for you.
http://jsfiddle.net/parthiv89/t0w3ht6m/1/
if you like then don't forget to like.
I got solution by own, Firstly i changed code in my schema like this:
schema: {
parse: function (data) {
var items = [];
for (var i = 0; i < data.data.length; i++) {
if (data.data[i].CorrectValue != null && data.data[i].SearchValue != null) {
$("#spnSR")[i].innerHTML = "<b>"+"Get results for this text: "+"</b>"+data.data[i].CorrectValue;
$("#spnSV")[i].innerHTML = "<b>" + "Searched for this text: " +"</b>" + data.data[i].SearchValue;
}
}
var product = {
data: data.data,
total: data.total
};
items.push(product);
return (items[0].data);
},
}
Then in html i used two span to show this value which is there in for loop.
and it's working fine for me.
Thanks everyone.
I want to let a user fire sql query and then see the result in grails view page.
My QueryController.groovy is
def query(){
def headers = null; //["id","name"]
Sql sql = new Sql(dataSource)
def rowResults = sql.rows(params.query) //GroovyRowResult
rowResults.eachWithIndex { row, index->
if (headers == null) {
headers = row.keySet()
}
}
[headers : headers, resultList: rowResults, total : rowResults.size() ]
}
In grails view page (query.gsp),
<table class="table table-striped">
<thead>
<tr>
<g:each in="${headers}" var="header">
<g:sortableColumn property="header" title="${header}" />
<th></th>
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${resultList}" var="row">
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val}</td>
</g:each>
</tr>
</g:each>
</tbody>
</table>
The <td>${val}</td> part in view is not working as expected, because it gives result as id=1 instead of 1. I want only value to be displayed there.
Might be a smaller issue though, need to fix it.
Thanks.
Try accessing values by accessing the value on each map:
<table class="table table-striped" border="1">
<thead>
<tr>
<g:each in="${headers}" var="header">
<g:sortableColumn property="header" title="${header}" />
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${resultList}" var="row">
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val.value}</td>
</g:each>
</tr>
</g:each>
</tbody>
</table>
/
Also in your query action you can get the header directly from the map:
def query(){
def headers = null; //["id","name"]
Sql sql = new Sql(dataSource)
def rowResults = sql.rows("select * from Message;") //GroovyRowResult
// rowResults is a list, you can access it by its index
headers = rowResults[0].keySet()
FYI, what you are giving your users is very powerful and they can run any sort of query against you database even to drop your tables.
Well, I got the following code working as val is KVP.
<tr>
<g:each status="counter" in="${row}" var="val">
<td>${val.value}</td>
</g:each>
</tr>