I am using jTable with PHP Language,below is the jTable with data enter image description here

code for jTable that i have tried...

<script type="text/javascript">

    $(document).ready(function () {

        $('#StudentTableContainer').jtable({
            title: 'The Student List',
            paging: true, //Enable paging
            pageSize: 10, //Set page size (default: 10)
            sorting: true, //Enable sorting
            defaultSorting: 'Name ASC', //Set default sorting
            actions: {
                listAction: '/Demo/StudentList',
                deleteAction: '/Demo/DeleteStudent',
                updateAction: '/Demo/UpdateStudent',
                createAction: '/Demo/CreateStudent'
            },
            fields: {
                StudentId: {
                    key: true,
                    create: false,
                    edit: false,
                    list: false
                },
                Name: {
                    title: 'Name',
                    width: '23%'
                },
                EmailAddress: {
                    title: 'Email address',
                    list: false
                },
                Password: {
                    title: 'User Password',
                    type: 'password',
                    list: false
                },
                Gender: {
                    title: 'Gender',
                    width: '13%',
                    options: { 'M': 'Male', 'F': 'Female' }
                },
                CityId: {
                    title: 'City',
                    width: '12%',
                    options: '/Demo/GetCityOptions'
                },
                BirthDate: {
                    title: 'Birth date',
                    width: '15%',
                    type: 'date',
                    displayFormat: 'yy-mm-dd'
                },
                Education: {
                    title: 'Education',
                    list: false,
                    type: 'radiobutton',
                    options: { '1': 'Primary school',
                               '2': 'High school',
                               '3': 'University' }
                },
                About: {
                    title: 'About this person',
                    type: 'textarea',
                    list: false
                },
                IsActive: {
                    title: 'Status',
                    width: '12%',
                    type: 'checkbox',
                    values: { 'false': 'Passive', 'true': 'Active' },
                    defaultValue: 'true'
                },
                RecordDate: {
                    title: 'Record date',
                    width: '15%',
                    type: 'date',
                    displayFormat: 'dd.mm.yy',
                    create: false,
                    edit: false,
                    sorting: false //This column is not sortable!
                }
            }
        });

        //Load student list from server
        $('#StudentTableContainer').jtable('load');
    });

</script>

all thing with jTable working fine for me, but i want to add one more colomn named "Sr No" with number with 1,2,3.. etc before Name colomn.

Desired Output.. enter image description here

For jTable Reference :

EDIT:

code inside /Demo/StudentList method..

                    //Get record count
            $result = mysql_query("SELECT COUNT(*) AS RecordCount FROM people;");
            $row = mysql_fetch_array($result);
            $recordCount = $row['RecordCount'];

            //Get records from database
            $result = mysql_query("SELECT * FROM people ORDER BY " . $_GET["jtSorting"] . " LIMIT " . $_GET["jtStartIndex"] . "," . $_GET["jtPageSize"] . ";");

            //Add all records to an array
            $rows = array();
            while($row = mysql_fetch_array($result))
            {
                $rows[] = $row;
            }

            //Return result to jTable
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            $jTableResult['TotalRecordCount'] = $recordCount;
            $jTableResult['Records'] = $rows;
            print json_encode($jTableResult);
有帮助吗?

解决方案

my best guess: put this before the 'name' field

srNo: {
  title: 'Sr',
},

and fill the srNo in your json object inside /Demo/StudentList service.. you can use index in your loop

try this in your PHP codes:

//Add all records to an array
$rows = array();
$i = 1;
while($row = mysql_fetch_array($result))
{
   $row['srNo'] = $i;
   $rows[] = $row;
   $i++; 
}

crossing fingers!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top