From the code as below, I have tried to filter result when the text input (searchdata) has some value and the search button is clicked.
I am using Datalist variable to display the search result on view. I am not sure how can I access Datalist variable (in indexAction) from filterAction to display filtered result on view. Could you please suggest?
View (views/test/index.volt) :
<!-- searching data -->
<div class="mdl-textfield fixedsize mdl-js-textfield mdl-textfield--floating-label" autocomplete="off">
{{ text_field('searchdata','autocomplete':'off', 'class': 'mdl-textfield__input searchfield') }}
</div>
<label class="search_buttondiv mdl-button mdl-js-button mdl-button--icon" for="sample6">
<i class="material-icons" id="search-icon1">search</i>
</label>
<!-- display data -->
<table class="userlist mdl-data-table mdl-js-data-table mdl-shadow--2dp">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric">UserID</th>
<th>username</th>
</tr>
</thead>
<tbody>
{% for i,Datalist in Data %}
<tr>
<td>
{{ Data['_id'] }}
</td>
<td>
{{ Data['name'] }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<script>
$('#search-icon1').click(function(){
$(function(){
$.ajax({
url: '<?php echo $this->url->get("test/filter");?>',
type: 'post',
data: {userid : document.getElementsByName('searchdata')[0].value},
dataType:"json",
success: function(){
console.log("success filtering");
},
error: function(xhr, ajaxOptions, thrownError){
console.log(thrownError);
}
});
});
});
</script>
Controller (controllers/testController.php) :
<?php
class testController extends ControllerBase
{
public function indexAction()
{
$app = new AppController;
$result = $app->getDatafromUser(null);
$this->view->setVar('Datalist', $result);
}
public function filterAction()
{
$app = new AppController;
$result = $app->getDatafromUser($_POST['userid']);
$this->view->setVar('Datalist', $result); /*How can I access Datalist variable from indexAction?*/
}
}