Sending and getting an Array using fnServerParams

Sending and getting an Array using fnServerParams

SpreADXSpreADX Posts: 8Questions: 0Answers: 0
edited February 2014 in DataTables 1.9
I am having problems sending and getting data from an array using fnServerParams. Can some one please help?
Thanks my javascript code
[code]
function generateDataTable(subject, checkboxes) {


var oTable = $('#datatable1').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": base_url() + '/' + 'ccreateexam/getQuestions',
"fnServerParams": function(aoData) {
aoData.push({"name": "subject", "value": subject});
aoData.push({"name": "checkboxes[]", "value": checkboxes});

},
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"iDisplayStart ": 20,
"oLanguage": {
//"sProcessing": ""
},
"fnInitComplete": function() {
oTable.fnAdjustColumnSizing();
},
'fnServerData': function(sSource, aoData, fnCallback)
{
// alert(sSource);
$.ajax
({
'dataType': 'json',
'type': 'POST',
'url': sSource,
'data': aoData,
'success': fnCallback
});
}
});
}
[/code]

And in my controller

[code]
public function getQuestions() {

$subject = $this->input->post('subject');

$checkboxes = $this->input->post('checkboxes');

echo $this->mcreateexam->getQuestionsForSubjectLessons2($subject, $checkboxes);

}
[/code]

Thanks

Replies

  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    What is `checkboxes` ? Is it an array? If so, you will need to loop over it and add each item at a time.

    Allan
  • SpreADXSpreADX Posts: 8Questions: 0Answers: 0
    Firts of all, thanks for the amazing library. Yesterdays problem is solved, here is the code:

    javascript
    [code]
    function generateDataTable(subject, checkboxesData) {

    var oTable = $('#datatable1').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": base_url() + '/' + 'ccreateexam/getQuestions',
    "fnServerParams": function(aoData) {

    aoData.push({"name": "subject", "value": subject});
    aoData.push({"name": "lessonsCount", "value": checkboxesData[0]});

    for(var i = 0; i < checkboxesData[0]; i++){
    aoData.push({"name": "selectedLessons[]", "value": checkboxesData[1][i]});
    }
    },
    "bJQueryUI": true,
    "sPaginationType": "full_numbers",
    "iDisplayStart ": 20,
    "fnInitComplete": function() {
    oTable.fnAdjustColumnSizing();
    },
    'fnServerData': function(sSource, aoData, fnCallback)
    {
    $.ajax
    ({
    'dataType': 'json',
    'type': 'POST',
    'url': sSource,
    'data': aoData,
    'success': fnCallback
    });
    }
    });
    }
    [/code]

    controller
    [code]
    public function getQuestions() {

    $subject = $this->input->post('subject');
    $lessonsCount = $this->input->post('lessonsCount');
    $selectedLessons = $this->input->post('selectedLessons');

    if (!empty($subject)) {
    echo $this->mcreateexam->getQuestionsForSubjectLessons($subject, $selectedLessons, $lessonsCount);
    } else {
    echo $this->mcreateexam->getQuestionsForAllSubjectsLessons($selectedLessons, $lessonsCount);
    }
    }
    [/code]

    Now we are stuck trying to add a checkbox for every row in the datatable in a new column with a paramatrized name or id. Now, we're adding the checkbox with specific id, but we need each checkbox to have a diferent name or id, so we can recover the checked status using javascript. Do you have any idea to help us? We thank you very much for your work and your time. Thanks.
  • allanallan Posts: 63,489Questions: 1Answers: 10,470 Site admin
    The mRender option might help here:

    [code]
    {
    mData: null,
    mRender: function ( data, type, row ) {
    return '';
    }
    }
    [/code]

    Assuming you have an `id` property in the row source object. Change to whatever you do have.

    Allan
  • SpreADXSpreADX Posts: 8Questions: 0Answers: 0
    Thank you Allan, we solved the problem using another code. We put an id in the name and we used this code:

    [code]
    $(document).ready(function() {
    $('#createExamButton').click(function() {

    var oTable = $('#datatable1').dataTable();
    var checkedQuestions = [];
    $("input:checked", oTable.fnGetNodes()).each(function(){
    var elemnt = this;
    checkedQuestions.push(elemnt.id);
    });
    alert(checkedQuestions);
    });
    });

    [/code]
This discussion has been closed.