Welcome Guest, Not a member yet? Register   Sign In
Data Tables
#1

[eluser]MLCstudio[/eluser]
Hi,

I have loaded some data from my database and want to make some standard changes to it before I export it to datatables. I need some extra tags for editing purposes. To begin with I made a string with concatenations to replicate the format of the required Json. This turned out to be an error as I behave there must be non visible information in a Json (just new don't really understand).

So I want to perform these functions on my data and then export as a json array. Don't know the steps between $this->db->get().

Here are the alterations I want to perform

Code:
foreach ($records->result_array() as $row)
{
   $reconciled = $row['Reconciled'];
if ($reconciled == 1)
  {$checked = "checked";}
      
else
  {$checked = "";}

$row['Comment'] = "<div id=\"".$row['ID']."\" class=\"edit\"> ".$row['Comment']." </div>";

$row['Reconciled'] = "&lt;form&gt;&lt;input id=\"".$row['ID']."\" type=\"checkbox\" name=\"Reconciled\" value=\"1\"".$checked.">&lt;/form&gt;";

Quite happy to accept I'm going about this totally the wrong way, would like to hear how someone experienced would go about this if they would be happy to share. Not sure why the escaping is functioning like this on the browser ' in dreamweaver don't seem to need escaping, perhaps this is one of my problems.
#2

[eluser]MLCstudio[/eluser]
Used another form of concatination to similar results. Really could do with some pointers on Array use as I am overwriting my array on each pass. And when I echoed inside the foreach it produced invalid Json as there was a }{ between each entry. And I guess from the example given I need ][

Code:
foreach ($records->result_array() as $row)
     {
      $reconciled = $row['Reconciled'];
      if ($reconciled == 1)
      {
       $checked = "checked";
     //  return $checked;
      }
      
      else
      {
       $checked = "";
     //  return $checked;
      }

      
     $pre_json_array = array(
     'Date' => $row['Date'],
     'Desc.' => $row['Desc.'],
     'Credit' => $row['Credit'],
     'Debit' => $row['Debit'],  
     'Comment' => "<div id='".$row['ID']."' class='edit'> ".$row['Comment']." </div>",
     'Reconciled' => "&lt;form&gt;&lt;input id='".$row['ID']."' type='checkbox' name='Reconciled' value='1'".$checked.">&lt;/form&gt;",
     'ID' => $row['ID']
     );
      
     }
     echo (json_encode ($pre_json_array));

My output was either one of these boxes or multiple depending on where I insered the echo

Code:
...............{
    "Date": "2012-03-30",
    "Desc.": "xxxxxxxxxxxxxxxxxxxx",
    "Credit": "11.78",
    "Debit": "0.00",
    "Comment": "<div id='1053' class='edit'> Personal <\/div>",
    "Reconciled": "&lt;form&gt;&lt;input id='1053' type='checkbox' name='Reconciled' value='1'><\/form>",
    "ID": "1053"
}{
    "Date": "2012-03-30",
    "Desc.": "xxxxxxxxxxxxxxxx",
    "Credit": "46.28",
    "Debit": "0.00",
    "Comment": "<div id='1052' class='edit'> Personal <\/div>",
    "Reconciled": "&lt;form&gt;&lt;input id='1052' type='checkbox' name='Reconciled' value='1'><\/form>",
    "ID": "1052"
}............

Forsee a problem with json escaping my end tags too e.g. <\/form> perhaps not
#3

[eluser]Rok Biderman[/eluser]
It's not really clear what you're trying to achieve here, as having to send form tags definitely seems odd. This might be helpful or not, but the last way I did something like this was load most of the data on first load, then use ajax only to get json_encoded result of a db query and made the rows for the table with jquery $()each and just append it to an existing table. This is copy paste of that:

Code:
$.each(data, function(key, val) {
              var tr=$('<tr></tr>');
                 $('<td class="center" id="'+val.bid_id+'">&lt;input type="radio" name="reaward_bid_id" value="'+val.bid_id+'"&gt;&lt;/td>').appendTo(tr);
                 $('<td class="reaward center">'+val.bid_amount+'</td>').appendTo(tr);
                 $('<td class="reaward center">'+val.user_first_name+' '+val.user_last_name+'</td>').appendTo(tr);
              tr.appendTo('#reaward_table tbody');
             });
#4

[eluser]MLCstudio[/eluser]
Thanks for this, seems to make more sense than my way. I just don't know a lot of the functions.

The example code is like this and I was trying to match it. I don't know of a way to add the tags to the columns individually for example make a div around the "win 95+" column with a class of edit. With no experience of propper practice I just had a go. Hope this might help you better understand what I want to acheive. Probably the extra tags come in the step between this json and the creation of the table?

Code:
[ "Trident", "Internet Explorer 4.0", "Win 95+", 4, "X" ],
            [ "Trident", "Internet Explorer 5.0", "Win 95+", 5, "C" ],
            [ "Trident", "Internet Explorer 5.5", "Win 95+", 5.5, "A" ],
            [ "Trident", "Internet Explorer 6.0", "Win 98+", 6, "A" ],
            [ "Trident", "Internet Explorer 7.0", "Win XP SP2+", 7, "A" ],
            [ "Gecko", "Firefox 1.5", "Win 98+ / OSX.2+", 1.8, "A" ],
            [ "Gecko", "Firefox 2", "Win 98+ / OSX.2+", 1.8, "A" ],
            [ "Gecko", "Firefox 3", "Win 2k+ / OSX.3+", 1.9, "A" ],
            [ "Webkit", "Safari 1.2", "OSX.3", 125.5, "A" ],
            [ "Webkit", "Safari 1.3", "OSX.3", 312.8, "A" ],
            [ "Webkit", "Safari 2.0", "OSX.4+", 419.3, "A" ],
            [ "Webkit", "Safari 3.0", "OSX.4+", 522.1, "A" ]

I would normally talk this through with my friend who has been introducing me to php but he's away for three weeks and then I leave for 3 months.
#5

[eluser]Rok Biderman[/eluser]
For me, usually the best solution is the most robust, cause I don' like support calls much. Now, there are a couple of square brackets missing which denote that it's an array of arrays:
Code:
[["Trident","Internet Explorer 4.0","Win 95+",4,"X"],["Trident","Internet Explorer 5.0","Win 95+",5,"C"],["Trident","Internet Explorer 5.5","Win 95+",5.5,"A"],["Trident","Internet Explorer 6.0","Win 98+",6,"A"],["Trident","Internet Explorer 7.0","Win XP SP2+",7,"A"],["Gecko","Firefox 1.5","Win 98+ \/ OSX.2+",1.8,"A"],["Gecko","Firefox 2","Win 98+ \/ OSX.2+",1.8,"A"],["Gecko","Firefox 3","Win 2k+ \/ OSX.3+",1.9,"A"],["Webkit","Safari 1.2","OSX.3",125.5,"A"],["Webkit","Safari 1.3","OSX.3",312.8,"A"],["Webkit","Safari 3.0","OSX.4+",522.1,"A"]]

I'll paste a bit more of that code, so you see how I've done it.

Code:
[removed]
$().ready(function() {

        $('.reaward_bid').click(function() {
            project_id = $(this).attr('id');
            $.post('&lt;?php echo site_url('bids/jquery_get_other_bids'); ?&gt;', {
                project_id: project_id
            }, function(data)
            {
             $.each(data, function(key, val) {
              var tr=$('<tr></tr>');
                 $('<td class="center" id="'+val.bid_id+'">&lt;input type="radio" name="reaward_bid_id" value="'+val.bid_id+'"&gt;&lt;/td>').appendTo(tr);
                 $('<td class="reaward center">'+val.bid_amount+'</td>').appendTo(tr);
                 $('<td class="reaward center">'+val.user_first_name+' '+val.user_last_name+'</td>').appendTo(tr);
              tr.appendTo('#reaward_table tbody');
             });
             $('#reaward_table').dataTable({"bJQueryUI": true, "bPaginate": false, "bFilter": false, "bRetrieve": true}); //initialize table only after you receive the data
            }, "json");

            $('#award_bid_dialog').dialog({
                resizable: false,
                height:"auto",
                width:"auto",
                modal: true,
                buttons: {
                    "Reaward": function() {
                        $.post('&lt;?php echo site_url('bids/jquery_reaward_bid'); ?&gt;', {
                            bid_id: $('input:radio[name=reaward_bid_id]:checked').val(),
                            project_id: project_id
                        }, function(data)
                        {
                         if (data == 1) {
                             $('#bid_award_notification').fadeIn('fast');
                             $('#award_bid_dialog').dialog('close');
                         }
                        });

                    },
                    "Cancel": function() {
                        $(this).dialog('close');
                    }
                }
            });
        });

});
[removed]

<div id="award_bid_dialog">
<span>Award the project to another bidder</span>
<table id="reaward_table" cellpadding="0" cellspacing="0" border="0" class="display data-table">
  <thead>
   <tr class="gradeA">
    <th></th>
    <th>Bid amount</th>
    <th>Partner name</th>
   </tr>
  </thead>
  <tbody>
  </tbody>
</table>
</div>
<a href="#" id="10" class="reaward_bid">Click to reaward bid</a>

the controller jquery_get_other_bids is very simple:

Code:
public function jquery_get_other_bids()
{
  $result = $this->mdl_bids->where(project_id', $this->input->post('project_id'))->where('bid_awarded', 0)->get()->result(); //this should be inside the model, but you get the idea...
  echo json_encode($result);
}
#6

[eluser]MLCstudio[/eluser]
Sure this will be very useful is a bit beyond my knowledge of javascript and jquery. Will take it round a friends.

Best

C




Theme © iAndrew 2016 - Forum software by © MyBB