Quiero usar este código como una función () para que se ejecute solo cuando hago clic en un botón y no automáticamente cuando se cambia el menú desplegable. Al hacer clic en <button onclick="CalendarFilter">Cercar</button> se ejecuta la función. Intenté poner el código en esto pero no funcionó.
function myFunction() { CODE HERE } $(document).ready(function () { $('.filter').change(function () { var values = []; $('.filter').each(function () { var colIdx = $(this).data('col'); $(this).find('option:selected').each(function () { if ($(this).val() != "") values.push( { text: $(this).text(), colId : colIdx }); }); }); filter('table > tbody > tr', values); }); function filter(selector, values) {console.log(values); $(selector).each(function () { var sel = $(this); var tokens = sel.text().trim().split('\n'); var toknesObj = [], i; for(i=0;i<tokens.length;i++){ toknesObj[i] = { text:tokens[i].trim(), found:false }; } var show = false; //console.log(toknesObj); $.each(values, function (i, val) { if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) { toknesObj[val.colId].found = true; } }); console.log(toknesObj); var count = 0; $.each(toknesObj, function (i, val) { if (val.found){ count+=1; } }); show = (count === values.length); show ? sel.show() : sel.hide(); }); } });Al hacer clic en Cercar se ejecuta el CalendarFilter
Gracias.
Necesito una forma de ejecutar esto de esta manera:
function myFunction() { $(document).ready(function () { $('.filter').change(function () { var values = []; $('.filter').each(function () { var colIdx = $(this).data('col'); $(this).find('option:selected').each(function () { if ($(this).val() != "") values.push( { text: $(this).text(), colId : colIdx }); }); }); filter('table > tbody > tr', values); }); function filter(selector, values) {console.log(values); $(selector).each(function () { var sel = $(this); var tokens = sel.text().trim().split('\n'); var toknesObj = [], i; for(i=0;i<tokens.length;i++){ toknesObj[i] = { text:tokens[i].trim(), found:false }; } var show = false; //console.log(toknesObj); $.each(values, function (i, val) { if (toknesObj[val.colId].text.search(new RegExp("\\b"+val.text+"\\b")) >= 0) { toknesObj[val.colId].found = true; } }); console.log(toknesObj); var count = 0; $.each(toknesObj, function (i, val) { if (val.found){ count+=1; } }); show = (count === values.length); show ? sel.show() : sel.hide(); }); } }); }Para llamar a esa función usando un botón.
<button onclick="myFunction()">Cercar</button>El problema es que de esta forma no funciona.
Gracias.